Extract & Decompress Files from a ZIP Archive in Java
Learn how to extract files and folders from a ZIP archive using a free API in Java.
When we share files in bulk across a network, it’s standard practice to first compress those files in a ZIP archive. Handling the compression process programmatically saves time, and we learned how to do that in another tutorial on this blog.
In this follow-up tutorial, we’ll learn how to perform the inverse operation. We’ll call an API that extracts and decompresses files and folders from a ZIP archive, returning the archive’s contents in a structured and easily readable/usable format. We can refer to the following example JSON response model to get a better idea of how our unzipped archive contents are returned:
{
"Successful": true,
"FilesInZip": [
{
"FileName": "string",
"FileContents": "string"
}
],
"DirectoriesInZip": [
{
"DirectoryName": "string",
"DirectoriesInDirectory": [
null
],
"FilesInDirectory": [
{
"FileName": "string",
"FileContents": "string"
}
]
}
]
}Step 1: Install the SDK
We’ll begin our tutorial by installing the SDK. We can install with either Maven or Gradle - I’ve included examples for both options in this tutorial.
To install with Maven, let’s first add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>After that, let’s 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>To install with Gradle, let’s add the below code to the root.build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}After that, let’s add a dependency in the build.gradle:
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v4.25'
}Step 2: Add the Imports
We’ll now add the imports to the top of our file. Let’s copy those from the following snippet:
// 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: Configuration
Let’s now add the following configuration snippet to our file:
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");We’ll authorize our API calls by supplying a Cloudmersive API key in this snippet. If we don’t have a Cloudmersive API key yet, we can get one for free by visiting the Cloudmersive website and creating a free account. Free API keys give us a limit of up to 800 API calls per month with no commitments (our call limit will reset each month, and we can scale up our plan whenever the need arises).
Step 4: Create an Instance of the API & Call the Function
To wrap up this process, we’ll add the below snippet to our file. This creates an instance of the API and calls the ZIP extraction and decompression function:
ZipArchiveApi apiInstance = new ZipArchiveApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
ZipExtractResponse result = apiInstance.zipArchiveZipExtract(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ZipArchiveApi#zipArchiveZipExtract");
e.printStackTrace();
}We’ll load the ZIP archive for this operation by supplying the file path via the File inputFile variable.
That’s all there is to it! Now we can easily zip and un-zip archives using just a few lines of Java code.
Interested in more Cloudmersive API tutorials? Don’t forget to subscribe for more useful content (with copy-and-paste code examples) each week!


