Python ZipInfo.compress_size
Inspect the number of bytes occupied by a member payload inside a ZIP file.
A simple example
ZipInfo.compress_size
- Start with A two-byte member stored without compression
- Do this Read its compressed data size
- Result 2 bytes
Syntax
info.compress_size
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyFind the stored size of a receipt
An office is preparing receipts for transfer and needs to budget storage. Identify which size the program measures, especially when a receipt is also stored inside a compressed archive.
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(info.compress_size)
4
ZIP_STORED keeps this four-byte payload unchanged. Directory and header bytes are separate.
Inspect a choir archive
A teacher saves the bytes for the word alto in an uncompressed archive member. Read the stored byte count for the member. The archive uses the store method, which leaves the member data uncompressed.
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. Read the stored byte count for the member. The archive uses the store method, which leaves the member data uncompressed.
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.