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");
Labels: C#






Blogger comments
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.
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