The pdfRest Zip Files API Tool is a powerful resource that allows developers to manage file decompression seamlessly. This tutorial will guide you through the process of sending an API call to unzip files using Java, demonstrating how to integrate this functionality into your applications.
Unzipping files is essential for accessing and processing individual files within a compressed archive. For instance, a user might receive a large number of documents in a zip file from a cloud service. By unzipping these files, the user can efficiently access and manage each document, ensuring that all files are available for processing without any being overlooked.
import io.github.cdimascio.dotenv.Dotenv; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import okhttp3.*; import org.json.JSONObject; public class Unzip { // Specify the path to your file here, or as the first argument when running the program. private static final String DEFAULT_FILE_PATH = "/path/to/file.zip"; // Specify your API key here, or in the environment variable PDFREST_API_KEY. // You can also put the environment variable in a .env file. private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; public static void main(String[] args) { File inputFile; if (args.length > 0) { inputFile = new File(args[0]); } else { inputFile = new File(DEFAULT_FILE_PATH); } final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); final RequestBody inputFileRequestBody = RequestBody.create(inputFile, MediaType.parse("application/pdf")); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) .build(); Request request = new Request.Builder() .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) .url("https://api.pdfrest.com/unzip") .post(requestBody) .build(); try { OkHttpClient client = new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); Response response = client.newCall(request).execute(); System.out.println("Result code " + response.code()); if (response.body() != null) { System.out.println(prettyJson(response.body().string())); } } catch (IOException e) { throw new RuntimeException(e); } } private static String prettyJson(String json) { // https://stackoverflow.com/a/9583835/11996393 return new JSONObject(json).toString(4); } }
Source: GitHub
The code begins by importing necessary libraries such as Dotenv
for environment variable management, OkHttpClient
for HTTP requests, and JSONObject
for JSON handling.
private static final String DEFAULT_FILE_PATH = "/path/to/file.zip"; private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
These lines define default values for the file path and API key. The API key can be stored in an environment variable named PDFREST_API_KEY
or directly in the code.
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
This line loads environment variables using the Dotenv library, allowing you to manage sensitive information like API keys securely.
RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) .build();
Here, a multipart form request body is created, which includes the file to be unzipped. The addFormDataPart
method attaches the file to the request.
Request request = new Request.Builder() .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) .url("https://api.pdfrest.com/unzip") .post(requestBody) .build();
This snippet constructs the HTTP POST request, setting the API endpoint URL and attaching the API key in the header.
OkHttpClient client = new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
An HTTP client is instantiated with a 60-second read timeout to handle the request execution.
In this tutorial, you learned how to send an API call to the pdfRest Zip Files endpoint using Java. This process allows you to manage file compression and decompression efficiently within your applications.
To further explore the capabilities of pdfRest, try out all the API Tools in the API Lab. For more detailed information, refer to the API Reference Guide.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found here.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.