SevenZipSharp: Compressor (Asynchronous)

1.      Import the SevenZip namespace in your namespace first.

  1. using SevenZip;

2.      Create object from SevenZipCompressor with a path for the file output

  1. SevenZipCompressor sc = new SevenZipCompressor(@"C:\Users\Thada\Desktop\Test7Zip\");

PS. add @ sign in front of path string. The @-quoting will tell the compiler that the escape sequences are not processed.

PS. leave \ sign at the last character of string too.

3.      Use BeginCompressFilesEncrypted method to compress files and encrypt with a password

  1. BeginCompressFilesEncrypted( Output file name including path, password, either list of file path or array of file path);

  1. sc.BeginCompressFilesEncrypted(sc.TempFolderPath + "test.7z", "passwordhere", @"C:\Users\Thada\Desktop\Test7Zip\a.txt", @"C:\Users\Thada\Desktop\Test7Zip\b.txt", @"C:\Users\Thada\Desktop\Test7Zip\c.txt");

or

 

  1. String[] files = { @"C:\Users\Thada\Desktop\Test7Zip\a.txt", @"C:\Users\Thada\Desktop\Test7Zip\b.txt", @"C:\Users\Thada\Desktop\Test7Zip\c.txt" };
  2. sc.BeginCompressFilesEncrypted(sc.TempFolderPath + "test.7z", "passwordhere", @"C:\Users\Thada\Desktop\Test7Zip\a.txt", @"C:\Users\Thada\Desktop\Test7Zip\b.txt", @"C:\Users\Thada\Desktop\Test7Zip\c.txt");

4.      Done!! But not completely. The method that we are using is asynchronous which means you cannot simply tell that statements after the line of calling BeginCompressFilesEncrypted will work after the compression has done. So, you cannot insert any statement that has to be cooperating with the BeginCompressFilesEncrypted. Because of that, SevenZipSharp also provides events for the compressing operation to fix this situation.

There are 5 events:

  • Compressing
  • CompressionFinished << Focus on this
  • FileCompressionStarted
  • FilesFound
  • FileCompressionFinished

5.      We have to embed the method for the event. So, start typing your object’s name and follow by dot with CompressionFinished.

Note: try to use intellisense to help you coding. When you are typing, there will be a list box of names that are partial match of your typing. Then click tab or spacebar button, it will autocomplete for you.

6.      Type operation += and then click tab twice. Visual Studio will complete the statement and create a method for you. You will get something like these.

  1. sc.CompressionFinished += new EventHandler<EventArgs>(sc_CompressionFinished);

  1. void sc_CompressionFinished(object sender, EventArgs e)
  2. {
  3. }

7.       Add any statement you want in the method. After the compression finished, it will call this method automatically.

Reference:

Vadim, M. (2010, 8 27). SevenZipSharp Documentation. Retrieved 1 4, 2011, from http://sevenzipsharp.codeplex.com/releases/51254/download/145909