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 Zip a folder

If you want to create a ZIP archive from an entire folder while preserving the structure, you can use the Aspose.Zip API for Java and the following code. The try-with-resources construct automatically closes the Archive resource after all code has been executed, regardless of whether all files are closed properly. The path specified in the File corpus object indicates the folder that will be compressed. With createEntries, each element of the archive is added separately, thus preserving the original structure, which is stored in a file called archive.zip. It is important to note that the code does not check for the existence of the "D:/Data" folder or the presence of files in it. If the directory does not exist or is empty, the code will not create any archive.
CreateEntries method has optional second parameter includeRootDirectory which indicates if the root directory needs to be included to the archive. Default is true, so in the above sample all of archive entries will be located inside "Data" folder. If you want only directory content to be added then pass false there.
Sample - Create archive from folder:

    try (Archive archive = new Archive()) {
        File corpus = new File("D:\\Data");
        archive.createEntries(corpus);
        archive.save("archive.zip");
    }

The following code example creates a ZIP archive with two files: "image.bmp" and "lyrics.txt". The fi1 and fi2 variables represent these files. The createEntry method of the archive object is used to add each file to the archive with the specified paths: "data/pictures" for "image.bmp" and "data/text" for "lyrics.txt". In summary, this code creates a ZIP archive named "archive.zip" containing two folders, "data/pictures" and "data/text", each holding one of the respective files.

    try (Archive archive = new Archive()) {
        File fi1 = new File("image.bmp");
        File fi2 = new File("lyrics.txt");
        archive.createEntry("data\\pictures\\photo.bmp", fi1);
        archive.createEntry("data\\text\\song.txt", fi2);
        archive.save("archive.zip");
    }

How to Compress Folder in 7-ZIP Respecting its Structure

The following code demonstrates how to create a 7Z archive and add all the files and folders from the specified directory to it. It works similarly to an archive with the ZIP file format. CreateEntries is the analogue method of 7Z archive. It also has includeRootDirectory parameter. Important: will be implemented in Aspose.ZIP for Java 24.4

    try (SevenZipArchive archive = new SevenZipArchive()) {
        File corpus = new File("D:\\Data");
        archive.createEntries(corpus);
        archive.save("archive.7z");
    }

How to Compress a Folder in TAR While Keeping Its Structure

The code creates a TAR archive for the specified directory. It utilizes the TarArchive class from the Java Archive API to achieve this. The createEntries method adds all files and folders from the directory to the archive, preserving the directory structure. Finally, the save method writes the archive’s contents to the file "archive.tar".
Sample:

    try (TarArchive archive = new TarArchive()) {
        archive.createEntries("D:\\Data");
        archive.save("archive.tar");
    }

The provided code creates a TAR archive named "archive.tar" containing two entries. Directory Archive - It adds all files and folders from the directory "D:/Data" to the archive, preserving the directory structure. This effectively archives the entire contents of the specified directory. And Single File Entry - It adds a specific file named "data.bin" from the "inner" subfolder to the archive. This allows for including a particular file without archiving the entire directory structure. In summary, this code creates a TAR archive that combines both directory-level archiving and individual file inclusion.
Sample 2 – Combine archiving directory and single file

    try (TarArchive archive = new TarArchive()) {
        archive.createEntries("D:\\Data");
        archive.createEntry("inner\\data.bin", "data.bin");
        archive.save("archive.tar");
    }

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: