Friday, October 7, 2011

Using DotNetZip to Compress all the Files in a Folder in C#


The inbuilt Compression class GzipStream class(System.IO.Compression) for C# does not provide the ability to compress all the files within a folder.You can do this by just few lines of code and just forgetting the techinical aspects behind it by using DotNetZip(Ioniczip).DotNetZip is a free class library that can be used for creating and editing zip files.

The project is Hosted at CodePlex check out http://dotnetzip.codeplex.com
To use DotNetZip Library in your C# Project follow these steps
  • Download the latest release from http://dotnetzip.codeplex.com/releases/
  • Add a Reference to DotNetZip from your project by going to Project> Add Reference .Browse for Ionic.Zip.dll in the release that you have downloaded.
  • Then add the reference in your project by adding the statement 'using Ionic.Zip'
  • Then use the code given below to create a zip file from a folder
 using (ZipFile zip = new ZipFile())  
 {  
   zip.AddDirectory(@"thefoldertoadd");  
   zip.Save("pathToSaveZipFile");  
 }  

2 comments:

Post a Comment

Subscribe

The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.