Create Password-Protected ZIP-Archives via Free Python Library
Leading Open Source Efficient & Secure Python Library That Enables Python Developers to Create Password-Protected ZIP File with Custom Compression Settings via Free API.
What is Pyminizip Library?
Pyminizip is a powerful, open-source Python library that enables software developers to create password-protected ZIP files with custom compression levels. Built as a wrapper around the minizip algorithm from the zlib suite, it offers a straightforward API ideal for beginners while delivering robust functionality for advanced needs. Key features include password-protected ZIP file creation, compressing multiple files or entire folders, directory compression, and adjustable compression settings.
Developed by Srdjan Mihajlovic, this fast and lightweight tool is perfect for data backup, archiving, and cloud storage applications. Pyminizip supports cross-platform compatibility across Windows, macOS, and Linux and enhances security with AES encryption for confidential files. Its simple integration suits everything from basic scripts to complex GUIs, providing flexibility for handling individual files, batch operations, and directories within diverse Python applications.
Getting Started with Pyminizip
The recommend way to install Pyminizip is using pypi.org. Please use the following command for a smooth installation.
Install Pyminizip via .NET via pypi
$ pip install pyminizipClone the Pyminizip repository from GitHub
git clone https://github.com/smihica/pyminizip.git
It is also possible to install it manually; download the latest release files directly from GitHub repository.Password-Protected ZIP Creation via Python
The open source Pyminizip library makes it easy for software developers to create and manage password-protected ZIP files inside their own python applications. While Python’s standard library offers some compression tools but password protection is missing in the zipfile module. Pyminizip fills this gap by allowing users to encrypt their files using the AES encryption standard. The following example shows how software developers can create password protected ZIP files and set level of compression level inside Python applications.
How to Create Password-Protected ZIP File inside Python Apps?
import pyminizip
# Define file paths and password
input_file = "sample.txt" # file to zip
output_file = "sample.zip" # output zip file name
password = "securepassword"
compression_level = 5 # range 1-9, where 1 is fastest and 9 is maximum compression
# Create password-protected zip file
pyminizip.compress(input_file, None, output_file, password, compression_level)
print(f"{output_file} created with password protection.")
Compress Directory via Python API
Beyond compressing single files, the Pyminizip library allows compressing entire directories with just a couple of lines of Python code. This feature is handy when multiple files need to be packaged together, especially for backup or distribution purposes. The library will include all files and folders in the specified directory, allowing for password protection on the whole archive. Here is a very useful example for compressing a directory inside Python applications.
How to Compress a Directory via Python Library?
import pyminizip
import os
def compress_directory(directory_path, output_file, password, compression_level):
for root, _, files in os.walk(directory_path):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, directory_path)
pyminizip.compress(file_path, arcname, output_file, password, compression_level)
# Usage
directory_path = "my_folder"
output_file = "my_folder.zip"
password = "mypassword"
compression_level = 5
compress_directory(directory_path, output_file, password, compression_level)
print(f"Directory '{directory_path}' compressed into '{output_file}' with password protection.")
Control Compression Level via Python API
The open source Pyminizip Python library provides control over the compression level, allowing users to balance between compression speeds and file size. A lower level results in faster compression with larger output sizes and on the other side a higher level provides better compression at the cost of speed. This flexibility makes Pyminizip adaptable for various use cases, from quick archiving to efficient file storage. Here is an example that demonstrates how to test different compression levels inside Python applications.
How to Apply Different Compression Levels inside Python Apps?
import pyminizip
input_file = "example.txt"
password = "test123"
for level in range(1, 10):
output_file = f"example_level_{level}.zip"
pyminizip.compress(input_file, None, output_file, password, level)
print(f"Created {output_file} with compression level {level}")
Integration with Python Applications
Given its simplicity and ease of use, Pyminizip library can be readily integrated into Python applications requiring file compression. It works particularly well with backup utilities, secure file distribution systems, and archival applications that need password protection.