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#
- Add Aspose.ZIP from the NuGet package manager to zip the folder
- Instantiate a FileStream class object with output ZIP file name
- Instantiate a ZIP Archive file object
- Create entries in the archive by providing the target folder name
- 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#.