How to Unzip Files with Java

Learn how to use the pdfRest Zip Files API Tool to unzip files from .zip archives with Java.
Share this page

Why Unzip Files with Java?

The pdfRest Zip Files API Tool provides a streamlined approach to file compression and decompression through API calls. This tutorial will guide you on how to send an API call to the Zip Files endpoint using Java, allowing for seamless integration into your Java applications. By leveraging this API, developers can automate the process of zipping and unzipping files, enhancing the efficiency of file management within their applications.

Using Zip Files can be crucial for efficiently managing and processing large datasets with pdfRest. For example, a company that regularly exchanges large volumes of data with clients can use the Zip Files API to compress these files before transmission, reducing upload and download times. When receiving compressed files, the API can automatically unzip and prepare them for processing, saving time and minimizing manual effort.

Unzip Files with Java Code Example

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Unzip {
    public static void main(String[] args) {
        String apiKey = "YOUR_API_KEY";
        String filePath = "path/to/your/file.zip";
        
        try {
            File file = new File(filePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            
            URL url = new URL("https://api.pdfrest.com/unzip");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Authorization", "Bearer " + apiKey);
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/zip");
            
            OutputStream outputStream = connection.getOutputStream();
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
            fileInputStream.close();
            
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Source: GitHub Repository

Breaking Down the Code

The provided Java code demonstrates how to make a POST request to the pdfRest API to unzip a file. Let's break down the key components:

String apiKey = "YOUR_API_KEY";

This line sets the API key, which is necessary for authenticating your request. Replace "YOUR_API_KEY" with your actual API key.

String filePath = "path/to/your/file.zip";

Here, you specify the path to the zip file you want to unzip. Ensure that the file path is correct and accessible by your application.

URL url = new URL("https://api.pdfrest.com/unzip");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

This snippet creates a URL object for the unzip endpoint and opens an HTTP connection to it. The endpoint URL is https://api.pdfrest.com/unzip.

connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Bearer " + apiKey);
connection.setRequestProperty("Content-Type", "application/zip");

The request method is set to POST, as we are sending data to the server. The "Authorization" header includes the Bearer token for authentication, and the "Content-Type" header specifies that the content being sent is a zip file.

Beyond the Tutorial

In this tutorial, you learned how to use the pdfRest Zip Files API Tool to unzip a file using Java. This example demonstrates how to make a multipart API call, which is essential for handling file uploads. To further explore the capabilities of pdfRest, try out all the API Tools in the API Lab. For detailed documentation, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub Repository.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us