Working with ZIP Archives in C# (.NET): A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with ZIP Archives in C# (.NET): Creating, Reading, and Managing



In modern applications, there is often a need to compress files to save space or for ease of transfer. The .NET platform provides powerful and convenient tools for working with ZIP archives right out of the box. Using the System.IO.Compression namespace, you can easily create, extract, and modify archives without installing third-party libraries. In this article, we will cover all the main operations in detail.



Adding Required Namespaces

To get started, make sure the following directives are added to your project:

using System;using System.IO;using System.IO.Compression;using System.Linq;


Creating a ZIP Archive

The easiest way to create an archive is to use the ZipFile class. It is available starting from .NET Framework 4.5 and .NET Core.



Creating an Archive from a Single Folder

string sourceDirectory = @"C:\\MyDocuments";string zipFilePath = @"C:\\MyArchive.zip";

// Create an archive from all files in the specified folderZipFile.CreateFromDirectory(sourceDirectory, zipFilePath);Console.WriteLine("Archive created: " + zipFilePath);


Creating an Archive with Additional Parameters

You can specify the compression level and add a path prefix inside the archive:

string sourceDir = @"C:\\Data";string outputZip = @"C:\\Data.zip";

// CompressionLevel.Optimal - maximum compression// includeBaseDirectory: true - will preserve the folder structureZipFile.CreateFromDirectory( sourceDir, outputZip, CompressionLevel.Optimal, includeBaseDirectory: true);Console.WriteLine("Archive created with optimization.");


Extracting Files from an Archive

Extracting the Entire Archive to a Folder

string zipPath = @"C:\\MyArchive.zip";string extractPath = @"C:\\ExtractedFiles";

// Extract all contentsZipFile.ExtractToDirectory(zipPath, extractPath);Console.WriteLine("Archive successfully extracted.");


Extracting with Overwriting Existing Files

// In .NET 6+ you can specify overwritingZipFile.ExtractToDirectory(zipPath, extractPath, overwriteFiles: true);


Working with Individual Entries in an Archive

The ZipArchive class gives you full control over the contents of the archive.



Reading the List of Files in an Archive

using (ZipArchive archive = ZipFile.OpenRead(@"C:\\Archive.zip")){    foreach (ZipArchiveEntry entry in archive.Entries)    {        Console.WriteLine($"File: {entry.FullName}, Size: {entry.Length} bytes");    }}


Extracting a Specific File from an Archive

string zipFilePath = @"C:\\Archive.zip";string entryName = @"folder\\document.txt"; // path inside the archivestring outputPath = @"C:\\Output\\document.txt";

using (ZipArchive archive = ZipFile.OpenRead(zipFilePath)){ ZipArchiveEntry entry = archive.GetEntry(entryName); if (entry != null) { // Extract the file entry.ExtractToFile(outputPath, overwrite: true); Console.WriteLine($"File '{entryName}' extracted."); } else { Console.WriteLine("File not found in the archive."); }}


Adding and Removing Files from an Existing Archive

To modify an existing archive, open it in ZipArchiveMode.Update mode:



Adding a New File to an Archive

string existingZip = @"C:\\Archive.zip";string fileToAdd = @"C:\\NewFile.txt";

using (ZipArchive archive = ZipFile.Open(existingZip, ZipArchiveMode.Update)){ // Add the file with the name "NewFile.txt" to the root of the archive archive.CreateEntryFromFile(fileToAdd, "NewFile.txt"); Console.WriteLine("File added to the archive.");}


Removing a File from an Archive

using (ZipArchive archive                    

Recommendations