A Python library for handling Microsoft Cabinet files with zipfile-compatible interface
Python 3.10+ Compatible - This library provides a zipfile-like interface for creating and extracting CAB files, fully compatible with Python 3.10 and later versions.
- zipfile-compatible interface - Use the same methods as Python's zipfile module
- Create and extract CAB files with simple API
- Support for binary and text files
- Unicode filename support
- Context manager support (with statement)
- No external dependencies required
- Python 3.10+ support - Fully compatible with modern Python versions
pip install pycabfile
# or Install from source
pip install git+https://github.com/hanul93/pycabfile.gitThe interface is designed to be identical to Python's zipfile module:
from pycabfile import CabFile
# Create a new CAB file
with CabFile('example.cab', 'w') as cab:
# Add files from disk
cab.write('document.txt', 'docs/document.txt') # arcname is optional
cab.write('image.jpg')
# Add data directly
cab.writestr('readme.txt', 'This is a README file')
cab.writestr('config.json', '{"version": "1.0"}')from pycabfile import CabFile
# Read an existing CAB file
with CabFile('example.cab', 'r') as cab:
# List all files
file_list = cab.namelist()
print(f"Files in CAB: {file_list}")
# Read file content
content = cab.read('readme.txt')
print(content.decode('utf-8'))
# Get file information
info = cab.getinfo('document.txt')
print(f"File size: {info.file_size} bytes")
# Extract files
cab.extract('document.txt', 'output/') # Extract single file
cab.extractall('output/') # Extract all filesfrom pycabfile import CabFile, CabInfo
# Append to existing CAB file
with CabFile('example.cab', 'a') as cab:
cab.writestr('appended.txt', 'This was added later')
# Working with file-like objects
with CabFile('example.cab', 'r') as cab:
with cab.open('document.txt') as f:
data = f.read()
print(data.decode('utf-8'))The API is designed to be a drop-in replacement for zipfile in most cases:
| zipfile | pycabfile |
|---|---|
zipfile.ZipFile('file.zip', 'w') |
pycabfile.CabFile('file.cab', 'w') |
zf.write('file.txt') |
cf.write('file.txt') |
zf.writestr('name', data) |
cf.writestr('name', data) |
zf.read('name') |
cf.read('name') |
zf.namelist() |
cf.namelist() |
zf.extractall() |
cf.extractall() |
Run the test suite:
# Basic functionality test
python test_pycabfile.py
# Run example demo
python example.py- Python 3.10+
- No external dependencies required
- Kei Choi (developer)
MIT License
This library provides a pure Python implementation for handling Microsoft Cabinet files with a zipfile-compatible interface. It implements the CAB format specification from Microsoft to ensure full compatibility with standard CAB files created by other tools.
For more information about the Microsoft Cabinet format: https://msdn.microsoft.com/en-us/library/bb417343.aspx
