Programmatically Zip and UnZip the files in .NET

Normally we may find the functionality to Zip and Unzip the file pro grammatically.We have many third party components to do this operation.Here I am going to present Zip and Unzip files using "CGZipLibrary.dll" in C#.NET.

To do this we need to download the CGLibrary.dll from this link and register this dll.

The zip file contains 3 DLL's:
  1. CGZipLibrary.dll
  2. zip32.dll
  3. Unzip32.dll
Open the command prompt in administrator mode.Now we need to register this CGZipLibrary.dll as given below

regsvr32 filepath\filename

Ex: regsvr32 D:\Download\CGLibrary.dll

After registering the dll and Copy zip32.dll and unzip32.dll to

c:\windows\system32 directory or c:\winnt\system32.

Unzip the file you need to do like this

           string SrcPath = @"D:\StudentReports.zip";
            string DestPath = @"D:\ZippedFiles";
            try
            {
                CGUnzipFiles oUnzip = new CGUnzipFiles();
                oUnzip.ZipFileName = SrcPath;
                oUnzip.ExtractDir = DestPath;
                int result = oUnzip.Unzip();
            }
            catch (Exception ex)
            {
                throw ex;
            }
Zip the file you need to do like this
               CGZipFiles oZip = new CGZipFiles();
                oZip.AddFile(@"D\Test1.txt");
                oZip.AddFile(@"D\Test2.txt");
                oZip.AddFile(@"D\Test3.txt");
                oZip.ZipFileName = @"D:\ZippedFiles";
                int result= oZip.MakeZipFile();
     
Like this we can do Zip and Unzip the files programmatically.
Happy coding :-)

Popular posts from this blog

Exporting to excel from a custom class object using C#.NET

Get the Clicked Cell Value and Column Name of GridView in ASP.NET

How to apply watermark to the textbox and dropdownlist?