Squashfs image parser
Slowing down. Last release over 3 years ago, little activity since. Fine for existing code; think twice for a new project.
pip install pysquashfsimagePySquashfsImage is a lightweight library and tool for reading and extracting squashfs 4.0 little endian images in Python. It provides a high-level API for easy access to these file systems. It is compatible with Python 2.7 and Python 3.1+.
Installation
pip install PySquashfsImage
Compression
Supported compression methods:
- Gzip
- LZ4 (requires Python 3.7+)
- LZO (requires Python 2.7 or 3.5+, also see here)
- XZ (included in Python 3.3+)
- Zstandard (requires Python 3.7+)
Some of them require a third-party library that you'll need to install separately if needed.
Use as a library
List all elements in the image:
from PySquashfsImage import SquashFsImage
image = SquashFsImage.from_file('/path/to/my/image.img')
for item in image:
print(item.name)
image.close()
Print all entries with their absolute path:
from PySquashfsImage import SquashFsImage
# Use with a context manager (recommended).
with SquashFsImage.from_file('/path/to/my/image.img') as image:
for file in image:
print(file.path)
Print all entries that aren't directories:
from PySquashfsImage import SquashFsImage
with open('/path/to/my/image.img', "rb") as f:
imgbytes = f.read()
# Create an image from bytes.
with SquashFsImage.from_bytes(imgbytes) as image:
for item in image:
if not item.is_dir:
print(item.path)
Save the content of a file:
from PySquashfsImage import SquashFsImage
from PySquashfsImage.extract import extract_file
with SquashFsImage.from_file('/path/to/my/image.img') as image:
myfile = image.find('myfilename')
if myfile is not None:
with open('/tmp/' + myfile.name, 'wb') as f:
print('Saving original ' + myfile.path + ' in /tmp/' + myfile.name)
f.write(myfile.read_bytes())
# If the file is large it's preferable to iterate over its content.
hugefile = image.select("/hugedir/myhugefile.big")
with open("myhugefile.big", "wb") as f:
for block in hugefile.iter_bytes():
f.write(block)
# Or use extract_file(), which preserves the file's metadata (except extended attributes).
extract_file(myfile, "myextractedfile")