SevenZipSharp - compress memory stream
Asked Answered
G

2

10

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.

Gauleiter answered 18/11, 2010 at 12:20 Comment(0)
P
14

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/.

Penneypenni answered 29/11, 2010 at 15:51 Comment(2)
This works. Note that SevenZipExtractor.DecompressStream throws an error if you use it like you'd expect.Glutathione
I know this is old, but I can't seem to get the 7z.dll to load/work properly. I can get the 7za.dll to load/work, but it only supports .7z, and I am looking for .zip support. Where/what 7z.dll is the "correct" dll to get? thanks!Pokelogan
S
0

Unfortunately is SevenZipSharp just a COM wrapper around the 7z application. So there is no easy stream support. Maybe it is possible to instruct it through <SevenZipCompressor Instance>.CustomParameters.Add() to output the file to stdout, which can then be read in somehow in your application. But this is just speculative and i have no direct tip on how to get this to work.

September answered 18/11, 2010 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.