SharpZipLib
| Language | C# / .NET (.NET Standard 2.0/2.1, .NET 6+) |
|---|---|
| License | MIT |
| Repository | github.com/icsharpcode/SharpZipLib |
| Documentation | wiki, API reference |
| Depends on | nothing |
| Read / write | yes / 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:
FastZip: zip or unzip a whole folder in one call, with filters and a password.ZipFile: random access to an existing archive, plus in-place updates (BeginUpdate/Add/Delete/CommitUpdate).ZipInputStream/ZipOutputStream: forward-only streams for reading and writing, entry by entry.
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#
ZipInputStreamreads archives from non-seekable streams, but entries written with a data descriptor and no sizes in the local header can’t always be read that way; preferZipFilewhen you have a file.ZipEntry.CleanName(path)makes a name suitable for an archive (forward slashes, no drive letter).- When extracting with
ZipFileyourself, checkentry.Namefor..before creating files;FastZip.ExtractZipalready rejects entries that escape the target folder.