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 Add File to existing ZIP archive

There are situations where you need to expand an existing archive with additional files. The typical approach involves extracting the archive, adding the new files to the extracted content, and then recompressing everything together. However, this is not an optimal solution as it wastes computational resources on already compressed files.
Aspose.ZIP offers a more efficient way to achieve this by allowing you to add entries directly to a ZIP archive without the need for full extraction and repacking. This feature is applicable to both ZIP and TAR archive formats, making it a versatile solution for working with compressed files using Java.

    try (Archive archive = new Archive("existing.zip")) 
    {
        archive.createEntry("one_more.bin", "data.bin");
        archive.save("added.zip");
    }

This code snippet adds a new entry named "one_more.bin" to an existing ZIP archive named "existing.zip". The data for the new entry comes from the source or file specified by "data.bin". The entire process is wrapped within a try-with-resources block for proper resource management. Here’s a breakdown of what it does:

  • Opening Existing Archive: It establishes a try-with-resources block, which ensures proper resource management (closing the archive object automatically after the block finishes). Inside the block, it creates an Archive object using the constructor new Archive("existing.zip").
  • Creating New Entry: The code utilizes the createEntry method on the archive object. This method takes two arguments:
    - "one_more.bin" This specifies the name that will be assigned to the new entry within the archive.
    - "data.bin" This is file with the path or source of the data that will be added to the newly created entry.
  • Saving Modified Archive: Finally, the code calls the save method on the archive object. This method saves the modified archive to a new file named "added.zip".

TAR Add File to Existing 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("existing.tar")) {
        archive.createEntry("one_more.bin", "data.bin");
        archive.save("added.tar");
    }

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

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: