ZipFile. The C# ZipFile class compresses directories. We call CreateFromDirectory and ExtractToDirectory. All files in the directory are compressed to, or expanded to, the directory.
Add reference. In Visual Studio, you may need to add a reference to your project to use ZipFile. Go to the Project menu, Add Reference, and then select System.IO.Compression.FileSystem.
Example. To start, this C# example uses the CreateFromDirectory and ExtractToDirectory methods. It uses hard-coded relative path names.
Info The directory we want to compress is "source" and the output file is "destination.zip."
And After execution, the folder the program resides in will have a file "destination.zip" and a folder "destination."
Note This program will cause an error if it is run twice. You must delete the previous output files for correct operation.
C# program that compresses folder, ZipFile
static void Main()
// Create a ZIP file from the directory "source".
// ... The "source" folder is in the same directory as this program.
// ... Use optimal compression.ZipFile.CreateFromDirectory("source", "destination.zip",
// Extract the directory we just created.
// ... Store the results in a new folder called "destination".
// ... The new folder must not exist.ZipFile.ExtractToDirectory("destination.zip", "destination");
Example 2. Next, we use the ZipArchiveEntry class. After calling ZipFile.Open, we can loop over the entries within the ZIP file by using the Entries property on the ZipArchive class.