7zip压缩网络流 [英] 7zip compress network stream

查看:36
本文介绍了7zip压缩网络流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在通过网络发送文件之前对其进行压缩.我认为最好的方法是 7zip,因为它是免费和开源的.

我如何在 .net 中使用 7zip?

我知道 7zip 是免费的,而且他们有 c# 的源代码,但由于某种原因它在 c# 上很慢,所以我宁愿调用 dll 7z.dll 出于性能原因安装 7zip 时会出现这种情况.因此,我能够在 7z.dll 中轻松编组和调用方法的方法是在名为 上发布的描述:

<块引用>

从流内容创建 zip 文件,保存到流,提取到流,从流中读取

与 7-Zip 不同,DotNetZip 旨在与 C#/.Net 配合使用.

http://上提供了大量示例 - 包括流式传输dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples .

另一种选择是使用 7-Zip 命令行版本 (7z.exe),并从标准输入/输出写入/读取.这将允许您使用 7-Zip 文件格式,同时还将所有核心工作保留在本机代码中(尽管可能不会有太大差异).

回顾 SevenZipSharp:

<块引用>

从 0.29 版本开始,就支持流式传输.

查看 http://sevenzipsharp.codeplex.com/SourceControl/changeset/查看/59007#364711 :

您似乎想要这种方法:

public void CompressStream(Stream inStream, Stream outStream)

感谢您考虑这里的性能!我认为太多人会做你想要避免的事情:压缩到临时文件,然后对临时文件做一些事情.

I will like to compress a file before sending it through the network. I think the best approach is 7zip because it is free and open source.

How I use 7zip with .net?

I know that 7zip is free and that they have the source code in c# but for some reason it is very slow on c# so I rather call the dll 7z.dll that comes when installing 7zip for performance reasons. So the way I am able to eassily marshal and call the methods in 7z.dll is with the help of the library called sevenzipsharp . For example adding that dll to my project will enable me to do:

        // if you installed 7zip 64bit version then make sure you change plataform target
        // like on the picture I showed above!
        SevenZip.SevenZipCompressor.SetLibraryPath(@"C:Program Files7-Zip7z.dll");

        var stream = System.IO.File.OpenRead(@"SomeFileToCompress.txt");
        var outputStream = System.IO.File.Create("Output.7z");

        SevenZip.SevenZipCompressor compressor = new SevenZip.SevenZipCompressor();
        compressor.CompressionMethod = SevenZip.CompressionMethod.Lzma2;
        compressor.CompressionLevel = SevenZip.CompressionLevel.Ultra;
        compressor.CompressStream(stream, outputStream);

that's how I use 7zip within c#.

Now my question is:

I will like to send a compressed file over the network. I know I could compress it first then send it. The file is 4GB so I will have to wait a long time for it to compress. I will be wasting a lot of space on hard drive. then I will finally be able to send it. I think that is to complicated. I was wondering how it will be possible to send the file meanwhile it is being compressed.


It seems to be a problem with SevenZipSharp:

解决方案

Have you considered an alternate library - one that doesn't even require 7-Zip to be installed / available?

From the description posted at http://dotnetzip.codeplex.com/ :

creating zip files from stream content, saving to a stream, extracting to a stream, reading from a stream

Unlike 7-Zip, DotNetZip is designed to work with C# / .Net.

Plenty of examples - including streaming, are available at http://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples .

Another option is to use the 7-Zip Command Line Version (7z.exe), and write to/read from standard in/out. This would allow you to use the 7-Zip file format, while also keeping all of the core work in native code (though there likely won't be much of a significant difference).

Looking back at SevenZipSharp:

Since the 0.29 release, streaming is supported.

Looking at http://sevenzipsharp.codeplex.com/SourceControl/changeset/view/59007#364711 :

it seems you'd want this method:

public void CompressStream(Stream inStream, Stream outStream)

Thank you for considering performance here! I think way too many people would do exactly what you're trying to avoid: compress to a temp file, then do something with the temp file.

这篇关于7zip压缩网络流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆