
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

Aug 15, 2024 - 2 min read
Compress a folder of files to a ZIP archive with .NET
When working with .NET apps you may sometimes need to bundle multiple files into a single compressed archive for easier storage, transfer or …

Aug 02, 2024 - 1 min read
Read cpu load on Windows with .NET
In C# (.NET 6 and above), there is now a very simple way to read out the CPU load with minimal overhead - i.e. without threads. The …

Jul 22, 2024 - 2 min read
.NET: the cost of returning an empty collection
Dealing with empty lists is an everyday situation in .NET. An empty list is often preferred to a null in order to control the logic. But how …
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.
