tags:

views:

378

answers:

5

I have installed 7-zip 4.65 on my machine at C:\Program files. I want to use it in C# code to zip a file. The file name will be provided by the user dynamically. Can any one please provide a sample code on how to use 7zip in C# code?

+3  A: 

Instead of using 7-zip, I recommend using SharpZipLib, which has code samples, documentation, written as 100% managed code. I've had great success with it, and its fairly simple to use.

Dave Markle
The requrirement is to use 7-zip. I cannot go for SharpZipLib
SharpZipLib *uses* 7-zip. It is just code you don't have to write.
Hans Passant
@Hans, very interesting. Can you point me to some documentation that confirms that SharpZipLib can deal with 7z files? Or failing that where in the code I can see that?
Stefan Egli
@Stefan: I looked at the code briefly before and it was on the very first line of code, a declaration to help find the unmanaged 7z.dll library.
Hans Passant
A: 

Or you can use the J# zip library (which is included in the .Net Framework) a sample: http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx

Flakron Bytyqi
Can J# do the .7z format? (OP didn't specify which format, but I know 7zip defaults to its own format, though it can do .zip too.)
Joe White
+2  A: 

Instead of the binary version you need the source code.

This can be get as the LZMA SDK.

There you'll find a folder CS that contains a C# implementation of the algorithm for 7zip files.

Oliver
A: 

I suppose if you wish to use the installed one you have in c:\program files, you could just use System.Diagnostics.Process to run command line apps - http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

Passing parameters is easy too. There are plenty of examples here - http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ShellCommandsInCS12032005042031AM/ShellCommandsInCS.aspx

Frank Tzanabetis