7zip压缩网络流

3
我想在通过网络发送文件之前对其进行压缩。我认为最好的方法是7zip,因为它是免费且开源的。
我知道7zip是免费的,并且他们有c#源代码,但出于某种原因,在c#上速度非常慢,所以我更愿意调用安装7zip时附带的7z.dll以提高性能。因此,我能够轻松地调用并调用7z.dll中的方法的方式是使用名为sevenzipsharp的库。例如,将该dll添加到我的项目中将使我能够执行以下操作:

enter image description here

        // 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 Files\7-Zip\7z.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);

这是我在C#中使用7zip的方法。
现在我的问题是:
我想通过网络发送一个压缩文件。我知道我可以先压缩它然后再发送。但是这个文件很大,有4GB,所以我要等很长时间才能完成压缩。我会浪费很多硬盘空间,最终才能将其发送。我认为这太复杂了。我想知道是否有可能在压缩的同时发送文件。

似乎是与SevenZipSharp有关的问题:

enter image description here


你应该了解这里的信息:http://sourceforge.net/projects/sevenzip/forums/forum/45798/topic/5086512 - Ben Voigt
另一种方法可以是创建跨越多个文件的压缩包。然后,在创建时可以传输编号文件,无需等待整个压缩完成。例如:MyZip.zip,MyZip.001,MyZip.002,... 但我的观点是流式压缩更好,因为它可以减少磁盘访问。 - erikH
我来晚了。你可以使用DeflateStream来完成这个任务,参考链接:http://stackoverflow.com/a/36499460/1878585 - Thariq Nugrohotomo
2个回答

2
您考虑过另一种库吗?它甚至不需要安装/可用7-Zip。根据http://dotnetzip.codeplex.com/上发布的描述:
“从流内容创建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/view/59007#364711
看起来你想要这个方法:
public void CompressStream(Stream inStream, Stream outStream)

谢谢您考虑在这里表现!我认为太多人会做您试图避免的事情:将文件压缩到临时文件,然后对临时文件进行操作。

我不需要安装7zip。我只需要那个dll,这样我就可以将其包含在我的项目中。我希望能使用7z,因为它的压缩比其他算法更高。 - Tono Nam
使用SevenZipSharp就像使用7z.exe一样。它有相同的方法需要调用:)。也许我没有理解你在编辑中的意思。非常感谢您的帮助! - Tono Nam
这就是我的代码。outStream 是一个网络流。我将继续进行编辑... - Tono Nam

0

CompressStream 抛出了一个异常。我的代码如下:

    public void TestCompress()
    {
        string fileToCompress = @"C:\Users\gary\Downloads\BD01.DAT";
        byte[] inputBytes = File.ReadAllBytes(fileToCompress);
        var inputStream = new MemoryStream(inputBytes);

        byte[] zipBytes = new byte[38000000];   // this memory size is large enough.
        MemoryStream outStream = new MemoryStream(zipBytes);

        string compressorEnginePath = @"C:\Engine\7z.dll";
        SevenZipCompressor.SetLibraryPath(compressorEnginePath);

        compressor = new SevenZip.SevenZipCompressor();
        compressor.CompressionLevel = CompressionLevel.Fast;
        compressor.CompressionMethod = CompressionMethod.Lzma2;
        compressor.CompressStream(inputStream, outputStream);

        inputStream.Close();
        outputStream.Close();

异常信息: 消息:测试方法Test7zip.UnitTest1.TestCompress引发异常: SevenZip.SevenZipException:由于SevenZipSharp中的错误,执行失败。 请向http://sevenzipsharp.codeplex.com/WorkItem/List.aspx报告此问题,发布版本号并附上存档。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接