1. Products
  2.   Compression
  3.   .NET
  4.   SharpZipLib
 
  

C# .NET Library for Compression File Formats

Open Source Free.NET API for File Archiver Formats and Can Easily Handle Zip, GZip, Tar, BZip2 files with AES encryption, Zip64 support & more

What is SharpZipLib?

SharpZipLib (also known as #ziplib) is a comprehensive, open-source compression library written entirely in C# for the .NET platform. Originally created to provide pure C# GZip and ZIP compression for the SharpDevelop IDE, it has grown into a versatile assembly that supports a wide range of formats including Zip, GZip, Tar, and BZip2. As a managed assembly, it can be seamlessly incorporated into any .NET project regardless of the programming language used, offering features like PKZIP 2.0 and AES encryption for Zip files, GNU long filename support for Tar, and Zip64 for large archives. Its development was driven by the need for a fully native .NET solution, eliminating dependencies on external DLLs and giving developers complete control over compression and archiving processes within their applications.

The library's evolution continued due to high user demand, leading to the addition of BZip2 compression and Tar archiving capabilities. Under the permissive MIT License, SharpZipLib provides a powerful and flexible toolset for software developers. It features a well-organized namespace layout—with dedicated sections for BZip2, GZip, Tar, ZIP, Encryption, and Core utilities—making it easy to integrate and use for specific tasks. From creating encrypted archives and calculating checksums to handling compressed streams, SharpZipLib serves as an essential component for any .NET application requiring reliable data compression, archiving, and file management functionalities, all within a pure C# environment.

Previous Next

Getting Started with SharpZipLib

You need to have .NET Framework 4.5 or above in order to configure SharpZipLib. Once you have the met the prerequisites, you can manually download the repository from GitHub or directly fetch the assembly from the NuGet.

The recommended way to install SharpZipLib is from NuGet as it is available as a NuGet Package

Install SharpZipLib from NuGet

 Install-Package SharpZipLib

NET Library to Create & Extract ZIP Files

SharpZipLib enables software developers to create a ZIP file inside their own .NET applications. Developers can easily compress all the files available inside a directory and assign them into a single zip file. ZIP is one of the most popular compression file formats and gives users the facility to reduce the size of files. The API also supports extracting the content of ZIP files to a place of the user’s choice

How to Extract files from ZIP via C#?

// Open zip file
using (Stream fsInput = File.OpenRead("D:\\input.zip"))
using (var zf = new ZipFile(fsInput))
{
  // Set password if required
  zf.Password = "12345";
  // Unzip data
  foreach (ZipEntry zipEntry in zf)
  {
    if (!zipEntry.IsFile)
    {
      // Ignore directories
      continue;
    }
    String entryFileName = zipEntry.Name;
    var directoryName = "D:\\output\\test";
    if (directoryName.Length > 0)
    {
      Directory.CreateDirectory(directoryName);
    }
    var buffer = new byte[4096];
    using (var zipStream = zf.GetInputStream(zipEntry))
    using (Stream fsOutput = File.Create("data.zip"))
    {
      StreamUtils.Copy(zipStream, fsOutput, buffer);
    }
  }
}

Create a Password Protected ZIP File

SharpZipLib gives software developers the ability to compress all the files inside a folder into a ZIP file and define a password for the created files. If you want to create ZIP files without protection please set its value to null or don’t declare it to leave the file without any password protection. It supports a large number of files.

How to Create a password protected ZIP file via C#?

// Create a new ZIP file 
using (FileStream fsOut = File.Create("D:\\output.zip"))
using (var zipStream = new ZipOutputStream(fsOut))
{
  //0-9, 9 being the highest level of compression
  zipStream.SetLevel(3);
  // Set password
  zipStream.Password = "12345";
  // Add files
  var files = Directory.GetFiles("D:\\sample");
  foreach (var filename in files)
  {
    var fi = new FileInfo(filename);
    // Make the name in zip based on the folder
    var entryName = filename.Substring(1);
    // Remove drive from name and fixe slash direction
    entryName = ZipEntry.CleanName(entryName);
    var newEntry = new ZipEntry(entryName);
    // Note the zip format stores 2 second granularity
    newEntry.DateTime = fi.LastWriteTime;
    newEntry.Size = fi.Length;
    zipStream.PutNextEntry(newEntry);
    var buffer = new byte[4096];
    using (FileStream fsInput = File.OpenRead(filename))
    {
      StreamUtils.Copy(fsInput, zipStream, buffer);
    }
    zipStream.CloseEntry();
  }

}
 English