Overview

We can divide archiving and compressing tools into three main classes:
- Archives with several files usually with a compression – ZIP, 7Z, XAR;
- Storing several files without compression – TAR, CPIO;
- Compression-only formats without entries – GZ, LZ, BZIP2, XZ, ZStandard, Z.
Archives from the second class are often combined with compression only formats. They are popular within Unix-like systems with their distinct operations – one for archiving, another for compression. Below are explanation and samples for every archive kind.

Compressed archives that contain multiple files

We use CreateEntry method for every file to be compressed. In these samples we do not supply compression settings so they would be default for chosen format.

How to ZIP several files

Default is Deflate algorithm

Zip compression with Deflate algoritm

    using (Archive archive = new Archive())
    {
        archive.CreateEntry("first.bin",  "data1.bin");
        archive.CreateEntry("second.bin",  "data2.bin");
        // Add as many entries as you need 
        archive.Save("archive.zip");	
    }

How to create 7-ZIP archives of several files

Default for 7-zip is LZMA algorithm

7-Zip with LZMA algorithm

    using (SevenZipArchive archive = new SevenZipArchive())
    {
        archive.CreateEntry("first.bin",  "data1.bin");
        archive.CreateEntry("second.bin",  "data2.bin");	
        // Add as many entries as you need 	
        archive.Save("archive.7z");	
    }

XAR archive with several files

Default is ZLib algorithm

XAR with ZLib algorithm

    using (XarArchive archive = new XarArchive())
    {
        archive.CreateEntry("first.bin",  "data1.bin");
        archive.CreateEntry("second.bin",  "data2.bin");
        // Add as many entries as you need 	
        archive.Save("archive.xar");	
    }

Storing several files without compression

Approach for TAR and CPIO formats is similar and lies in already known CreateEntry method.

TAR archive without compression

TAR storing files without compression

    using (TarArchive archive = new TarArchive())
    {
        archive.CreateEntry("first.bin",  "data1.bin");
        archive.CreateEntry("second.bin",  "data2.bin");
        // Add as many entries as you need 
        archive.Save("archive.tar");
    }

CPIO archive without compression

CPIO storing files without compression

    using (TarArchive archive = new TarArchive())
    {
        archive.CreateEntry("first.bin",  "data1.bin");
        archive.CreateEntry("second.bin",  "data2.bin");
        // Add as many entries as you need 
        archive.Save("archive.cpio");
    }

Compression-only formats without entries

Such archives like GZIP, LZ, BZIP2, XZ, Z do not support entries and can compress only single source. See samples with such a compression.

GZ archive Compression-only formats without entries

GZIP with compress only single source

using (FileStream source = File.Open("alice29.txt", FileMode.Open, FileAccess.Read))
{
    using (GzipArchive archive = new GzipArchive())
    {
        archive.SetSource(source);
        archive.Save("archive.gz");
    }
}

LZ archive Compression-only formats without entries

LZIP with compress only single source

using (FileStream source = File.Open("alice29.txt", FileMode.Open, FileAccess.Read))
{
    using (GzipArchive archive = new GzipArchive())
    {
        archive.SetSource(source);
        archive.Save("archive.gz");
    }
}

Aspose.ZIP allows combining archive-only and compression-only formats with a single method. Here is an example of this functionality with the TAR.GZ combination. To achieve this, we use the SaveGzipped method.

TAR.GZ combination

There are similar methods for LZIP, XZ, ZStandard, Z compressing formats.

TAR.GZ with SaveGzipped method

    using (TarArchive archive = new TarArchive())
    {
        archive.CreateEntry("first.bin",  "data1.bin");
        archive.CreateEntry("second.bin",  "data2.bin");
        archive.SaveGzipped("archive.tar.gz");
    }

TAR.BZ2

However we need to use another approach for BZIP2 format.

BZIP2 with SaveGzipped method

    using (TarArchive tar = new TarArchive())
    {
        tar.CreateEntry("first.bin",  "data1.bin");
        tar.CreateEntry("second.bin",  "data2.bin");	
        using (Bzip2Archive archive = new Bzip2Archive())
        {
            archive.SetSource(tar);
            archive.Save("archive.tar.bz2");
        } 
    }
  

Support and Learning Resources

  
  

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