All formats

SharpZipLib

LanguageC# / .NET (.NET Standard 2.0/2.1, .NET 6+)
LicenseMIT
Repositorygithub.com/icsharpcode/SharpZipLib
Documentationwiki, API reference
Depends onnothing
Read / writeyes / yes, ZIP64, ZipCrypto and AES-128/256 (read and write), stored and DEFLATE

SharpZipLib (ICSharpCode.SharpZipLib) is one of the oldest .NET compression libraries. For ZIP it has:

Its main advantage over the built-in System.IO.Compression is encryption: it reads and writes both ZipCrypto and WinZip AES. Releases are infrequent (the last release is 1.4.2).

Installation#

Shell
dotnet add package SharpZipLib

Basic usage: zip and unzip a folder#

C#
using ICSharpCode.SharpZipLib.Zip;

var fastZip = new FastZip();
fastZip.CreateZip("photos.zip", "photos", recurse: true, fileFilter: null);

fastZip.ExtractZip("photos.zip", "out", fileFilter: null);

fileFilter is a regular-expression filter like @"\.jpg$"; null means all files.

How to create a password-protected ZIP with AES#

C#
using ICSharpCode.SharpZipLib.Zip;

var fastZip = new FastZip
{
    Password = "my password",
    EntryEncryptionMethod = ZipEncryptionMethod.AES256,  // default is ZipCrypto
};
fastZip.CreateZip("secret.zip", "docs", recurse: true, fileFilter: null);

// extracting needs the same password
var unzip = new FastZip { Password = "my password" };
unzip.ExtractZip("secret.zip", "out", fileFilter: null);

How to write a ZIP entry by entry#

C#
using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;

using var zipStream = new ZipOutputStream(File.Create("out.zip"));
zipStream.SetLevel(9);  // 0 (store) .. 9 (best)

var entry = new ZipEntry("hello.txt") { DateTime = DateTime.Now };
zipStream.PutNextEntry(entry);
byte[] data = Encoding.UTF8.GetBytes("hello from SharpZipLib\n");
zipStream.Write(data, 0, data.Length);
zipStream.CloseEntry();
// disposing the stream writes the central directory

Set zipStream.Password and entry.AESKeySize = 256 before PutNextEntry to encrypt an entry with AES.

How to list and read files#

C#
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

using var zip = new ZipFile("in.zip");
// zip.Password = "my password";  // for encrypted archives
foreach (ZipEntry entry in zip)
{
    if (!entry.IsFile) continue;
    Console.WriteLine($"{entry.Name} {entry.Size} bytes, {entry.CompressionMethod}");
}

ZipEntry? readme = zip.GetEntry("readme.txt");
if (readme != null)
{
    using Stream s = zip.GetInputStream(readme);
    using var reader = new StreamReader(s);
    Console.WriteLine(reader.ReadToEnd());
}

Tips#