Overview

Archives that consist of entries have a createEntries method for compressing entire folders. These methods preserve the directory structure and recursively include all subfolders. Additionally, you can add individual file entries to any subfolder within the archive.

How to Compress ZIP Folder

To compress a folder into a ZIP archive, you can use the following Python code snippet. By using the zp.Archive class, you can create entries for the entire folder while preserving its structure. Simply call the create_entries method with the folder path, then save the archive with the save method, as shown below:

    with zp.Archive() as archive:
        archive.create_entries("D:\\Data")
        archive.save("archive.zip")

Sampe 2:

    with zp.Archive() as archive:
        archive.create_entry("data\\pictures\\photo.bmp", "image.bmp")
        archive.create_entry("data\\text\\song.txt", "lyrics.txt")
        archive.save("archive.zip")

These codes are used to create archives from files and folders. The first code creates an archive that recursively adds all the files and subdirectories from the "D:\Data" folder, preserving their structure, and then saves the archive as "archive.zip". The second code creates an archive to which individual files are added: the file "data\pictures\photo.bmp" is added to the archive with the name "image.bmp", and the file "data\text\song.txt" is added with the name "lyrics.txt". In both cases, the archive is saved as "archive.zip". These methods allow you to add both entire directories and individual files to the ZIP archive with the ability to change their names inside the archive, providing flexibility in data organization.

Compress folder in 7ZIP

For the 7Z format, the approach is similar to the previous one, but it uses the SevenZipArchive class for handling 7z compression

    with zp.sevenzip.SevenZipArchive() as archive:
        archive.create_entries("D:\\Data")
        archive.save("archive.7z")

Compress files in TAR

You can use TarArchive instead of zip archive class. The usage is completely similar to the previous examples:

    with zp.tar.TarArchive() as archive:
        archive.create_entries("D:\\Data")
        archive.save("archive.tar")

Sampe 2:

    with zp.tar.TarArchive() as archive:
        archive.create_entry("data\\pictures\\photo.bmp", "image.bmp")
        archive.create_entry("data\\text\\song.txt", "lyrics.txt")
        archive.save("archive.tar")

XAR

The XarArchive operates similarly to the examples described earlier, allowing the creation of XAR archives with options to add files or directories while preserving their structure.

    with open("archive.xar", "xb") as xar_file:
        with zp.xar.XarArchive() as archive:
            archive.create_entries("C:\\Folder", false)
            archive.save(xar_file)

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: