CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-py7zr

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

Pending
Overview
Eval results
Files

cli.mddocs/

Command Line Interface

py7zr provides comprehensive command-line tools for working with 7z archives, supporting all major archive operations including create, extract, list, test, and info commands. The CLI supports password protection, multi-volume archives, and various compression options.

Capabilities

CLI Entry Point

Main entry point for command-line operations.

def main():
    """
    Main CLI entry point function.
    
    Parses command line arguments and executes appropriate archive operations.
    Called when running 'py7zr' command or 'python -m py7zr'.
    """

Command Overview

The py7zr CLI supports the following commands:

  • c: Create archive with files and directories
  • x: Extract files from archive with full paths
  • l: List contents of archive
  • t: Test integrity of archive
  • i: Show information about supported formats

Command Usage

Create Archive (c)

Create new 7z archives from files and directories.

# Create archive from directory
py7zr c archive.7z /path/to/directory

# Create archive from multiple files
py7zr c archive.7z file1.txt file2.txt directory/

# Create archive with password
py7zr c -P archive.7z /path/to/directory
password?: ****

# Create multi-volume archive (500KB volumes)
py7zr c -v 500k archive.7z /path/to/large_directory

# Create archive with compression level
py7zr c -l 9 archive.7z /path/to/directory

# Create archive excluding files
py7zr c -x '*.tmp' archive.7z /path/to/directory

Options:

  • -P: Prompt for password to encrypt archive
  • -v SIZE: Create multi-volume archive with specified volume size (k/m/g suffixes)
  • -l LEVEL: Set compression level (0-9, default varies by algorithm)
  • -x PATTERN: Exclude files matching pattern

Extract Archive (x)

Extract files from 7z archives with full directory paths.

# Extract entire archive to current directory
py7zr x archive.7z

# Extract to specific directory
py7zr x archive.7z -o /tmp/extracted

# Extract password-protected archive
py7zr x -P archive.7z
password?: ****

# Extract specific files only
py7zr x archive.7z file1.txt directory/file2.txt

# Extract with overwrite confirmation
py7zr x -y archive.7z

# Extract without creating subdirectories (flatten)
py7zr x -j archive.7z

Options:

  • -o PATH: Extract to specified output directory
  • -P: Prompt for password for encrypted archives
  • -y: Assume yes to all prompts (overwrite existing files)
  • -j: Extract without directory structure (junk paths)

List Contents (l)

Display contents and information about files in 7z archives.

# List all files in archive
py7zr l archive.7z

# List with detailed information
py7zr l -v archive.7z

# List specific files/patterns
py7zr l archive.7z '*.txt'

# List password-protected archive
py7zr l -P archive.7z
password?: ****

Options:

  • -v: Verbose listing with detailed file information
  • -P: Prompt for password for encrypted archives

Output Format:

Date      Time    Attr         Size   Compressed  Name
--------- -----  ----------  -------  ----------  --------
2023-01-15 10:30  .....A....     1024        512  readme.txt
2023-01-15 10:31  D.....A....        0          0  docs/
2023-01-15 10:31  .....A....     2048       1024  docs/manual.pdf
--------- -----  ----------  -------  ----------  --------
                                3072       1536  3 files, 1 folders

Test Archive (t)

Verify integrity of 7z archives by testing decompression without extracting files.

# Test entire archive
py7zr t archive.7z

# Test password-protected archive
py7zr t -P archive.7z
password?: ****

# Test specific files
py7zr t archive.7z file1.txt directory/

Options:

  • -P: Prompt for password for encrypted archives

Output:

Testing archive: archive.7z

Extracting  readme.txt     OK
Extracting  docs/manual.pdf     OK

Everything is Ok

Archives: 1
Files: 2
Size:       3072
Compressed: 1536

Show Information (i)

Display information about supported compression methods, filters, and formats.

# Show all supported formats and methods
py7zr i

# Show version information
py7zr --version

Output Example:

7-Zip [64] 21.07 : py7zr [Python] 0.20.0

Formats:
 C  7z

Methods:
 C  LZMA2   LZMA   BZip2   Deflate   Copy   ZStandard   Brotli   PPMd
 E  7zAES

Filters:
 C  Delta   BCJ(x86)   BCJ(ARM)   BCJ(ARMT)   BCJ(PPC)   BCJ(SPARC)   BCJ(IA64)

Append to Archive (a)

Add files to existing archives (creates new archive if doesn't exist).

# Append files to existing archive
py7zr a archive.7z new_file.txt new_directory/

# Append with password (for encrypted archives)
py7zr a -P archive.7z additional_files/
password?: ****

Options:

  • -P: Prompt for password for encrypted archives

Global Options

Options that work with multiple commands:

  • --help: Show help message and exit
  • --version: Show version information and exit
  • -v: Verbose output (command-specific behavior)
  • -P: Prompt for password
  • -o PATH: Output directory (for extraction)
  • -y: Assume yes to all prompts

Usage Examples

Complete Workflow Examples

# Create backup archive
py7zr c -l 6 backup_$(date +%Y%m%d).7z ~/Documents ~/Pictures

# Create encrypted backup
py7zr c -P secure_backup.7z ~/sensitive_data
password?: my_secure_password

# Extract and verify
py7zr t backup.7z
py7zr x backup.7z -o ~/restored_backup

# Multi-volume archive for large datasets
py7zr c -v 1g large_dataset.7z ~/big_data_directory

# List and selective extraction
py7zr l archive.7z
py7zr x archive.7z 'docs/*.pdf' 'config/*.json'

Integration with Shell Scripts

#!/bin/bash
# Backup script example

BACKUP_DIR="/backups"
SOURCE_DIR="/home/user/important_data"
DATE=$(date +%Y%m%d_%H%M%S)
ARCHIVE="$BACKUP_DIR/backup_$DATE.7z"

# Create compressed backup
py7zr c -l 9 "$ARCHIVE" "$SOURCE_DIR"

# Verify backup integrity
if py7zr t "$ARCHIVE"; then
    echo "Backup created successfully: $ARCHIVE"
else
    echo "Backup verification failed!"
    exit 1
fi

# Clean old backups (keep last 7 days)
find "$BACKUP_DIR" -name "backup_*.7z" -mtime +7 -delete

Error Handling

The CLI returns appropriate exit codes:

  • 0: Success
  • 1: Warning (some files couldn't be processed)
  • 2: Fatal error
  • 7: Command line error
  • 8: Not enough memory for operation
  • 255: User interrupted operation

Common error scenarios:

# Handle password-protected archives
py7zr x secure.7z || echo "Extraction failed - check password"

# Check if archive exists before operations
if py7zr t archive.7z 2>/dev/null; then
    py7zr x archive.7z
else
    echo "Archive is corrupted or doesn't exist"
fi

Environment Variables

py7zr CLI respects the following environment variables:

  • PY7ZR_PASSWORD: Default password for encrypted archives (use with caution)
  • TMPDIR: Directory for temporary files during operations

Performance Considerations

  • Use appropriate compression levels (-l 1 for speed, -l 9 for compression)
  • Multi-volume archives (-v) can improve handling of very large datasets
  • Consider available memory when working with large archives
  • Solid archives provide better compression but slower selective extraction

Install with Tessl CLI

npx tessl i tessl/pypi-py7zr

docs

cli.md

core-operations.md

exceptions.md

index.md

io-callbacks.md

tile.json