C# .NET Library for Compression File Formats

Open Source Free .NET API for file archiver formats like ZIP, GZIP, TAR & BZIP2.

 

SharpZipLib library is written entirely in C# for the .NET platform. It is implemented as an assembly and so you can incorporate it into other projects (in any .NET language).

It was originally ported from the GNU Classpath java.util.zip library so it could be used with SharpDevelop, which needed GZIP & ZIP compression. Later due to high user's demand, the BZIP2 and Tar archiving were also included in the library.

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 format and gives users the facility to reduce the size of files.  The API also supports extracting content of ZIP file to a place of user’s choice

Extract files from ZIP - 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 large number of files.

Create a password protected ZIP file - 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();
    }

}