How to Extract, Decompress Files and Folders from a Zip Archive Automatically with Java
Decompressing Zip files can be done in a wide variety of different ways. If you’re looking to include this operation as a service in your application, we have an API for you. Our Zip Decompression API will quickly extract a zip archive by decompressing its files and folders, leaving you with the component files to take additional actions on. To take advantage of this API, follow instructions below to structure your API call in Java.
To start, let’s install Maven. Include a reference in the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then include one in the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Next, let’s add the imports to the top of the file and include the decompression function:
// 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;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");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();
}
No further code required. Ensure the following parameters are satisfied, and you’re all set:
- Your input Zip archive
- Your Cloudmersive API key (obtainable by registering a free account on our website, www.cloudmersive.com)