Ulasan

Kita dapat membagi alat pengarsipan dan kompresi menjadi tiga kelas utama:
- Arsip dengan beberapa file biasanya dengan kompresi – ZIP, 7Z, XAR;
- Menyimpan beberapa file tanpa kompresi – TAR, CPIO;
- Kompresi -hanya format tanpa entri – GZ, LZ, BZIP2, XZ, ZStandard, Z.
Arsip dari kelas kedua sering digabungkan dengan format kompresi saja. Mereka populer dalam sistem mirip Unix dengan operasinya yang berbeda – satu untuk pengarsipan, satu lagi untuk kompresi. Berikut penjelasan dan contoh masing-masing jenis arsip.

Arsip terkompresi yang berisi banyak file

Kami menggunakan metode CreateEntry untuk setiap file yang akan dikompresi. Dalam contoh ini kami tidak menyediakan pengaturan kompresi sehingga akan menjadi default untuk format yang dipilih.

Cara ZIP beberapa file

Defaultnya adalah algoritma Deflate

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");	
    }

Cara membuat arsip 7-ZIP dari beberapa file

Default untuk 7-zip adalah algoritma LZMA

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");	
    }

Arsip XAR dengan beberapa file

Defaultnya adalah algoritma ZLib

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");	
    }

Menyimpan beberapa file tanpa kompresi

Pendekatan untuk format TAR dan CPIO serupa dan terletak pada metode CreateEntry yang sudah dikenal.

Arsip TAR tanpa kompresi

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");
    }

Arsip CPIO tanpa kompresi

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");
    }

Format hanya kompresi tanpa entri

Arsip seperti GZIP, LZ, BZIP2, XZ, Z tidak mendukung entri dan hanya dapat mengompresi satu sumber. Lihat sampel dengan kompresi seperti itu.

Arsip GZ Format khusus kompresi tanpa entri

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");
    }
}

Arsip LZ Format hanya kompresi tanpa entri

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 memungkinkan penggabungan format hanya arsip dan hanya kompresi dengan satu metode. Berikut adalah contoh fungsi ini dengan kombinasi TAR.GZ. Untuk mencapai hal ini, kami menggunakan metode SaveGzipped.

Kombinasi TAR.GZ

Ada metode serupa untuk format kompresi LZIP, XZ, ZStandard, Z.

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

Namun kita perlu menggunakan pendekatan lain untuk format BZIP2.

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: