How to Compress, Encrypt, and Password Protect Files in a Zip Archive using Python

Cloudmersive
2 min readJan 6, 2023

--

Creating zip archives is a great way to store a high volume of bulky files in the same place. Securing those files is equally important — and the below API solution will perform both useful services in one fell swoop. All you need to do take advantage of the ready-to-run Python code examples provided below, and you’ll have the ability to compress up to 10 files at once with a customized password and encryption algorithm.

To get started, let’s first install the Python SDK by running the below command:

pip install cloudmersive-convert-api-client

To wrap up, let’s add in 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))
password = 'password_example' # str | Password to place on the Zip file; the longer the password, the more secure
input_file1 = '/path/to/inputfile' # file | First input file to perform the operation on.
encryption_algorithm = 'encryption_algorithm_example' # str | Encryption algorithm to use; possible values are AES-256 (recommended), AES-128, and PK-Zip (not recommended; legacy, weak encryption algorithm). Default is AES-256. (optional)
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, encrypted and password-protected zip archive
api_response = api_instance.zip_archive_zip_create_encrypted(password, input_file1, encryption_algorithm=encryption_algorithm, 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_encrypted: %s\n" % e)

Easy, right? To complete your API call, you’ll just need to include a free-tier API key, which you can get by registering a free account on our website (this will yield a limit of 800 API calls per month and no commitments; once you reach your limit, the total will reset the following month).

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet