7z libraries for Python
Python’s standard library has zipfile, tarfile and the lzma module (raw LZMA and .xz streams) but no 7z archive support. Two libraries fill the gap:
- py7zr is pure Python (with C-accelerated compression modules from PyPI) and has a
zipfile-like API. It reads and writes 7z with LZMA, LZMA2, BZip2, Deflate, PPMd, Zstandard, Brotli, BCJ and Delta, and supports AES-256 encryption including encrypted headers. It can plug intoshutil.make_archiveandshutil.unpack_archive. It doesn’t support BCJ2 and reads Deflate64 but can’t write it. - libarchive-c is a ctypes binding to the C libarchive library. It reads 7z (including BCJ2) and writes it, and gives you the same API for TAR, ZIP, ISO, RAR and other formats. It can’t handle encrypted 7z archives and needs the libarchive shared library installed.
Pick py7zr for most 7z work, especially encryption; libarchive-c when you process many archive formats with one code path or need BCJ2 archives.
- libarchive-c libarchive-c is a Python binding to libarchive that reads and writes 7z archives. How to install it, extract a 7z file, read entries in memory.
- py7zr py7zr is a Python library for reading and writing 7z archives. How to install it, extract and create 7z files, list contents, read a file into memory.