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

Open Source .NET API for Compression File Formats

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

What is SharpCompress?

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 a zip archive from all files in a directory to a file, extracting all files from a RAR file to a directory, Using ReaderFactory to autodetect archive type and Open the entry stream, Using ReaderFactory to autodetect archive type and Open the entry stream and many more

Previous Next

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 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("\\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 existing ZIP file. First, you need to select the file you want to append to an existing 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. You can add an existing file to a zip archive using three simple steps

Add file to existing ZIP file

  1. Open existing ZIP file using ZipArchive.Open() method and pass file name as parameter
  2. Add files in ZIP using AddAllFromDirectory() method and pass directory path as attachments
  3. Save file using SaveTo() method and pass output path as first argument and CompressionType as second argument

Add files to existing ZIP Archive - C#

// open existing ZIP file
ZipArchive archive = ZipArchive.Open("test.zip");
// add samples files in it
archive.AddAllFromDirectory("\\sample");
// save file
archive.SaveTo("sample.zip", CompressionType.Deflate);
    
 English