Zipping a directory using C# and SharpZipLib

While I have used C# and SharpZipLib to zip a directory before, I simple used the sample provided in the framework.   However the sample doesn’t support maintaining the directory structure in the zip file. 

So, I looked around the site very quickly and noticed a FastZip class.  This has all the common scenarios like CreateZip, ExtractZip and setting a password for the zip. Very simple, but very quick to integrate and test.

There is some sample code to zip the directory ZipTest into ZipTest.zip.

ICSharpCode.SharpZipLib.Zip.FastZip z = new ICSharpCode.SharpZipLib.Zip.FastZip();
z.CreateEmptyDirectories = true;
z.CreateZip(“F:ZipTest.zip”, “F:ZipTest”, true, “”);

if (File.Exists(“F:ZipTest.zip”))
    Console.WriteLine(“Done”);
else
    Console.WriteLine(“Failed”);

Technorati tags: ,

3 thoughts on “Zipping a directory using C# and SharpZipLib”

  1. Thnx for the info so far, it does exactly what is says it does.
    But is there a way that you can set the compressionlevel for fastzip just as with the regular zip class? i can’t seem to find it.

  2. This works for me in some ways, but it does not work in the filemask section if I want to zip all files within a directory..I set the bool to true but if i leave “” and put nothing inside the quotes, it creates the zip but puts nothing in it..I have tried putting “*.*” but it did not like that at all…Any tips?

  3. Hi,

    Thanks for the comments, its been a while since I last used this but I think you can’t use “*.txt”, you must use a regular expression to match on. Something like “.txt$” I think.

    Hope this works.

    Ben

Leave a Reply

Your email address will not be published. Required fields are marked *