views:

5440

answers:

5

How can I create 7-Zip archives from my C# console application? I need to be able to extract the archives using the regular, widely available 7-Zip program.


Here are my results with the examples provided as answers to this question

  • "Shelling out" to 7z.exe - this is the simplest and most effective approach, and I can confirm that it works nicely. As workmad3 mentions, I just need to guarantee that 7z.exe is installed on all target machines, which is something I can guarantee.
  • 7Zip in memory compression - this refers to compressing cookies "in-memory" before sending to the client; this method seems somewhat promising. The wrapper methods (wrapping the LZMA SDK) return type byte[]. When I write the byte[] array to a file, I can't extract it using 7-Zip (File.7z is not supported archive).
  • 7zSharp Wrapper (found on CodePlex) - this wraps the 7z exe/LZMA SDK. I referenced the project from my app, and it successfully created some archive files, but I was unable to extract the files using the regular 7-Zip program (File.7z is not supported archive).
  • 7Zip SDK aka LZMA SDK - I guess I'm not smart enough to figure out how to use this (which is why I posted here)... Any working code examples that demonstrate creating a 7zip archive that is able to be extracted by the regular 7zip program?
  • CodeProject C# (.NET) Interface for 7-Zip Archive DLLs - only supports extracting from 7zip archives... I need to create them!
  • SharpZipLib - According to their FAQ, SharpZipLib doesn't support 7zip.
+20  A: 

EggCafe 7Zip cookie example This is an example (zipping cookie) with the DLL of 7Zip.

CodePlex Warper This is an open source project that warp zipping function of 7z.

7Zip SDK The official SDK for 7zip (C, C++, C#, Java) <---My suggestion

.Net zipping library by SharpDevelop.net

CodeProject example with 7zip

SharpZipLib Many zipping

Daok
Summarize the link, and I'll upvote this.
Joel Coehoorn
Done and I added an open source project useful too.
Daok
I have looked through the code supplied from the first link - I don't need to compress a cookie, I would like to create a 7-Zip file.
Terrapin
LOL? Change the cookie by a string... a file or whatever... lol?
Daok
The best solution I posted is the 7zip SDK over here. Take the dll and zip. Job is done :)
Daok
No no, if you want to create a 7-zip file you must compress a cookie!
Ed Guiness
hahaha yeah 7zip is for cookie only haha Terrapin you make my day :D
Mister Dev
just remember that compressed cookies need to be uncompressed before eating... otherwise you don't get any chocolate chips..... I think that's my crazy over with for today :P
workmad3
Terrapin what you need more to accept this answer?
Daok
Have you actually used any of the links that you posted? The first link refers to compressing a cookie in memory before sending to the client. The second link (7zSharp) is a wrapper around the SDK. I haven't been able to produce an archive that I can decode. SharpZipLib does not support 7zip. Code
Terrapin
The CodeProject example only supports decoding - "Currently my project only has extract capabilities". I'm looking for a useful answer to my question. I was also able to type a few keywords into Google to return the same results. If they would have been helpful, I wouldn't have asked here.
Terrapin
Alright you want us to code it for you? NIce...
Daok
No, I just want to see if anyone has come up with a working example... Thank you for your research, but none of the links you provided have been able to lead me to something that works.
Terrapin
I have to agree that none have a code that you can copy and paste. The SDK is the one with all answer, but it takes some time and effort to have something that work. I can understand you do not want to do it. Have a nice day.
Daok
+8  A: 

If you can guarantee the 7-zip app will be installed (and in the path) on all target machines, you can offload by calling the command line app 7z. Not the most elegant solution but it is the least work.

workmad3
We actually ship the 7z command line utility with our binaries and shell out to it. Works really well for us.
David Mohundro
A: 

SharpZipLib

I have used SharpZipLib in a production environment even though it is technically beta. I thought it worked well.

Jason Jackson
+3  A: 

I used the sdk.

eg:

using SevenZip.Compression.LZMA;
private static void CompressFileLZMA(string inFile, string outFile)
{
   SevenZip.Compression.LZMA.Encoder coder = new SevenZip.Compression.LZMA.Encoder();

   using (FileStream input = new FileStream(inFile, FileMode.Open))
   {
      using (FileStream output = new FileStream(outFile, FileMode.Create))
      {
          coder.Code(input, output, -1, -1, null);
          output.Flush();
      }
   }
}
WOPR
And how will you decompress the file? I tried to do the same using the SevenZip.Compression.LZMA.Decoder and calling it with Code(stream1, stream2, -1, -1, null);That gave an data exception from the framework.
Karsten
+1  A: 

SevenZipSharp is another solution. Creates 7-zip archives...

markhor