How to do 7zip programmatically using C# ?

Dear All,

Some times it may be possible you require to  make zip of  a file or directory.

so here is simple example.

To start it you have to download “SevenZipSharp.dll”, “7z.dll” you can download sdk from http://www.7-zip.org/sdk.html

Once you download sdk copy above 2 DLL in your project bin folder.

Now write below code which i am writing here

   1:  

   2: public bool CompressFolder(String 

   3: backupFolder)

   4:  

   5: {

   6:  

   7: try

   8:  

   9: {

  10:  

  11: SevenZipCompressor sz = new SevenZipCompressor(); 

  12:  

  13: SevenZip.SevenZipCompressor.SetLibraryPath(DLLPath);

  14:  

  15: sz.CompressionLevel = 

  16: CompressionLevel.Ultra;

  17:  

  18: sz.CompressionMode = 

  19: CompressionMode.Create;

  20:  

  21: sz.CompressionMethod = 

  22: CompressionMethod.Default;

  23:  

  24: sz.FastCompression = 

  25: true;

  26:  

  27: sz.CompressDirectory(backupFolder, BackupPath + "\\" + 

  28: DateTime.Now.Date.ToString("yyyyMMddHHmmss") + 

  29: "_7Zip.7z");

  30:  

  31: Directory.Delete(backupFolder,true);

  32:  

  33: return true;

  34:  

  35: }

  36:  catch (Exception 

  37: ex)

  38:  

  39: {

  40:  

  41: throw ex;

  42:  }

Now let me describe the code to you.

This is function which compress the folder to 7zip what ever pass as a paramter

in this function we have created a sevenzip compressor object

SevenZip.SevenZipCompressor.SetLibraryPath(DLLPath);

in the above line we set the DLLpath which is basically your bin folder path

sz.CompressionLevel = CompressionLevel.Ultra; is compress level which can be different according to your need.

sz.CompressDirectory(backupFolder, BackupPath + "\\" + DateTime.Now.Date.ToString("yyyyMMddHHmmss") + "_7Zip.7z");

above row compress the directory of backupfolder and rename with datetime_7zip.7z

I hope this will help you.

Enjoy 7zip.

Thanks & Best Regards,

Rajat Jaiswla

Advertisements

One thought on “How to do 7zip programmatically using C# ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s