Integrating Compression API in a .NET Application

The compression library integration is simple whereas most of the compress & decompress operations can be performed with just a few lines of code.

Single file compression - C#

  var dir = "Path/To/Directory/";
  using (var arch = new Archive())
  {
      arch.CreateEntry("filename.dat", dir + "file.dat");
      arch.Save(dir + "result.zip");
  }

This code creates a ZIP archive named "result.zip" and adds a single file to it. Here’s a description of what it does:

  • Directory Path : It defines a variable named dir and assigns a string containing the full directory path where the files reside.
  • Creating the Archive : The code utilizes a using construct, ensuring correct resource management by automatically releasing the archive object (arch) after the code within the block finishes executing.
  • Adding File to Archive : Inside the using block, the code creates an entry in the archive with the name "filename.dat". The CreateEntry method takes two arguments:
    - The first argument specifies the name for the entry within the archive, "filename.dat".
    - The second argument provides the path to the actual file that will be added ("dir + file.dat"). This path combines the directory path stored in dir with the filename "file.dat".
    Since compression method was not specified, the file will be compressed with the default Deflate algorithm.
  • Saving the Archive : Finally, the code calls the Save method on the archive object (arch). This composes the newly created ZIP archive with the added file to a file named "result.zip" within the same directory specified by dir.

  
 

Aspose.ZIP for .NET is a flexible documents compression and archive manipulation API for standard ZIP format. API enables .NET applications to achieve files compression/decompression, archiving files as well as folders and encryption of archives. This API works with all well known archive formats: ZIP, RAR, 7Zip, GZIP, BZ2. And with many archive formats from Linux: CPIO, TAR, Lzip, Bzip2, XZ, Z. It provides protection via user-defined passwords and traditional encryption using ZipCrypto or AES encryption such as AES128, 192 and AES256.

Aspose.Zip provides a .NET API for file compression and archiving. The library enables developers to compress, archive, and extract files within the .NET framework, ensuring efficient integration and performance.
The .NET platform has become a versatile foundation for applications ranging from web development to enterprise solutions. Aspose.Zip provides tools for file compression tasks, supporting efficient and reliable development within the .NET framework.

Advanced .NET Compression & Archiving API Features

Create an archive from one or more files

Add entries to existing archive

Compress folder respecting its structure

Decompress to directory respecting archive structure

Compress any size documents by applying parallelism

How to Extract RAR Using .NET

With Aspose.Zip for .NET, you can easily work with RAR archives, including extracting their contents to a directory. This process involves initializing the RarArchive class, loading the RAR file, and extracting its entries to a target directory.

Aspose.Zip for .NET provides a high-performance and reliable interface for working with various archive formats. The example below demonstrates how to extract a RAR archive into a specified directory.

Extract RAR Archives via .NET

using (RarArchive archive = new RarArchive("archive.rar"))
{
    archive.ExtractToDirectory("extracted");
}

The code snippet showcases the extraction of a RAR archive using the Aspose.Zip API for .NET. The script extracts all entries from the RAR archive into a specified directory, ensuring high performance and flexibility.

How to Convert Zip Using C#

With .NET, you can use third-party libraries, such as SevenZipArchive, to convert ZIP archives into the 7Z format. This process involves extracting data from a ZIP archive and creating corresponding entries in a new 7Z archive.

SevenZipArchive provides a simple and flexible interface for working with archive formats, including support for compression and structured data. The example below demonstrates how to convert a ZIP archive by extracting its entries into memory and adding them to a 7Z archive.

Convert from ZIP to 7Z via .NET

  using (SevenZipArchive sevenZipArchive =  new SevenZipArchive())
  {
    using (Archive archive = new Archive("source.zip"))
    {
      for (int i = 0; i < archive.Entries.Count; i++)
      {
        var ms = new MemoryStream();
        archive.Entries[i].Extract(ms);
        ms.Seek(0, SeekOrigin.Begin);
        sevenZipArchive.CreateEntry(archive.Entries[i].Name.Replace('\\', '/'), ms);
      }
    }

    sevenZipArchive.Save("output.7z");
  }

The code snippet showcases the conversion of a ZIP archive to a 7Z format via C#. Leveraging the SevenZipArchive class, the script extracts the ZIP entries and creates corresponding entries in the 7Z archive.

Other Supported Aspose.ZIP for .NET API Features

Use the Aspose.ZIP C# library to convert, merge, edit zip-file documents, extract data from the arhives, and more!

  

Support and Learning Resources

  
  

Aspose.Zip offers individual archive processing APIs for other popular development environments, listed below: