py7zr
Pure Python 7-Zip archive library — reads and writes .7z archives without requiring the 7-Zip binary. py7zr features: SevenZipFile context manager (similar to zipfile API), extractall()/extract() for extraction, write()/writeall() for archive creation, compression methods (LZMA, LZMA2, Bzip2, ZStd, PPMd, BCJ filters), password encryption (AES-256), list() for archive contents, read() returning bytes dict, ArchiveInfo for member metadata, supports multi-volume archives, BCJ filter for executable compression, and solid archives. Enables Python agents to handle 7z files without external binary dependencies.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Archive extraction: validate extracted paths to prevent zip-slip path traversal — check that extracted paths don't escape target directory. Decompression bomb risk: 7z solid archives can expand to very large sizes. Encrypted archives use AES-256 — secure; but password in code is bad practice, use environment variable. Never trust archive contents from untrusted sources.
⚡ Reliability
Best When
Working with 7z archives in Python without installing the 7-Zip binary — py7zr handles reading, writing, and encrypting .7z files purely in Python.
Avoid When
Zip/tar formats (use stdlib), high-speed compression (use zstd/lz4), streaming (use gzip/zstd), or when 7-Zip CLI is available and performance matters.
Use Cases
- • Agent archive extraction — import py7zr; with py7zr.SevenZipFile('archive.7z', mode='r') as archive: archive.extractall(path='output/') — extract; agent extracts 7z archives without 7zip installed; extractall() restores full directory structure; mode='r' for reading
- • Agent selective extraction — with py7zr.SevenZipFile('archive.7z') as archive: archive.extract(targets=['config.json', 'data/'], path='output/') — selective extract; agent extracts specific files from large archive; targets list specifies files/directories to extract; others skipped
- • Agent archive creation — with py7zr.SevenZipFile('output.7z', mode='w') as archive: archive.writeall('/path/to/directory/', 'backup') — create archive; agent compresses directory to 7z format; writeall() recursively adds directory contents; mode='w' creates new archive
- • Agent encrypted archive — with py7zr.SevenZipFile('secure.7z', mode='w', password='secret') as archive: archive.writeall('sensitive_data/') — encrypted 7z; agent creates password-protected 7z archives with AES-256 encryption; same password required for extraction
- • Agent archive listing — with py7zr.SevenZipFile('archive.7z') as archive: files = archive.list(); for f in files: print(f.filename, f.file_size, f.compress_size) — inspect contents; agent checks archive contents before extraction; ArchiveInfo has filename, file_size, compress_size, is_directory, lastwritetime
Not For
- • Zip/tar archives — py7zr handles .7z only; for .zip use Python's zipfile; for .tar.gz use tarfile module
- • High-speed compression — 7z LZMA2 optimizes for ratio not speed; for fast compression use zstd or lz4
- • Streaming compression — py7zr requires complete files; for streaming use zstd or gzip
Interface
Authentication
No auth — local archive library. Archive-level password encryption via password= parameter.
Pricing
py7zr is LGPL v2.1+ licensed. Free for use; LGPL applies to modifications of py7zr itself.
Agent Metadata
Known Gotchas
- ⚠ Always use context manager — py7zr.SevenZipFile() must be used as context manager or explicitly closed; unclosed archive can corrupt 7z file being written; agent code: with py7zr.SevenZipFile('f.7z', mode='w') as archive: archive.writeall(...) — context manager ensures proper finalization of central directory
- ⚠ extractall() path must exist — py7zr does not create output directory automatically in all cases; agent code should: import os; os.makedirs(output_path, exist_ok=True) before extractall(); extractall() raises FileNotFoundError if target directory doesn't exist
- ⚠ Encrypted archives need password at open not extract — with py7zr.SevenZipFile('secure.7z', password='pass') as archive — password passed at construction; not at extractall(); forgetting password at open raises PasswordRequired on first read operation; not at archive open time
- ⚠ archive.read() returns dict of {filename: BytesIO} — archive.read(['file.txt']) returns {'file.txt': <BytesIO>}; agent code: content = archive.read(['config.json'])['config.json'].read() to get bytes; read() without arguments reads all files; returns BytesIO objects not strings; .read() to get bytes then .decode() for text
- ⚠ writeall() path argument is archive prefix — archive.writeall('/local/dir/', 'prefix') adds files as prefix/file.txt inside archive; archive.writeall('/local/dir/') without prefix adds files without path prefix; agent code archiving for later extraction must choose prefix carefully to match expected extraction structure
- ⚠ Solid archives cannot be selectively extracted efficiently — solid archive (default for multiple files) must decompress from beginning to extract any file; archive.extract(targets=['specific.txt']) from solid archive decompresses everything to extract one file; for selective extraction, use non-solid: py7zr.SevenZipFile('f.7z', mode='w', filters=[{'id': py7zr.FILTER_LZMA2}])
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for py7zr.
Scores are editorial opinions as of 2026-03-06.