How to ZIP Folder in C#

This quick article contains information on how to zip folder in C#. It provides detailed steps to be performed while writing a tool to create a ZIP file for a folder. A few lines of code in C# zip folder and files in all of its sub-folders without installing any third-party tool.

Steps to Zip Folder in C#

  1. Add Aspose.ZIP from the NuGet package manager to zip the folder
  2. Instantiate a FileStream class object with output ZIP file name
  3. Instantiate a ZIP Archive file object
  4. Create entries in the archive by providing the target folder name
  5. Save the archive to create a ZIP file containing all the files and sub-folders

These steps describe the process in detail by first providing the environment configuration and then introducing all the main classes required for creating a ZIP file for a folder. To zip folder C# coding steps are given afterward for the complete understanding.

Code to Zip Folder in C#

These lines of code in C# create ZIP file from folder. There are a few variations possible to accomplish the task e.g. instead of providing the target folder name, you may provide the DirectoryInfo class object as a source of the files for the output ZIP file. Similarly, you can also set a flag to include the root folder in the output ZIP file.

This tutorial has guided us to zip a complete folder. If you are interested to learn the reverse process i.e. extract ZIP file, refer to the article on how to extract ZIP file in C#.

 English