How to create and extract a zip archive by using the ZipFile class in C#

The following code snippet shows how to create a zip file of the folder by using ZipFile Class in c#. In .Net framework 4.5, C# includes a new class in System.IO.Compression.FileSystem. However before 4.5 we have been using GZipStream Class that Provides methods and properties used to compress and decompress streams. Before using the example you need to add reference the System.IO.Compression.FileSystem assembly in your project.
 
This example shows how to create and extract back a file by using the ZipFile class in C#. It compresses the contents of a folder into a zip archive, and then extracts back to a new folder.
 

  private void perform_ZipActions()
        {
            string FolderPath = @"c:\Sample";
            string ZipFolderPath = @"c:\Sample.zip";
            string ExtractPath = @"c:\extract\Sample";
            // Create Zip file of the folder 'Sample'
            CreateZipFile(FolderPath, ZipFolderPath);
            // Extract back Sample.zip to the 'Sample'
            ExtractZipFile(ZipFolderPath, ExtractPath);
        }

Note: You can see from the above, we declare the folders path as preceding by @, it is called by verbatim string literal. Verbatim string literals begin with @” and end with the matching quote. They do not have escape sequences. See the below:
string FolderPath = @”c:\Sample”; is same as :
string FolderPath = “c:\\Sample”;

 
Create a zip file:

private void CreateZipFile(string SourcePath, string DestinationPath)
 {
   try
    {
     ZipFile.CreateFromDirectory(SourcePath, DestinationPath);
    }
   catch (DirectoryNotFoundException dirEx)
    {
      // Let the user know that the directory did not exist.
      MessageBox.Show("Source File is not Found!" + dirEx.Message);
    }
   catch (NotSupportedException NotSupportex)
    {
     MessageBox.Show("sourceDirectoryName or destinationArchiveFileName contains"+
                "an invalid format!" + NotSupportex.Message);
    }
   catch (UnauthorizedAccessException permissionEx)
    {
     MessageBox.Show("you have not the required permission to access " +
           " the archive or the destination directory." + permissionEx.Message);
    }
   catch (Exception ex)
    {       
       MessageBox.Show("Unable to create Zip file!" + ex.Message);
    {      
 }

 
Let’s examine the above function, firstly we are passing the source folder path and zip folder path as arguments in the function ‘CreateZipFile’. We are using the Try and Catch block for handling exceptions.
In try block we call the zipFile.CreateFromDirectory method directly without creating an object of the zipFile class because both are the static.
First catch block is for handling the very common exception- DirectoryNotFoundException. If user will pass the wrong source folder path then exception will be thrown by this catch block. Next catch block is for handling NotSupportedException exception such as if source and destination folder path contains some invalid characters for format. If user has not enough permission to create zip file then UnauthorizedAccessException exception will be thrown by the next catch block.

 
Extract a zip file:

private void ExtractZipFile(string SourcePath, string DestinationPath)
{
  try
     {
       ZipFile.ExtractToDirectory(SourcePath, DestinationPath);
    }
  catch (DirectoryNotFoundException dirEx)
    {
      // Let the user know that the directory did not exist.
      MessageBox.Show("Source File is not Found!" + dirEx.Message);
    }
  catch (NotSupportedException NotSupportex)
   {
    MessageBox.Show("sourceDirectoryName or destinationArchiveFileName contains" +
                "an invalid format!" + NotSupportex.Message);
   }
  catch (UnauthorizedAccessException permissionEx)
  {
   MessageBox.Show("you have not the required permission to access " +
           " the archive or the destination directory." + permissionEx.Message);
   }
  catch (Exception ex)
  {
    MessageBox.Show("Unable to create Zip file!" + ex.Message);
  }
 }

 
Before run the program you must add reference the System.IO.Compression.FileSystem assembly in your project.