Library contents

Python ZipExtFile.read

Read bytes from a known archive member and decode them as text.

A simple example

ZipExtFile.read

  1. Start with Open member containing AB
  2. Do this Read one byte
  3. Result A
The first byte is A. A subsequent one-byte read would return B.

Syntax

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

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

python3 lesson.py

Read the text of an archived receipt

An office keeps individual receipts inside one archive. Work with the archive and its named entries so a clerk can locate the intended receipt without treating the archive itself as a single receipt.

Resultpaid
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:
        print(entry.read().decode("utf-8"))
Output
paid

The archive stores the UTF-8 bytes of paid. Decoding turns them back into a printable string.

Advertisements

Inspect a choir archive

A teacher saves the bytes for the word alto in an uncompressed archive member. Open the member by name and read its text. What does the teacher see?

Givenparts/alto.txt · 4 bytes
Worked resultalto

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. Open the member by name and read its text. What does the teacher see?

Givenstudio/tag.txt · 5 bytes
Worked resultglaze

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