How to Compress Files to Create a New Zip Archive in Java
Learn how to automate zip file compression using Java code examples.
Moving big, bulky files around tends to slow things down – and that gets considerably worse when we’re dealing with a large volume of such files at once. Storage and bandwidth are precious commodities; if we don’t conserve them properly, our application performance can suffer directly as a result.
Of course, there’s one obvious solution to a big-file problem: zip archive compression.
Compressing our bulky files into manageable zip archives saves space, allowing for much more efficient file storage and file transfer. The only question is, how do we programmatically handle zip compression in a consistent, reliable way?
You guessed it - there’s an API for that!
In this tutorial, we’ll learn how to call a zip compression API using complementary Java code examples. With this solution under our belt, we’ll be able to easily automate zip archive compression and reduce the burden large files place on our applications.
Let’s get started!
Step 1: Installing the Client SDK
Installing the client SDK is our first order of business. In this tutorial, we’ll install with Maven.
Let’s add the following reference to the repository in our Maven POM file (Jitpack is used to dynamically compile the library):
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
After that, let’s go ahead and add a reference to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Step 2: Adding the Imports
Now it’s time to add the imports to our controller. Let’s copy and paste those now:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ZipArchiveApi;
Step 3: Configuring our API Key
If we’ve used Cloudmersive APIs in our projects before, we’ll know what comes next: it’s time to copy an API key from our Cloudmersive account and paste that in the authorization code snippet:
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
If we’re new to using Cloudmersive APIs, we can get a free API key by signing up for a free account on the Cloudmersive website. That will give us a limit of 800 API calls per month with zero commitments (once we reach our limit, our total will simply reset the following month).
Step 4: Creating an Instance of the API
At this point, we’re ready to create an instance of the zip compression API and load our files:
ZipArchiveApi apiInstance = new ZipArchiveApi();
File inputFile1 = new File("/path/to/inputfile"); // File | First input file to perform the operation on.
File inputFile2 = new File("/path/to/inputfile"); // File | Second input file to perform the operation on.
File inputFile3 = new File("/path/to/inputfile"); // File | Third input file to perform the operation on.
File inputFile4 = new File("/path/to/inputfile"); // File | Fourth input file to perform the operation on.
File inputFile5 = new File("/path/to/inputfile"); // File | Fifth input file to perform the operation on.
File inputFile6 = new File("/path/to/inputfile"); // File | Sixth input file to perform the operation on.
File inputFile7 = new File("/path/to/inputfile"); // File | Seventh input file to perform the operation on.
File inputFile8 = new File("/path/to/inputfile"); // File | Eighth input file to perform the operation on.
File inputFile9 = new File("/path/to/inputfile"); // File | Ninth input file to perform the operation on.
File inputFile10 = new File("/path/to/inputfile"); // File | Tenth input file to perform the operation on.
try {
byte[] result = apiInstance.zipArchiveZipCreate(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ZipArchiveApi#zipArchiveZipCreate");
e.printStackTrace();
}
The API response will return our zip archive file bytes in a string, and we can simply write that content to a new zip file.
All done! Now we can easily automate zip compression in our Java applications with a low-code API solution.
If you have any questions following this tutorial, please feel free to reach out to a member of our team. As always, don’t forget to hit “subscribe” before you move on to your next read!