How to Validate a ZIP Archive using Java

Cloudmersive
2 min readJun 24, 2022

--

Looking to clean up your data? It’s easy to let invalid file types into your database without an adequate data validation service to check them first. When it comes to ZIP files especially, you can never be too careful — you never know what dangerous files might be lurking within the archive, and you probably don’t want to find out by opening them. By incorporating our ZIP Archive Validation API, you can ensure each file within a ZIP archive is valid before moving that file to storage. The API will return important information about the archive, including if it was password protected, along with an error count and warning count if the file is found to be invalid. Below, I’ll walk you through how to easily structure an API call using ready-to-run code snippets in Java.

Let’s kick things off by installing the Java SDK with Maven. Include the following reference in pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next include a reference in the dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now that installation is complete, include the imports at the top of your file and call the validation 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.ValidateDocumentApi;
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");
ValidateDocumentApi apiInstance = new ValidateDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
DocumentValidationResult result = apiInstance.validateDocumentZipValidation(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ValidateDocumentApi#validateDocumentZipValidation");
e.printStackTrace();
}

The only two parameters you need to satisfy are:

  1. The input file for the operation.
  2. Your Cloudmersive API key (can be easily obtained by registering a free account on our website; this account will provide a limit of 800 API calls per month).

After that, you’re all set — no more code required.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet