Working with ZIP Archives in C# (.NET)
In modern .NET development, working with archives is one of the most common tasks. Whether it's packaging logs, creating backups, or transferring data over a network — the ability to efficiently compress and decompress files is essential for every C# developer. In this article, we will thoroughly examine two main approaches: the built-in System.IO.Compression namespace and the third-party library SharpZipLib.
Built-in .NET Tools: System.IO.Compression
Starting with .NET Framework 4.5, the platform provides a native API for working with ZIP archives. The main classes are located in the System.IO.Compression namespace.
Creating a ZIP Archive
To create an archive, use the ZipArchive class together with ZipFile:
using System.IO;using System.IO.Compression;
string sourceDirectory = @"C:\\Data\\Reports";string zipPath = @"C:\\Archives\\reports.zip";
if (File.Exists(zipPath)) File.Delete(zipPath);
ZipFile.CreateFromDirectory(sourceDirectory, zipPath);Console.WriteLine("Archive created: " + zipPath);Extracting an Archive
Extraction is done similarly using the ExtractToDirectory method:
string extractPath = @"C:\\Data\\Extracted";ZipFile.ExtractToDirectory(zipPath, extractPath);Console.WriteLine("Archive extracted to: " + extractPath);Working with Individual Entries
For finer control, use ZipArchive directly:
using (FileStream zipStream = new FileStream(zipPath, FileMode.Open))using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Read)){ foreach (ZipArchiveEntry entry in archive.Entries) { Console.WriteLine($"File: {entry.FullName}, Size: {entry.Length} bytes"); // Extracting a specific file entry.ExtractToFile(Path.Combine(extractPath, entry.FullName), true); }}SharpZipLib Library
SharpZipLib is a powerful third-party open-source library that provides advanced capabilities compared to built-in tools. It supports more formats (GZip, BZip2, Tar) and offers more flexible control over the compression process.
Installing SharpZipLib
Install the library via NuGet Package Manager:
Install-Package SharpZipLibOr via .NET CLI:
dotnet add package SharpZipLibCreating an Archive with SharpZipLib
using ICSharpCode.SharpZipLib.Zip;using ICSharpCode.SharpZipLib.Core;
string outputZip = @"C:\\Archives\\backup.zip";string[] filesToZip = Directory.GetFiles(@"C:\\Data\\Files", "*.*", SearchOption.AllDirectories);
using (FileStream fsOut = File.Create(outputZip))using (ZipOutputStream zipStream = new ZipOutputStream(fsOut)){ zipStream.SetLevel(9); // Maximum compression (0-9)
foreach (string filename in filesToZip) { FileInfo fi = new FileInfo(filename); string entryName = ZipEntry.CleanName(fi.Name); ZipEntry newEntry = new ZipEntry(entryName); newEntry.DateTime = fi.LastWriteTime; newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096]; using (FileStream fsInput = File.OpenRead(filename)) { StreamUtils.Copy(fsInput, zipStream, buffer); } zipStream.CloseEntry(); }}Extracting an Archive with SharpZipLib
string inputZip = @"C:\\Archives\\backup.zip";string outputDir = @"C:\\Data\\Restored";
using (FileStream fsIn = File.OpenRead(inputZip))using (ZipInputStream zipStream = new ZipInputStream(fsIn)){ ZipEntry entry; while ((entry = zipStream.GetNextEntry()) != Also in library
Cryptography in Kotlin: Encryption and Hashing with Examples
Database Migrations in Rust: Overview of Diesel, SeaORM, and sqlx Tools
Sending SMS in PHP: API libraries of sms.ru and other services
Data Validation in Kotlin: Libraries and Approaches (Kotlin, Java)