
Working with compressed files is common in many applications, whether you’re extracting data from an archive, installing software packages or retrieving bundled files. Thankfully, .NET finally provides an efficient, straightforward way to decompress ZIP files using the System.IO.Compression namespace. In this post, I’ll walk through a simple code snippet that you can use to decompress ZIP files in your .NET apps.
1namespace BenjaminAbt.ZipFileSample;
2
3using System;
4using System.IO;
5using System.IO.Compression;
6
7class Program
8{
9 static void Main()
10 {
11 // fullname zip file
12 string zipFilePath = @"C:\ben\path\to\your\compressedFolder.zip";
13
14 // target folder to write files of the zip archive
15 string extractPath = @"C:\ben\path\to\extract\folder";
16
17 DecompressZipFile(zipFilePath, extractPath);
18 Console.WriteLine("ZIP file decompressed successfully!");
19 }
20
21 static void DecompressZipFile(string zipFilePath, string extractPath)
22 {
23 if (Directory.Exists(extractPath))
24 {
25 Directory.Delete(extractPath, recursive: true); // Delete existing directory to avoid conflicts
26 }
27
28 ZipFile.ExtractToDirectory(zipFilePath, extractPath);
29 }
30}How It Works
- Set File Paths: Define zipFilePath as the path to the ZIP file you want to decompress and
extractPathas the folder where you want the files extracted. - Handle Existing Directories: The snippet checks if a folder already exists at
extractPath. If it does, it deletes it to prevent conflicts during decompression. - Decompress with
ZipFile.ExtractToDirectory: The ExtractToDirectory method handles the decompression, extracting the contents of the ZIP file to the specified destination.
Related articles

- 16 min read
Production Rate Limiting in ASP.NET Core
Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

- 11 min read
The Big Decimal Bugfix with .NET 11
For more than two decades, (decimal)1.23 returned 1.23m. It looked correct, it passed every test and it was a polite lie.

- 10 min read
Distroless, Chiseled or Alpine: Choosing a .NET Base Image
Distroless, Chiseled and Alpine are often compared as three equivalent Linux distributions. They are not. Alpine is a distribution built …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.
