Skip to main content

Python wrapper for 7zz CLI tool providing cross-platform compression with multiple archive formats

Project description

py7zz

PyPI Python License CI

A Python wrapper for 7zz CLI tool providing cross-platform archive operations with built-in security protection, Windows filename compatibility, and comprehensive API support.

Table of Contents

Features

  • Cross-platform: Windows, macOS, Linux
  • 50+ formats: 7Z, ZIP, TAR, RAR, and more
  • API compatible: Drop-in replacement for zipfile/tarfile
  • Windows compatibility: Automatic filename sanitization
  • Security protection: ZIP bomb detection and file count limits
  • Async support: Non-blocking operations with progress
  • Zero dependencies: Bundled 7zz binary

Installation

pip install py7zz

For development:

git clone https://github.com/rxchi1d/py7zz.git
cd py7zz
pip install -e .

Quick Start

Basic Usage

import py7zz

# Create archive
py7zz.create_archive('backup.7z', ['documents/', 'photos/'])

# Extract archive
py7zz.extract_archive('backup.7z', 'extracted/')

# List contents
with py7zz.SevenZipFile('backup.7z', 'r') as sz:
    print(sz.namelist())

Drop-in Replacement

# OLD: zipfile
import zipfile
with zipfile.ZipFile('archive.zip', 'r') as zf:
    zf.extractall('output/')

# NEW: py7zz (identical API)
import py7zz
with py7zz.SevenZipFile('archive.7z', 'r') as sz:
    sz.extractall('output/')

Async Operations

import asyncio
import py7zz

async def main():
    await py7zz.create_archive_async('backup.7z', ['data/'])
    await py7zz.extract_archive_async('backup.7z', 'output/')

asyncio.run(main())

API Reference

Core Classes

SevenZipFile(file, mode='r', preset=None)

Main class for archive operations, compatible with zipfile.ZipFile.

Parameters:

  • file: Path to archive
  • mode: 'r' (read), 'w' (write), 'a' (append)
  • preset: Compression preset ('fast', 'balanced', 'ultra')

Methods:

  • namelist(): List all files
  • extractall(path, members): Extract files
  • add(name, arcname): Add file
  • read(name): Read file content
  • testzip(): Test integrity

Simple Functions

create_archive(path, files, preset='balanced')

Create archive from files.

extract_archive(path, output_dir='.')

Extract all files from archive.

test_archive(path)

Test archive integrity.

Security Features

SecurityConfig(max_file_count=5000, max_compression_ratio=100.0, max_total_size=10737418240)

Configure security limits for archive processing.

check_file_count_security(file_list, config=None)

Check if archive file count exceeds security limits.

Filename Utilities

sanitize_filename(filename)

Sanitize filename for Windows compatibility.

is_valid_windows_filename(filename)

Check if filename is valid on Windows.

get_safe_filename(filename, existing_names=None)

Get Windows-compatible filename with conflict resolution.

Async API

AsyncSevenZipFile

Async version of SevenZipFile with identical methods.

create_archive_async(), extract_archive_async()

Async versions of simple functions.

Configuration

Compression Presets

  • 'fast': Quick compression
  • 'balanced': Default
  • 'ultra': Maximum compression

Logging

py7zz.setup_logging('INFO')  # Configure logging
py7zz.disable_warnings()     # Hide warnings

Exception Handling

py7zz provides specific exceptions for different error conditions:

from py7zz.exceptions import (
    ZipBombError,           # Potential ZIP bomb detected
    SecurityError,          # Security limits exceeded
    PasswordRequiredError,  # Archive requires password
    FileNotFoundError,      # File or archive not found
    CorruptedArchiveError   # Archive is corrupted
)

try:
    py7zz.extract_archive('archive.7z')
except ZipBombError:
    print("Archive may be a ZIP bomb")
except PasswordRequiredError:
    print("Archive is password protected")
except CorruptedArchiveError:
    print("Archive is corrupted")

See API Documentation for complete reference.

Migration Guide

From zipfile

# Change import
import py7zz  # was: import zipfile

# Change class name
with py7zz.SevenZipFile('archive.7z', 'r') as sz:  # was: zipfile.ZipFile
    sz.extractall()  # Same API!

From tarfile

# Change import
import py7zz  # was: import tarfile

# Use same class
with py7zz.SevenZipFile('archive.tar.gz', 'r') as sz:  # was: tarfile.open
    sz.extractall()  # Same API!

See Migration Guide for detailed instructions.

Supported Formats

Format Read Write
7Z
ZIP
TAR
RAR
GZIP
BZIP2
XZ

And 40+ more formats for reading.

Advanced Features

Security Protection

Built-in protection against malicious archives:

from py7zz import SecurityConfig, ZipBombError

# Configure security limits
config = SecurityConfig(max_file_count=1000, max_compression_ratio=50.0)

try:
    py7zz.extract_archive('suspicious.zip')
except ZipBombError as e:
    print(f"Potential ZIP bomb detected: {e}")

Windows Filename Compatibility

Automatically handles Windows restrictions and provides utilities:

from py7zz import sanitize_filename, is_valid_windows_filename

# Auto-sanitization during extraction
py7zz.extract_archive('unix-archive.tar.gz')  # Files sanitized automatically

# Manual filename utilities
safe_name = sanitize_filename("invalid<file>name.txt")  # → "invalid_file_name.txt"
is_valid = is_valid_windows_filename("CON.txt")  # → False

Progress Monitoring

async def progress_callback(info):
    print(f"Progress: {info.percentage:.1f}%")

await py7zz.extract_archive_async('large.7z', progress_callback=progress_callback)

Batch Operations

archives = ['backup1.7z', 'backup2.7z', 'backup3.7z']
py7zz.batch_extract_archives(archives, 'output/')

Development

Setup

# Clone repository
git clone https://github.com/rxchi1d/py7zz.git
cd py7zz

# Install dependencies (development mode)
uv sync --dev
uv pip install -e .

Testing

# Run tests
pytest

# Check code quality
ruff check .
mypy .

Code Style

  • Follow PEP 8
  • Use type hints
  • Maximum line length: 88
  • Format with ruff format

Requirements

  • Python 3.8+
  • No external dependencies
  • Supported platforms:
    • Windows x64 / ARM64 (ARM64 requires Python 3.11+)
    • macOS (Intel & Apple Silicon)
    • Linux x86_64 / ARM64
  • Note: Windows ARM64 requires Python 3.11+ because native CPython builds for Windows on ARM start at that version (python.org limitation).

Version Information

py7zz follows PEP 440 versioning standard:

import py7zz
print(py7zz.get_version())           # py7zz version (e.g., "1.0.0")
print(py7zz.get_bundled_7zz_version())  # 7zz version

# Version types supported:
# - Stable: 1.0.0
# - Alpha: 1.0.0a1
# - Beta: 1.0.0b1
# - Release Candidate: 1.0.0rc1
# - Development: 1.0.0.dev1

Contributing

We welcome contributions! See Contributing Guide for:

  • Development setup
  • Code style guidelines
  • Commit conventions
  • Pull request process

Support

License

Python source code: MIT (see LICENSE) Bundled runtime: 7-Zip 7zz under its own licenses (LGPL v2.1 + unRAR; parts BSD). See THIRD_PARTY_NOTICES.md and licenses/7zip-LICENSE.txt.

Acknowledgments

Built on 7-Zip by Igor Pavlov.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

py7zz-1.2.0a1-py3-none-win_arm64.whl (1.1 MB view details)

Uploaded Python 3Windows ARM64

py7zz-1.2.0a1-py3-none-win_amd64.whl (1.2 MB view details)

Uploaded Python 3Windows x86-64

py7zz-1.2.0a1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

py7zz-1.2.0a1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

py7zz-1.2.0a1-py3-none-macosx_10_9_universal2.whl (2.7 MB view details)

Uploaded Python 3macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file py7zz-1.2.0a1-py3-none-win_arm64.whl.

File metadata

  • Download URL: py7zz-1.2.0a1-py3-none-win_arm64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py7zz-1.2.0a1-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 a0adabe1b98adcc8f183a60221358dc09665b1f6fb2f8ff6227434b0ba5f0f00
MD5 d749c435ce25af74a94a288b1974a159
BLAKE2b-256 579636b52ecaeb96ce466c9223fd0f1024b71f8df9481dcbb4b780f0051801eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for py7zz-1.2.0a1-py3-none-win_arm64.whl:

Publisher: release.yml on RxChi1d/py7zz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py7zz-1.2.0a1-py3-none-win_amd64.whl.

File metadata

  • Download URL: py7zz-1.2.0a1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for py7zz-1.2.0a1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 0ba560125379e167b1335c430ffd1747a5f16962f880f1e346beda1ee60ad05b
MD5 b907ae42cb039a68a9b138d5c74bb6e8
BLAKE2b-256 150f189178f2ad600108590bcf565405af776dc6b2693148a6d8debe1678a423

See more details on using hashes here.

Provenance

The following attestation bundles were made for py7zz-1.2.0a1-py3-none-win_amd64.whl:

Publisher: release.yml on RxChi1d/py7zz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py7zz-1.2.0a1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for py7zz-1.2.0a1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6272a1179968bba572560b4a8b138d7f7b3488d9da366b40b0243fa9654e6c5d
MD5 8d3a61982139f41fbbb7de6d64aac0c9
BLAKE2b-256 a1dac21c0e5087dd2d0681f5413976b1e62f09c3dfe11f947aa2aff4d08c8fde

See more details on using hashes here.

Provenance

The following attestation bundles were made for py7zz-1.2.0a1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on RxChi1d/py7zz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py7zz-1.2.0a1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for py7zz-1.2.0a1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d049214196c125b2e23694ad0fce5c6c6029740080ae0454058993b425f3aa7c
MD5 9f367dd22de2c2c4e5c50dac1fce0f66
BLAKE2b-256 42c59e8eeae86401b47989a522c8f4652c0e30d34ffe2ea7431f6aab95c3b6ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for py7zz-1.2.0a1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on RxChi1d/py7zz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file py7zz-1.2.0a1-py3-none-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for py7zz-1.2.0a1-py3-none-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 4577ba14806d97ee5766f9d3b2f044e08a133f0a393c96a19bb9638657490482
MD5 65da7e1c697c1933dcbab764b56124dc
BLAKE2b-256 ad8747bc976f1ec74b68fd9ad7fce83736ff00a862087a7aa7c9e09a86d45556

See more details on using hashes here.

Provenance

The following attestation bundles were made for py7zz-1.2.0a1-py3-none-macosx_10_9_universal2.whl:

Publisher: release.yml on RxChi1d/py7zz

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page