Overwiew

Archives that consist of entries have createEntries method for compressing whole folder. Such methods respect directory structure and compose recurrently for all subfolders. Also you can add a single file entry to arbitrary subfolder wherever to the archive.

How to make a ZIP archive

The createEntry 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.

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

This code makes a ZIP archive named "archive.zip" containing two files: "data1.bin" and "data2.bin". Here’s a breakdown of how it works:

  • Automatic Resource Management: The code utilizes a try-with-resources block to ensure proper management of resources. This block automatically closes the archive object upon completion.
  • Adding Files to Archive: Inside the block, the code employs the createEntry method twice to add files to the archive:
    - The first entry is named "first.bin" and contains the contents of the file "data1.bin".
    - The second entry is named "second.bin" and contains the contents of the file "data2.bin".
  • Saving the Archive: Finally, the save method is called on the archive object to save the newly created ZIP archive with the specified entries to "archive.zip".

How to create 7ZIP

To add multiple attachments to a single 7zip archive, we employ the SevenZipArchive class along with the createEntry and save methods in a similar manner.

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

The following code performs the same actions as the previous one, but for the 7-ZIP format.

TAR make archive

Expanding archives often involves extracting, adding new files, and recompressing, which is a resource-intensive process. Aspose.ZIP offers a smarter solution in Java. Simply open the archive for extraction and add new entries directly, avoiding unnecessary repacking. This efficient approach works for the TAR archive format.

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

This application shares the functionality of the previous one, but it’s tailored for archives with the TAR extension.

How to create GZ File

Compression-Only Formats without Entries. These compression formats, including GZIP, LZ, BZIP2, XZ, and Z, do not support file entries. They can only compress a single source file at a time.

    try (FileInputStream source = new FileInputStream("alice29.txt")) {
        try (GzipArchive archive = new GzipArchive()) {
            archive.setSource(source);
            archive.save("archive.gz");
        }
    } catch (IOException ex) {
        System.err.println(ex.getMessage());
    }

Create TAR GZ

Aspose.ZIP offers a convenient way to combine archive-only and compression-only formats using a single method. Here’s an example of this functionality with the TAR.GZ combination. This method utilizes the saveGzipped method.

    try (TarArchive archive = new TarArchive()) {
        archive.createEntry("first.bin", "data1.bin");
        archive.createEntry("second.bin", "data2.bin");
        archive.saveGzipped("archive.tar.gz");
    }

Create TAR BZ2 File

Aspose.ZIP offers similar methods for compressing archives with LZIP, XZ, ZStandard, and Z formats. However, a different approach is required for BZIP2.

    try (TarArchive tar = new TarArchive()) {
        tar.createEntry("first.bin", "data1.bin");
        tar.createEntry("second.bin", "data2.bin");
        try (Bzip2Archive archive = new Bzip2Archive()) {
            archive.setSource(tar);
            archive.save("archive.tar.bz2");
        }
    }

This code construction combines the use of two different archive formats, namely TAR and Bzip2. The main feature is that a TAR archive is first created using the TarArchive class by adding several files to it using the createEntry method, and then this archive is compressed into Bzip2 format using the Bzip2Archive class .

Other Supported Aspose.ZIP for Java API Features

With the Aspose.ZIP library in Java, 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: