Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SevenZipSharp - compress memory stream

I am using SevenZipSharp in order to compress files into a zip file. Is there a way to use it to create a zip from a memory stream (meaning, load the file into the memory stream before)?

Thanks, Maya.

like image 440
Maya Avatar asked Nov 18 '10 12:11

Maya


1 Answers

I'm using SevenZipSharp with streams no problem what so ever.

SevenZip.SevenZipCompressor compressor = new SevenZip.SevenZipCompressor();
compressor.CompressionMethod = SevenZip.CompressionMethod.Lzma2;
compressor.CompressionLevel = SevenZip.CompressionLevel.Normal;
compressor.CompressStream(ms, compressedStream);

On the final line, "ms" is a stream that I want to compress, let's say a MemoryStream. "compressedStream" is the stream I want to zip to, it can be either another MemoryStream or even a FileStream...

For decompression:

SevenZip.SevenZipExtractor extractor = new SevenZip.SevenZipExtractor(compressedStream);
extractor.ExtractFile(0, decompressedStream);

The SevenZipExtractor doesn't have a decompress stream method, so I use ExtractFile() instead.

Very easy. And before any of the above code is called I must specify the 7zip dll with:

SevenZip.SevenZipBase.SetLibraryPath(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\7-zip\\7z.dll");

In my case, I don't bundle the 7z.dll with my app, 7-zip is installed seperately onto the machine.

All in all super easy. I downloaded SevenZipSharp from codeplex - http://sevenzipsharp.codeplex.com/ and 7-zip from http://www.7-zip.org/.

like image 118
Marko Avatar answered Sep 22 '22 16:09

Marko