Homepage | About Me | Mayday | Testing ASP.net Book | Follow me on Twitter | GitHub | SlideShare | RSS | DropBox referral link
Blog.BenHall.me.uk

Zipping a directory using C# and SharpZipLib

Sunday, September 23, 2007

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: ,

Labels:

Blogger comments

At 11:43 AM, Anonymous Anonymous said...
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.    
At 7:54 PM, Anonymous Anonymous said...
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?    
At 10:05 PM, Blogger Ben Hall said...
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