Dealing with Zip Files in C#

December 07, 2023

In this post, I’ll cover some of the things that you can do with the System.IO.Compression namespace. We’ll discuss how you can use this namespace to manipulate zip files.


Let’s start with simply listing the contents of a zip file:

using ZipArchive archive = ZipFile.OpenRead(zipFilePath);

foreach (ZipArchiveEntry entry in archive.Entries)

Let’s build a new zip file from the entire contents of a directory:

ZipFile.CreateFromDirectory(sourceFolderPath, zipFilePath);    

You can also use this to extract from a zip file - for example, we can extract the first file:

using ZipArchive archive = ZipFile.OpenRead(zipFilePath);

var first = archive
    .ExtractToFile(Path.Combine(extractPath, first.FullName), true);


You can also extract files from the archive in parallel. For example:

using ZipArchive archive = ZipFile.OpenRead(zipFilePath);        

Parallel.ForEach(archive.Entries, entry =>
    string entryPath = Path.Combine(extractPath, entry.FullName);
    entry.ExtractToFile(entryPath, true);    

Profile picture

A blog about one man's journey through code… and some pictures of the Peak District

© Paul Michaels 2024