create an 7zip archive with sharpcompress

7.7k views Asked by At

I need to compress a file as 7zip using SharpCompress: http://sharpcompress.codeplex.com

what I have done as follows:

using (var archive = ZipArchive.Create())
{
     archive.AddEntry("CompressionTest.pdb", new FileInfo("CompressionTest.pdb"));

     using (Stream newStream = File.Create("CompressionTest212.7z"))
     {
         archive.SaveTo(newStream, SharpCompress.Common.CompressionType.LZMA);
     }
 }

The compression process is done successfully. However, the compressed file can not be extracted either using 7z (http://www.7-zip.org/download.html) or winrar.

I dont know if somebody also got the same problem and had an idea how to solve it?

Thanks in advance.

2

There are 2 answers

2
Polity On BEST ANSWER

SharpCompress doesn't support 7zip compression. Only decompression, see: http://sharpcompress.codeplex.com/ ( Supported Format Table )

You can use the native library of 7zip for compression, or use an opensource wrapper around it like: http://sevenzipsharp.codeplex.com/

1
adamhathcock On

I'm the author of SharpCompress (thanks for trying it out by the way) and 7Zip compression isn't supported: http://sharpcompress.codeplex.com/wikipage?title=Supported%20formats

What you wrote is code for creating a standard Zip file with LZMA compression. It's possible my code does not create a proper zip file but it's also possible that the created file can't be read by all programs. The Zip format allows for LZMA compression but not all programs may expect it. PeaZip (based on the 7Zip archiver code) does extract a Zip with LZMA, but WinRAR does not.

If you really need the 7Zip format, I do suggest using something else. Personally, I think the 7Zip format is overly complex and recommend Zip or Tar then just pick your compression of choice.