Pure python 7-zip library providing comprehensive 7z archive format support with compression, decompression, encryption and CLI tools
npx @tessl/cli install tessl/pypi-py7zr@1.0.0A comprehensive pure Python implementation of the 7-zip archive format, supporting compression, decompression, encryption, and decryption operations without requiring external C/C++ dependencies. py7zr provides both a library interface and command-line tools for working with 7z archives, supporting multiple compression algorithms and encryption methods.
pip install py7zrimport py7zrCommon imports for archive operations:
from py7zr import SevenZipFile, is_7zfile, pack_7zarchive, unpack_7zarchiveException classes:
from py7zr import Bad7zFile, PasswordRequired, DecompressionError, UnsupportedCompressionMethodErrorFilter and compression constants:
from py7zr import FILTER_LZMA2, FILTER_ZSTD, FILTER_CRYPTO_AES256_SHA256import py7zr
# Extract entire archive
with py7zr.SevenZipFile('archive.7z', mode='r') as archive:
archive.extractall(path='/tmp/extracted')
# Extract specific files
with py7zr.SevenZipFile('archive.7z', mode='r') as archive:
archive.extract(['file1.txt', 'folder/file2.txt'], path='/tmp/extracted')
# Extract password-protected archive
with py7zr.SevenZipFile('encrypted.7z', mode='r', password='secret') as archive:
archive.extractall()import py7zr
# Create archive from directory
with py7zr.SevenZipFile('new_archive.7z', mode='w') as archive:
archive.writeall('/path/to/source_directory', 'archived_name')
# Create encrypted archive
with py7zr.SevenZipFile('encrypted.7z', mode='w', password='secret') as archive:
archive.writeall('/path/to/source_directory')
# Create archive with custom compression
filters = [{"id": py7zr.FILTER_ZSTD}]
with py7zr.SevenZipFile('compressed.7z', mode='w', filters=filters) as archive:
archive.writeall('/path/to/source_directory')import py7zr
# Check if file is valid 7z archive
if py7zr.is_7zfile('file.7z'):
print("Valid 7z archive")
# Use with shutil interface
py7zr.pack_7zarchive('output', '/path/to/source')
py7zr.unpack_7zarchive('archive.7z', '/path/to/destination')py7zr is built around several key components:
Primary archive manipulation functionality including reading, writing, extracting, and testing 7z archives. Supports password protection, selective extraction, and various compression algorithms.
class SevenZipFile:
def __init__(self, file, mode="r", *, filters=None, dereference=False,
password=None, header_encryption=False, blocksize=None, mp=False): ...
def extractall(self, path=None, members=None, pwd=None, callback=None): ...
def extract(self, member, path=None, pwd=None): ...
def writeall(self, path, arcname=None): ...
def write(self, file, arcname=None): ...
def test(): ...Command-line tools for working with 7z archives including create, extract, list, test, and info operations. Supports all major archive operations with password protection and multi-volume archives.
def main(): ... # CLI entry point
# Commands: c (create), x (extract), l (list), t (test), i (info)Flexible I/O abstraction supporting file, memory, and custom backends with progress reporting through callback interfaces. Enables custom extraction destinations and progress monitoring.
class Py7zIO:
def write(self, s): ...
def read(self, size=None): ...
def seek(self, offset, whence=0): ...
class Callback:
def report_start(self, processing_file_path, processing_bytes): ...
def report_update(self, decompressed_bytes): ...Comprehensive error handling with specific exception types for different failure modes including invalid archives, compression errors, and missing passwords.
class Bad7zFile(Exception): ...
class DecompressionError(Exception): ...
class PasswordRequired(Exception): ...
class UnsupportedCompressionMethodError(Exception): ...py7zr supports multiple compression algorithms and filters:
Compression Methods:
Filters:
Encryption:
# Filter constants
FILTER_LZMA2: int
FILTER_LZMA: int
FILTER_BZIP2: int
FILTER_DEFLATE: int
FILTER_ZSTD: int
FILTER_BROTLI: int
FILTER_PPMD: int
FILTER_CRYPTO_AES256_SHA256: int
FILTER_DELTA: int
FILTER_X86: int
FILTER_ARM: int
# Checksum constants
CHECK_SHA256: int
CHECK_CRC64: int
CHECK_CRC32: int
CHECK_NONE: int
# Compression presets
PRESET_DEFAULT: int
PRESET_EXTREME: intdef is_7zfile(file) -> bool:
"""
Check if file is a valid 7z archive.
Parameters:
- file: str or file-like object, path to file or file object to check
Returns:
bool: True if file is valid 7z archive
"""
def pack_7zarchive(base_name, base_dir, owner=None, group=None,
dry_run=None, logger=None) -> str:
"""
Create 7z archive for shutil integration.
Parameters:
- base_name: str, base name for archive (without .7z extension)
- base_dir: str, directory to archive
- owner: unused (for shutil compatibility)
- group: unused (for shutil compatibility)
- dry_run: unused (for shutil compatibility)
- logger: unused (for shutil compatibility)
Returns:
str: path to created archive file
"""
def unpack_7zarchive(archive, path, extra=None):
"""
Extract 7z archive for shutil integration.
Parameters:
- archive: str, path to archive file
- path: str, extraction destination directory
- extra: unused (for shutil compatibility)
Returns:
None
Raises:
shutil.ReadError: if archive is not valid 7z file
"""