7zip compress network stream
Asked Answered
N

2

3

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:

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);

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:

enter image description here

Nosepiece answered 8/9, 2012 at 4:40 Comment(3)
You should know the information here: sourceforge.net/projects/sevenzip/forums/forum/45798/topic/…Delimitate
Another way could be to create spanned zip files. Then the numbered files could be transfered when created, without waiting for the entire compression to finish. E.g. MyZip.zip, MyZip.001, MyZip.002, ... But my opinion is that compress to stream is better due to less disc access.Spickandspan
I'm late to join the party. You can do this using DeflateStream instead https://mcmap.net/q/831702/-how-to-lock-a-streamAvera
L
2

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.

Londoner answered 8/9, 2012 at 4:52 Comment(3)
I dont require to have 7zip installed. I just need that dll so I could include that on my project. I was hoping I could use 7z because it's compression seems to be higher than other algorihtmsNosepiece
Using SevenZipSharp is like using 7z.exe. It has the same methods to be invoked :) . Maybe I did not understood what you meant on your edit. Thanks a lot for the help!Nosepiece
That's how I have it. The outStream is a network stream. I will work on an edit...Nosepiece
T
0

CompressStream threw an exception. My code is as follows:

    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();

The exception messages: Message: Test method Test7zip.UnitTest1.TestCompress threw exception: SevenZip.SevenZipException: The execution has failed due to the bug in the SevenZipSharp. Please report about it to http://sevenzipsharp.codeplex.com/WorkItem/List.aspx, post the release number and attach the archive

Tani answered 27/12, 2018 at 0:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.