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 powerful and convenient solution for unzipping files programmatically. This tutorial will guide you through the process of sending an API call to unzip files using Python, leveraging the pdfRest API. This functionality is particularly beneficial when dealing with compressed files that need to be extracted and processed within an automated workflow.

Users might need to unzip files for various reasons. For example, a business might receive a batch of compressed invoices that require extraction and individual processing. Automating this task through an API call can save time and reduce the potential for human error, ensuring that files are handled efficiently and consistently.

Unzip Files with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

unzip_endpoint_url = 'https://api.pdfrest.com/unzip'

mp_encoder_unzip = MultipartEncoder(
    fields={
        'file': ('file_name.zip', open('/path/to/file', 'rb'), 'application/zip'),
        'output' : 'example_out'
    }
)

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_unzip.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to unzip endpoint...")
response = requests.post(unzip_endpoint_url, data=mp_encoder_unzip, headers=headers)

print("Response status code: " + str(response.status_code))

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

Source: GitHub

Breaking Down the Code

The code begins by importing necessary modules: MultipartEncoder from requests_toolbelt, and requests and json for handling HTTP requests and JSON data.

unzip_endpoint_url = 'https://api.pdfrest.com/unzip'

This line sets the URL for the unzip endpoint of the pdfRest API.

mp_encoder_unzip = MultipartEncoder(
    fields={
        'file': ('file_name.zip', open('/path/to/file', 'rb'), 'application/zip'),
        'output' : 'example_out'
    }
)

The MultipartEncoder is used to create a multipart/form-data payload. The fields dictionary contains:

  • 'file': A tuple where the first element is the file name, the second is the file object opened in binary read mode, and the third is the MIME type.
  • 'output': Specifies the name of the output file after extraction.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_unzip.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

The headers dictionary includes:

  • 'Accept': Indicates that the client expects a JSON response.
  • 'Content-Type': Set to the content type of the MultipartEncoder instance.
  • 'Api-Key': A placeholder for your unique API key.
response = requests.post(unzip_endpoint_url, data=mp_encoder_unzip, headers=headers)

This line sends a POST request to the unzip endpoint with the multipart data and headers.

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

If the request is successful, the JSON response is printed in a formatted manner. Otherwise, the error message is displayed.

Beyond the Tutorial

In this tutorial, you learned how to use Python to make an API call to the pdfRest Zip Files API Tool, allowing you to unzip files programmatically. This can be a valuable tool for automating file management tasks.

To explore more, try out all the pdfRest API Tools in the API Lab. For further reading and a deeper understanding, refer to the API Reference Guide.

Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit this 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