Compress several entries into single archive

Compress several entries into single archive create_entry method should be used for each file to be compressed. We’ll use default compression settings for each format below.

How to ZIP several files using Python.Net

The create_entry method is used to add each file to the compressed archive. In these examples, we don’t specify any compression settings, so the default settings for the chosen format will be applied, which is the Deflate algorithm.

    with zp.Archive() as archive:
        archive.create_entry("first.bin", "data1.bin")
        archive.create_entry("second.bin", "data2.bin")
        # Add as many entries as you need 
        archive.save("archive.zip")

How to add files to a 7z archive

To add multiple attachments to a single 7zip archive, need to use the SevenZipArchive class along with the create_entry and save methods. Default is LZMA algorithm.

    with zp.sevenzip.SevenZipArchive() as archive:
        archive.create_entry("first.bin", "data1.bin")
        archive.create_entry("second.bin", "data2.bin")
        # Add as many entries as you need 	
        archive.save("archive.7z")

Create XAR file

To create XAR file you need to use ZLib algorithm.

    with zp.xar.XarArchive() as archive:
        archive.create_entry("first.bin", "data1.bin")
        archive.create_entry("second.bin", "data2.bin")
        # Add as many entries as you need 	
        archive.save("archive.xar")

This and the previous examples create Zip, 7Zip and XAR archives, respectively. The code example opens a context manager to create an archive in the appropriate format and adds files to the archive with the specified names. After all the files have been added to the archive, it is saved with the name specified in the archive.save method.

Storing several files without compression

Creating a TAR archive with file or files appended

Same approach we can use with TAR and CPIO formats:

    with zp.tar.TarArchive() as archive:
        archive.create_entry("first.bin", "data1.bin")
        archive.create_entry("second.bin", "data2.bin")
        # Add as many entries as you need 
        archive.save("archive.tar")

Appending files to a CPIO archive

You can use CpioArchive instead of TarArchive. The usage is completely similar to the previous example:

    with zp.cpio.CpioArchive() as archive:
        archive.create_entry("first.bin", "data1.bin")
        archive.create_entry("second.bin", "data2.bin")
        # Add as many entries as you need 
        archive.save("archive.cpio")

The provided code examples demonstrate how, by using the aspose.zip library, you can easily and efficiently store any number of files in archive. This approach ensures the speed of operations and increases the convenience of working with files. Thanks to this, you will be able to quickly create archives in TAR, and CPIO formats.

Compression-only formats without entries

How to add a file to a .GZ archive

With several compression formats we can not use several entries, so we should use the single-source approach, as described below:

    with io.FileIO("alice29.txt") as source:
        with zp.gzip.GzipArchive() as archive:
            archive.set_source(source)
            archive.save("archive.gz")

The provided code examples demonstrate how, by using the aspose.zip library, you can easily and efficiently store any number of files in an uncompressed archive. This approach ensures the speed of operations and increases the convenience of working with files. Thanks to this, you will be able to quickly create archives in ZIP, 7Z, TAR, XAR, and CPIO formats, which is useful for organizing and transferring files in various situations.

Compress files to LZ

    with io.FileIO("alice29.txt") as source:
        with zp.lzip.LzipArchive() as archive:
            archive.set_source(source)
            archive.save("archive.lz")

In both examples, the code opens the alice29.txt file for reading, creates an archive in the appropriate format, adds the file to the archive, and saves the archive with the appropriate file extension.

TAR.GZ pack

We can use save_gzipped method to combine archive only and compression only formats as well. See example for tar.gz below:

    with zp.tar.TarArchive() as archive:
        archive.create_entry("first.bin", "data1.bin")
        archive.create_entry("second.bin", "data2.bin")
        # Add as many entries as you need 
        archive.save_gzipped("archive.tar.gz")

This code demonstrates creating a compressed TAR archive named archive.tar.gz. It utilizes a context manager to securely create the archive and add two files, data1.bin and data2.bin, renaming them to first.bin and second.bin within the archive. This approach simplifies the creation of compressed TAR archives while maintaining the original file structure and content.

TAR create BZ2 archive

There is another way to combine formats – we can use different compression algorithms sequentially. In the example below we’re using TAR first, then BZIP2:

    with zp.tar.TarArchive() as tar:
        tar.create_entry("first.bin", "data1.bin")
        tar.create_entry("second.bin", "data2.bin")
        with zp.bzip2.Bzip2Archive() as archive:
            archive.set_source(tar)
            archive.save("archive.tar.bz2")

This code achieves the same functionality, but with a different approach. It utilizes the tar.create_entry method to construct entries within the archive. Consequently, the archive is written in the tar.bz2 format using the zp.bzip2.Bzip2Archive method.

Other Supported Aspose.ZIP for Python.Net API Features

With the Aspose.ZIP library in Python.Net, you can seamlessly handle various tasks related to ZIP file documents. Whether you need to convert formats, merge files, edit contents, or extract data, Aspose.ZIP provides comprehensive functionality to streamline your workflow and enhance productivity.

  

Support and Learning Resources

  
  

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