Create ZIP in Java

This quick guide explains how to create a ZIP file in Java. It encompasses all the information including the program flow and a runnable sample code to develop a ZIP file maker in Java. Moreover, you can follow this tutorial and run the code snippet in Windows, MacOS, Linux, etc., where JDK is configured.

Steps to Create ZIP in Java

  1. Install Aspose.ZIP using the NuGet package manager to create a ZIP archive
  2. Initiate an instance of the Archive class
  3. Get the input files and add them to the ZIP directory using the createEntry method
  4. Export the output ZIP directory to the disk

These steps outline the program flow to create ZIP in Java. They elaborate on setting up the environment and compressing the files into the ZIP archive. Finally, the generated ZIP directory is exported to conclude the process.

Code to Create ZIP File Maker in Java

// Create an instance of the Archive class
try (com.aspose.zip.Archive archive = new com.aspose.zip.Archive())
{
// Create the entries
archive.createEntry("data.png", "input.png");
archive.createEntry("test.jpg", "sample.jpg");
// Call save method
create-zip-in-javaarchive.save("output.zip");
}

This sample code showcases how to make a ZIP file in Java. However, you can further customize this process to compress single or multiple files into the archive with the createEntry method. Furthermore, you can also set custom options like encrypting or password-protecting the output directory, setting self-extraction, etc., as per your requirements.

This article has covered the details of creating a ZIP maker in Java. Besides, if you want to understand CSV file compression, then take a look at Compress CSV in Java.

 English