Free Python Library to Create Password-Protected ZIP-Archives
Leading Open Source Efficient and 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 an open-source Python library that enables software developers to create ZIP files with password protection and custom compression levels inside Python applications. Developed as a wrapper around the minizip library (a part of the zlib library suite), Pyminizip lets users create encrypted ZIP files with ease. It provides a very simple API that is well-suited for beginners while still providing robust functionality for more advanced users. There are several important features part of the library, such as Password Protected ZIP file creation, compress multiple files or folders, directory compression for folder-based applications, setting custom compression levels and many more.
Pyminizip is a powerful Python library that provides a simple and efficient way to compress and decompress files using the minizip algorithm. Developed by Srdjan Mihajlovic, the library is designed to be fast, lightweight, and easy to use, making it an ideal choice for a wide range of applications, from data backup and archiving to cloud storage and more. Pyminizip supports multiple platforms, making applications compatible across Windows, macOS, and Linux. By allowing AES encryption for password protection, it enhances security, meeting the needs of applications handling confidential files. Its straightforward API can be easily integrated into a variety of applications, from simple scripts to complex GUIs. Its flexibility in handling individual files, batch files, and directories makes it adaptable to many application types.
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 pyminizip
Clone 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.