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): A Complete Guide

In modern .NET platform development, working with archives is a common task. Whether it's packaging logs, extracting configurations, or creating backups, the built-in System.IO.Compression tools allow you to do this efficiently and without third-party libraries. In this article, we will cover the main operations with ZIP archives in C#, including compression, extraction, adding files, and working with streams.



Required Namespaces

Two main namespaces are used for working with archives in .NET:

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

Starting with .NET Framework 4.5, the ZipFile class is available in the System.IO.Compression.FileSystem assembly. In .NET Core / .NET 5+, it is included in the base library.



Creating a ZIP Archive from Files

The simplest way to create an archive is to use the static method ZipFile.CreateFromDirectory. It takes the folder path, archive path, and compression level.

string sourceFolder = @"C:\\Temp\\Documents";string zipPath = @"C:\\Temp\\backup.zip";

// Create an archive with Optimal compressionZipFile.CreateFromDirectory(sourceFolder, zipPath, CompressionLevel.Optimal, false);

Console.WriteLine($"Archive created: {zipPath}");

Parameters:

  • sourceFolder — the source directory;
  • zipPath — the path to the future archive;
  • CompressionLevel — Optimal, Fastest, or NoCompression;
  • includeBaseDirectory — if true, the root folder will be included in the archive.


Extracting a ZIP Archive

File extraction is performed using the ZipFile.ExtractToDirectory method:

string zipPath = @"C:\\Temp\\backup.zip";string extractPath = @"C:\\Temp\\Restored";

// Extract with overwriting existing filesZipFile.ExtractToDirectory(zipPath, extractPath, overwriteFiles: true);

Console.WriteLine("Archive extracted.");

In .NET 6+, the overwriteFiles parameter was introduced, allowing files to be overwritten without exceptions.



Adding Files to an Existing Archive

The ZipArchive class gives you full control over the contents. Let's open the archive in update mode and add a new file:

string zipPath = @"C:\\Temp\\backup.zip";string newFile = @"C:\\TempewDocument.txt";

using (FileStream zipStream = new FileStream(zipPath, FileMode.Open, FileAccess.ReadWrite))using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Update)){ // Create an entry in the archive ZipArchiveEntry entry = archive.CreateEntry("newDocument.txt");

using (StreamWriter writer = new StreamWriter(entry.Open())) { writer.Write("This is the content of the new file."); }

Console.WriteLine($"File {newFile} added to the archive.");}

Important: when using ZipArchiveMode.Update, the archive must be opened for reading and writing.



Reading Archive Contents Without Extraction

Sometimes you just need to see what files are inside. To do this, iterate through the archive entries:

string zipPath = @"C:\\Temp\\backup.zip";

using (FileStream zipStream = new FileStream(zipPath, FileMode.Open, FileAccess.Read))using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Read)){ foreach (ZipArchiveEntry entry in archive.Entries) { Console.WriteLine($"File: {entry.FullName}, Size: {entry.Length} bytes"); }}

The FullName property contains the path inside the archive, including subfolders.



Working with Streams: Compression and Extraction in Memory

For scenarios where the archive needs to be sent over a network or saved to a database, it is convenient to use MemoryStream:<

Recommendations