Open Source .NET API for Compression File Formats

Open Source .NET library for working with popular compression file formats.

 

SharpCompress is an open source pure .NET library that enables software developers to work with popular compression file formats like RAR, 7ZAP, ZIP, TAR, GZIP, BZIP2 & more. It provides the capability for decompressing 7ZIP, ZIP/unzip, TAR/untar LZIP/unlzip, BZIP2/unbzip2, and GZIP/ungzip with forward-only reading and files random access APIs. It has also implemented write support for ZIP, TAR, BZIP2, and GZIP file format. 

SharpCompress Several important features such as creating zip archive from all files in a directory to a file, extract all files from a RAR file to a directory, Use ReaderFactory to autodetect archive type and Open the entry stream, Use ReaderFactory to autodetect archive type and Open the entry stream and many more 

Getting Started with SharpCompress

To install SharpCompress, you need to have .NET Framework 3.5 or above. You can manually download the repository from GitHub. Or you can use NuGet.

Here is the command

 Install-Package sharpcompress -Version number  

.NET Library to Compress and Extract Files from a ZIP File

SharpCompress enables .NET developers to create a ZIP file by compressing the contents of a folder into a new ZIP file. The ZIP file format is one of the most widely used compression and archiving file formats. It helps to reduce the size of one or more files and also you can archive multiple files and folders into a single file. The API also enables developers to extract files from a ZIP file. You just need to point of out which file you want to extract, and it will cycle through every file in the archive to save it to a directory

Extract all File from RAR - C#

// Read RAR file
RarArchive rarArchive = RarArchive.Open("fileformat.rar");
// Extract all data
foreach (var entry in rarArchive.Entries.Where(entry => !entry.IsDirectory))
{
    entry.WriteToDirectory("D:\\filformat", new ExtractionOptions()
    {
        ExtractFullPath = true,
        Overwrite = true
    });
}

Add a File to an Existing ZIP File using .NET

SharpCompress Library provides users the capability to add files to an existence ZIP file. First, you need to select the file you want to append to an existence ZIP file.  SharpCompress will first save it to a temporary file and once you have done can move the temporary file to the permanent location. In this way, the new file gets compressed when you add it to the existing ZIP.

Add files to existing ZIP Archive - C#

// Open existing ZIP file
ZipArchive archive = ZipArchive.Open("D:\\test.zip");
// Add samples files in it
archive.AddAllFromDirectory("D:\\sample");
// Save file
archive.SaveTo("D:\\sample.zip", CompressionType.Deflate);