Python ZipInfo.compress_type
Compare the member compression identifier with the standard stored-method constant.
A simple example
ZipInfo.compress_type
- Start with A member added with no compression
- Do this Read the method
- Result Stored
Syntax
info.compress_type == ZIP_STORED
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyIdentify how a receipt was stored
An archive inventory records how each receipt was stored in addition to its filename. Inspect the entry metadata to distinguish the storage method from the original receipt contents.
from io import BytesIO
from zipfile import ZipFile, ZIP_STORED
store = BytesIO()
with ZipFile(store, "w", compression=ZIP_STORED) as archive:
archive.writestr("tea.txt", "paid")
store.seek(0)
with ZipFile(store) as archive:
info = archive.getinfo("tea.txt")
print("stored" if info.compress_type == ZIP_STORED else "compressed")
stored
ZIP_STORED stores the original bytes without compression.
Inspect a choir archive
A teacher saves the bytes for the word alto in an uncompressed archive member. Inspect the compression method recorded for the member. Which method name should the catalog display?
The archive contains one stored member. Its entry metadata and contents can be inspected independently.
Inspect a workshop archive
A tutor saves the bytes for glaze in a second uncompressed archive member. Inspect the compression method recorded for the member. Which method name should the catalog display?
The archive contains one stored member. Its entry metadata and contents can be inspected independently.
What to watch for
For an archive supplied by someone else, check expected member names and size limits before reading large payloads.