Library contents

Python ZipExtFile.close

Release a member stream while keeping its parent archive available.

A simple example

ZipExtFile.close

  1. Start with One open archive member
  2. Do this Close just that member
  3. Result Entry stream closed
Finishing one entry allows the program to move on within the archive.

Syntax

Python syntax
entry.close()
Run these examples in Python

Save one example at a time as lesson.py. Use Python 3.9 or later.

python3 lesson.py

Close one receipt inside an archive

An archive reader finishes working with an entry or the surrounding archive. Release the appropriate handle at that boundary so processing later receipts does not accumulate open resources.

ResultTrue
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:
    with archive.open("tea.txt") as entry:
        entry.read(1)
    print(entry.closed)
Output
True

The member is closed when its with block ends. The archive remains open until the outer block ends.

Advertisements

Inspect a choir archive

A teacher saves the bytes for the word alto in an uncompressed archive member. The reader finishes its work and closes the member stream. What is the state of that resource?

Givenparts/alto.txt · 4 bytes
Worked resultClosed

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. The reader finishes its work and closes the member stream. What is the state of that resource?

Givenstudio/tag.txt · 5 bytes
Worked resultClosed

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 ZipExtFile.close