A Python wrapper for 7zz CLI tool providing cross-platform archive operations with built-in security protection, Windows filename compatibility, and comprehensive API support.
- Features
- Installation
- Quick Start
- API Reference
- Advanced Features
- Migration Guide
- Contributing
- License
- 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
pip install py7zzFor development:
git clone https://github.com/rxchi1d/py7zz.git
cd py7zz
pip install -e .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())# 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/')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())Main class for archive operations, compatible with zipfile.ZipFile.
Parameters:
file: Path to archivemode: 'r' (read), 'w' (write), 'a' (append)preset: Compression preset ('fast', 'balanced', 'ultra')
Methods:
namelist(): List all filesextractall(path, members): Extract filesadd(name, arcname): Add fileread(name): Read file contenttestzip(): Test integrity
Create archive from files.
Extract all files from archive.
Test archive integrity.
Configure security limits for archive processing.
Check if archive file count exceeds security limits.
Sanitize filename for Windows compatibility.
Check if filename is valid on Windows.
Get Windows-compatible filename with conflict resolution.
Async version of SevenZipFile with identical methods.
Async versions of simple functions.
'fast': Quick compression'balanced': Default'ultra': Maximum compression
py7zz.setup_logging('INFO') # Configure logging
py7zz.disable_warnings() # Hide warningspy7zz 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.
# 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!# 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.
| Format | Read | Write |
|---|---|---|
| 7Z | ✅ | ✅ |
| ZIP | ✅ | ✅ |
| TAR | ✅ | ✅ |
| RAR | ✅ | ❌ |
| GZIP | ✅ | ✅ |
| BZIP2 | ✅ | ✅ |
| XZ | ✅ | ✅ |
And 40+ more formats for reading.
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}")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") # → Falseasync def progress_callback(info):
print(f"Progress: {info.percentage:.1f}%")
await py7zz.extract_archive_async('large.7z', progress_callback=progress_callback)archives = ['backup1.7z', 'backup2.7z', 'backup3.7z']
py7zz.batch_extract_archives(archives, 'output/')# Clone repository
git clone https://github.com/rxchi1d/py7zz.git
cd py7zz
# Install dependencies (development mode)
uv sync --dev
uv pip install -e .# Run tests
pytest
# Check code quality
ruff check .
mypy .- Follow PEP 8
- Use type hints
- Maximum line length: 88
- Format with
ruff format
- Python 3.8+
- No external dependencies
- Supported platforms:
- Windows x64
- macOS (Intel & Apple Silicon)
- Linux x86_64
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.dev1We welcome contributions! See Contributing Guide for:
- Development setup
- Code style guidelines
- Commit conventions
- Pull request process
- Documentation: API Reference
- Issues: GitHub Issues
- Discussions: GitHub Discussions
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.
Built on 7-Zip by Igor Pavlov.