Library contents

Python ZipInfo.compress_size

Inspect the number of bytes occupied by a member payload inside a ZIP file.

A simple example

ZipInfo.compress_size

  1. Start with A two-byte member stored without compression
  2. Do this Read its compressed data size
  3. Result 2 bytes
The stored method keeps the original two data bytes. Archive headers are counted separately.

Syntax

Python 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.py

Find 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.

Result4
lesson.py
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)
Output
4

ZIP_STORED keeps this four-byte payload unchanged. Directory and header bytes are separate.

Advertisements

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.

Givenparts/alto.txt · 4 bytes
Worked result4

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.

Givenstudio/tag.txt · 5 bytes
Worked result5

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.

Documentation for ZipInfo.compress_size