pyzipper

0.3.6 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to create a new ZIP archive, add a file with AES encryption and LZMA compression, set a password, and then read the content back, using `pyzipper.AESZipFile`.

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}'.")

view raw JSON →