or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli.mdcore-operations.mdexceptions.mdindex.mdio-callbacks.md
tile.json

tessl/pypi-py7zr

Pure python 7-zip library providing comprehensive 7z archive format support with compression, decompression, encryption and CLI tools

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/py7zr@1.0.x

To install, run

npx @tessl/cli install tessl/pypi-py7zr@1.0.0

index.mddocs/

py7zr

A 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.

Package Information

  • Package Name: py7zr
  • Language: Python
  • Installation: pip install py7zr
  • Minimum Python Version: 3.9

Core Imports

import py7zr

Common imports for archive operations:

from py7zr import SevenZipFile, is_7zfile, pack_7zarchive, unpack_7zarchive

Exception classes:

from py7zr import Bad7zFile, PasswordRequired, DecompressionError, UnsupportedCompressionMethodError

Filter and compression constants:

from py7zr import FILTER_LZMA2, FILTER_ZSTD, FILTER_CRYPTO_AES256_SHA256

Basic Usage

Extracting Archives

import 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()

Creating Archives

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')

Utility Functions

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')

Architecture

py7zr is built around several key components:

  • SevenZipFile: Primary interface for reading and writing 7z archives with context manager support
  • Archive Information Classes: Metadata containers (ArchiveInfo, FileInfo) for archive and file details
  • I/O Abstraction Layer: Pluggable I/O system supporting memory, file, and custom backends
  • Callback System: Progress reporting and event handling during operations
  • Filter Chain: Configurable compression and encryption pipeline supporting LZMA, LZMA2, Bzip2, Deflate, ZStandard, Brotli, PPMd, and 7zAES encryption
  • CLI Tools: Command-line interface for common archive operations

Capabilities

Core Archive Operations

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(): ...

Core Archive Operations

Command Line Interface

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)

Command Line Interface

I/O System and Callbacks

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): ...

I/O System and Callbacks

Exception Handling

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): ...

Exception Handling

Compression Algorithms and Filters

py7zr supports multiple compression algorithms and filters:

Compression Methods:

  • LZMA2 (default), LZMA
  • Bzip2, Deflate
  • ZStandard, Brotli, PPMd
  • Copy (no compression)

Filters:

  • Delta filter
  • BCJ filters (x86, ARM, SPARC, PowerPC, IA64, ARM Thumb)

Encryption:

  • 7zAES with SHA256

Constants and Presets

# 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: int

Utility Functions

def 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
    """