pyzipper
Pyzipper is a Python library that extends the functionality of Python's built-in `zipfile` module by adding support for AES encryption. Forked from Python 3.7's `zipfile` module, it maintains a similar API while enabling the creation and extraction of password-protected ZIP archives with AES encryption. The current version is 0.3.6, released in July 2022, and it appears to have an infrequent release cadence.
Warnings
- gotcha Passwords must be provided as byte strings (e.g., `b'yourpassword'`), not regular Python strings. Failing to do so will result in errors during encryption or decryption operations.
- breaking Pyzipper was forked from Python 3.7's `zipfile` module and therefore lacks support for `pathlib`-compatible wrappers and other new features introduced in Python 3.8 and later versions of the standard `zipfile` module.
- gotcha File metadata, such as filenames, are not encrypted by pyzipper. Only the file contents are protected by AES encryption. This means an attacker could still see the names of the files within the archive.
- deprecated The AES-2 encryption used by pyzipper, while better than ZipCrypto, has known flaws and may not be suitable for all high-security use cases. Information leakage can occur, especially if the ZIP file can be intercepted and modified.
- gotcha The project's last release was in July 2022, which might indicate infrequent maintenance. Users should be aware that active development, bug fixes, or security patches might not be regularly provided.
Install
-
pip install pyzipper
Imports
- AESZipFile
import pyzipper with pyzipper.AESZipFile(...) as zf:
Quickstart
import pyzipper
import os
secret_password = b'your_secret_password'
zip_filename = 'secure_archive.zip'
content_to_write = "This is a secret message that needs to be protected."
# Create an AES encrypted zip file
with pyzipper.AESZipFile(zip_filename, 'w', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as zf:
zf.setpassword(secret_password)
zf.writestr('secret.txt', content_to_write)
print(f"'{zip_filename}' created with 'secret.txt' encrypted.")
# Read content from the AES encrypted zip file
with pyzipper.AESZipFile(zip_filename, 'r') as zf:
zf.setpassword(secret_password)
try:
read_content = zf.read('secret.txt').decode('utf-8')
print(f"Content read from 'secret.txt': {read_content}")
except RuntimeError as e:
print(f"Error reading file (incorrect password or corruption): {e}")
# Clean up the created zip file
if os.path.exists(zip_filename):
os.remove(zip_filename)
print(f"Cleaned up '{zip_filename}'.")