How to Create Zip Archive in Python
If you’re looking for a quick & easy way to zip-archive files within a file processing application, the below API solution will make a big difference. With only a few lines of Python code, you’ll gain the ability to programmatically compress up to 10 files at once into a new zip archive.
Your first step is to install the Python SDK; you can do so by running the below command:
pip install cloudmersive-convert-api-client
After that, include the imports and call the function:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ZipArchiveApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file1 = '/path/to/inputfile' # file | First input file to perform the operation on.
input_file2 = '/path/to/inputfile' # file | Second input file to perform the operation on. (optional)
input_file3 = '/path/to/inputfile' # file | Third input file to perform the operation on. (optional)
input_file4 = '/path/to/inputfile' # file | Fourth input file to perform the operation on. (optional)
input_file5 = '/path/to/inputfile' # file | Fifth input file to perform the operation on. (optional)
input_file6 = '/path/to/inputfile' # file | Sixth input file to perform the operation on. (optional)
input_file7 = '/path/to/inputfile' # file | Seventh input file to perform the operation on. (optional)
input_file8 = '/path/to/inputfile' # file | Eighth input file to perform the operation on. (optional)
input_file9 = '/path/to/inputfile' # file | Ninth input file to perform the operation on. (optional)
input_file10 = '/path/to/inputfile' # file | Tenth input file to perform the operation on. (optional)
try:
# Compress files to create a new zip archive
api_response = api_instance.zip_archive_zip_create(input_file1, input_file2=input_file2, input_file3=input_file3, input_file4=input_file4, input_file5=input_file5, input_file6=input_file6, input_file7=input_file7, input_file8=input_file8, input_file9=input_file9, input_file10=input_file10)
pprint(api_response)
except ApiException as e:
print("Exception when calling ZipArchiveApi->zip_archive_zip_create: %s\n" % e)
That’s all the code you’ll need. To complete your API call, include a Cloudmersive API key in the configuration.api_key field (you can get one by registering a free account on our website).