Dot Net Perls

.NET - 7-Zip Executable Tutorial

by Sam Allen

Problem

You need the absolute best compression that is available in your C# .NET program. You also need many compression options and ways to compress many files into one archive. Embed 7-Zip, an open source compressor, in your .NET program.

Solution: C#

What we will do here is embed the 7-zip command-line executable in a C# program and then invoke it with the Process class. The result will be compression that is 10% better than GZipStream or other common methods.

1. First things first

Create a new C# project (either Console or Windows Forms, although any will work). Here we will show a console application. Next, you need to download the executable.

2. Visit 7-Zip.org and download

Go to the downloads page at 7-Zip.org and download the console application. The description is "7-Zip Command Line Version" and the filename is 7za457.zip. This is the file we will embed in our program. After you download, unzip it.

3. Add 7za.exe to your project

Right-click on your project name in the Solution Explorer and select Add Existing Item. You need to change the drop down icon on the dialog to "All Files (*.*)", which will show the 7za.exe executable.

Copy if newer. With Visual Studio, you must specify "Copy if newer" or "Copy always" to copy files such as executables to the output directory. Specify either of those options to copy the 7za.exe executable.

Add example text file. We need an example file to test 7-Zip with, so add a new text file to your console project. Make sure to specify "Copy if newer" for it too. This will be our source file.

4. Implement C# code

Here we add the code to control the 7-Zip executable. In the first part of the code, we specify the source file name and the target file name. The source file name is the name of the text file you added to the project.

Target file name. The target name is the location of the archive you want to create. Note that if the archive is already there, you will have to delete it or tell 7-Zip to overwrite it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

class Program
{
    static void Main()
    {
        string sourceName = "ExampleText.txt";
        string targetName = "Example.gz";

        // 1
        // Initialize process information.
        //
        ProcessStartInfo p = new ProcessStartInfo();
        p.FileName = "7za.exe";

        // 2
        // Use 7-zip
        // specify a=archive and -tgzip=gzip
        // and then target file in quotes followed by source file in quotes
        //
        p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
        p.WindowStyle = ProcessWindowStyle.Hidden;

        // 3.
        // Start process and wait for it to exit
        //
        Process x = Process.Start(p);
        x.WaitForExit();
    }
}
  1. Initialize process information.
    We use the ProcessStartInfo class and set the FileName to the name of the 7za.exe executable in the project.
  2. Use 7-Zip.
    This is the hard part. Pay close attention to how the quotes are escaped and used in the line where Arguments is set. In this example, I use GZip compression. I will show more on 7-Zip's arguments later on.
  3. Start process and wait.
    This part of the code example will actually start the Process and execute it. Note that Vista can cause problems here if you are not an administrator on your PC.

5. Verify your results

Now we need to check if our project worked properly. Open the bin\Debug or bin\Release folder in your project's directory, and you should see the compressed file. Extract that file with 7-Zip in Windows, and it will have the same data.

What could possibly go wrong?

Lots of stuff. First make sure all the files are being copied and the Arguments syntax is correct. The quotes in the syntax are really important if you deal with more complicated paths. After that, you might have problems with Vista's UAC stuff. Note that the first argument (a) in the command doesn't have a hyphen (-) before it.

What other options can I use?

I won't go over every option, because 7-Zip itself provides an excellent reference. Go to the 7-Zip folder and double-click on the "7-zip.chm" file. That's a compiled HTML help file that you can browse for many options.

Specify compression levels. You want the smallest possible compressed files, but don't want to wait for hours. On the example, I use -mx=9, which sets the maximum for Zip and GZip. Experiment with this.

FlagDescription
-mx=0No compression
-mx=1Fastest
-mx=3Fast
-mx=5Normal
-mx=7Maximum
-mx=9Ultra
You'll want this one!

Specify archive type. You may need something different than GZip in your program. The .7z format probably has the best compression ratio. If you want to use HTTP compression, use GZip, but if you are archiving, then I suggest 7z. Specify the compression with these flags.

FlagDescription
-t7zProbably best compression
-tzipStandard compression
-tgzipUse for HTTP compression
-tbzip2,
etc...
Many more options
(use at your own risk!)

Is this worthwhile?

Yes, but only if you are trying to achieve excellent compression ratios. Otherwise, use GZipStream in your C# programs or just Windows' zip capabilities.

HTTP compression. If you have a website, use 7-Zip to compress static resources. Use GZip like I show in the code example, and with level 9 compression, your web pages will be 10% smaller than most GZip-compressed pages.

HTTP compression results

For my web site's static pages, 7-Zip was a substantial improvement over standard compression methods. Look at how I reduced the size of my files 10% over standard GZip.

Summary

Sometimes it is best to go outside the .NET framework when developing and use an open-source exe like 7-Zip. For me, 10% is a big improvement and 7-Zip is definitely worthwhile. Here I showed an effective way of embedding 7-Zip and improving compression ratios.

More reading. There is a very thorough guide to using 7-Zip and 7za.exe on the console, which can help with many problems using 7-Zip on the console.

 
© 2008 Sam Allen. All rights reserved.

Ads by The Lounge