How to Zip Files with Python

Learn how to use pdfRest Zip Files API Tool with Python to zip processed files for easy downloads.
Share this page

Why Use Zip Files with Python?

The pdfRest Zip Files API Tool provides a convenient way to compress multiple files into a single ZIP archive programmatically.

This tutorial will guide you through the process of sending an API call to the pdfRest Zip Files endpoint using Python. Zipping files can be particularly useful when dealing with a large number of documents or when you need to save space and bandwidth during file transfer.

For instance, suppose you are working on a web application that allows users to download multiple PDF reports. Instead of downloading each report individually, you could use the pdfRest API to compress these reports into a ZIP file, making the download process more efficient and user-friendly. This not only saves time for the user but also reduces the load on your server and network.

Zip Files with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

zip_endpoint_url = 'https://api.pdfrest.com/zip'

# The /zip endpoint can take one or more file or ids as input and compresses them into a .zip.
# This sample takes 2 files and compresses them into a zip file.
zip_request_data = []

# Array of tuples that contains information about the 2 files that will be compressed into a .zip
# The 'application/pdf' string below is known as a MIME type, which is a label used to identify the type of a file so that it is handled properly by software.
# Please see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types for more information about MIME types.
files = [
    ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
    ('file_name.tif', open('/path/to/file', 'rb'), 'image/tiff'),
    ('file_name.bmp', open('/path/to/file', 'rb'), 'image/bmp')
]

# Structure the data that will be sent to POST zip request as an array of tuples
for i in range(len(files)):
    zip_request_data.append(("file", files[i]))

zip_request_data.append(('output', 'example_zip_out'))

mp_encoder_zip = MultipartEncoder(
    fields=zip_request_data
)

# Let's set the headers that the zip endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_zip.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to zip endpoint...")
response = requests.post(zip_endpoint_url, data=mp_encoder_zip, 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)

# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

Source of the provided code: GitHub

Breaking Down the Code

The provided Python code demonstrates how to use the pdfRest API to compress multiple files into a ZIP archive. Let's break down the key components of the code:

zip_endpoint_url = 'https://api.pdfrest.com/zip'

This line sets the API endpoint URL to which the POST request will be sent.

files = [
    ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
    ('file_name.tif', open('/path/to/file', 'rb'), 'image/tiff'),
    ('file_name.bmp', open('/path/to/file', 'rb'), 'image/bmp')
]

Here, 'files' is an array of tuples. Each tuple represents a file to be zipped, including the file name, a file object opened in binary read mode, and the file's MIME type.

for i in range(len(files)):
    zip_request_data.append(("file", files[i]))

This loop adds each file to the 'zip_request_data' list as part of the data to be sent in the API request.

mp_encoder_zip = MultipartEncoder(
    fields=zip_request_data
)

The 'MultipartEncoder' is used to encode the files and data for the multipart/form-data POST request.

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

The 'headers' dictionary includes the necessary headers for the request, such as 'Content-Type' and 'Accept'. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.

response = requests.post(zip_endpoint_url, data=mp_encoder_zip, headers=headers)

A POST request is made to the zip endpoint with the encoded data and headers.

Beyond the Tutorial

By following the steps outlined in this tutorial, you've learned how to make an API call to the pdfRest Zip Files endpoint to compress multiple files into a ZIP archive using Python. This can be a powerful feature for applications that need to handle file compression and distribution efficiently.

If you want to explore more capabilities of the pdfRest API, consider demoing all of the pdfRest API Tools in the API Lab. For a comprehensive understanding of the available endpoints and their functionalities, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads for the pdfRest API 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