How to Unzip Files with Python

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

Why Unzip Files with Python?

The pdfRest Zip Files API Tool offers a seamless way to manage .zip files within your applications using Python. This tutorial will guide you through the process of sending an API call to the /unzip endpoint to unzip files, demonstrating how to efficiently integrate this functionality into your projects.

In real-world applications, you might need to manage multiple files at once, such as extracting documents from a zip archive for processing or analysis. Unzipping files can streamline access to these documents, making them easier to work with and faster to process. This is particularly useful in scenarios where quick access to individual files is needed or when dealing with large datasets.

Unzip Files with Python Code Example

import requests

url = "https://api.pdfrest.com/unzip"
payload = {}
files = [
  ('file', open('/path/to/your/file.zip','rb'))
]
headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)

Source: GitHub

Breaking Down the Code

The code begins by importing the requests library, which is essential for making HTTP requests in Python.

url = "https://api.pdfrest.com/unzip"

This line sets the URL for the API endpoint you will be calling. The endpoint https://api.pdfrest.com/unzip is specifically used for unzipping files through the pdfRest API.

payload = {}

The payload variable is initialized as an empty dictionary. This is where you would typically include any additional data required by the API, but for this particular call, no extra data is needed.

files = [
  ('file', open('/path/to/your/file.zip','rb'))
]

The files variable is a list of tuples, where each tuple contains a file key and the file object. Here, 'file' is the key expected by the API, and the file object is opened in binary read mode. Replace /path/to/your/file.zip with the actual path to the zip file you wish to unzip.

headers = {
  'Accept': 'application/json',
  'Authorization': 'Bearer YOUR_ACCESS_TOKEN'
}

The headers dictionary includes the necessary headers for the API request. The 'Accept' header specifies that the response should be in JSON format. The 'Authorization' header includes a Bearer token, which is required for authenticating the request. Replace YOUR_ACCESS_TOKEN with your actual access token.

response = requests.request("POST", url, headers=headers, data=payload, files=files)

This line sends a POST request to the API endpoint, including the headers, payload, and files. The response from the server is stored in the response variable.

print(response.text)

Finally, the response text is printed to the console, allowing you to see the result of your API call.

Beyond the Tutorial

In this tutorial, you learned how to use Python to send an API call to the pdfRest Zip Files tool to unzip a file. This is a practical example of how you can integrate file handling capabilities into your applications using the pdfRest API.

To explore more functionalities, try out all the pdfRest 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 at GitHub.

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