How to Zip and Unzip File in Asp.Net?


Asp.Net 4.5 introduces a new feature Zip (Archive file format).
To use this feature, We need to reference two namespaces:

  • System.IO.Compression.FileSystem
  • System.IO.Compression

Zip file

//Import Namespace for ZipFile class
using System.IO.Compression;
private void CompressFile()
{
    String Source = @"C:/SourceFolder";
    String Destination = @"C:/DestinationFolder.zip";
    ZipFile.CreateFromDirectory(Source, Destination);
}

UnZip file

//Import Namespace for ZipFile class
using System.IO.Compression;
private void DeCompressFile()
{
   String Source = @"C:/SourceFolder.zip"
   String Destination = @"C:\DestinationFolder"; 
   ZipFile.ExtractToDirectory(Source, Destination);
}