0

Read One Inventory ZIP Member Without Extracting It

If an application needs the contents of one known CSV member, it does not need to turn archive member names into filesystem paths. Python's zipfile module can open a member as a stream. The application can keep the content in memory and avoid an extraction step entirely.

For a global dropshipping file handover, an archive may contain both an inventory file and unrelated documents. A warehousing and fulfillment tool should define the exact expected member rather than selecting whichever CSV appears first.

Select an exact member and reject ambiguity

import io
import zipfile


def read_inventory_member(blob, max_bytes=4096):
    if not isinstance(blob, bytes):
        raise TypeError("bytes required")
    if type(max_bytes) is not int or max_bytes < 1:
        raise ValueError("positive byte limit required")
    with zipfile.ZipFile(io.BytesIO(blob)) as archive:
        matches = [item for item in archive.infolist()
                   if item.filename == "inventory.csv"]
        if len(matches) != 1:
            raise ValueError("exactly one inventory.csv required")
        info = matches[0]
        if info.is_dir() or info.file_size > max_bytes:
            raise ValueError("member exceeds contract")
        with archive.open(info) as member:
            raw = member.read(max_bytes + 1)
        if len(raw) > max_bytes:
            raise ValueError("member exceeds contract")
        return raw.decode("utf-8-sig")


buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w") as archive:
    archive.writestr("inventory.csv", "sku,quantity\n0007,2\n")
print(read_inventory_member(buffer.getvalue()))

The expected name is a literal member name. A nested name such as folder/inventory.csv, a differently cased name or ../inventory.csv does not match it. Duplicate members with the expected name are rejected instead of relying on an ambiguous name lookup.

Keep the limit's scope clear

The size check and bounded read apply to the selected member's uncompressed content. This is not a complete resource-isolation layer for hostile archives. It does not bound the supplied archive's total size, central-directory parsing cost, member count or total decompression CPU. Those need separate limits and an appropriate execution environment before accepting untrusted uploads.

The function also returns CSV text, not validated inventory rows. It does not establish column definitions, quantity rules, stock ownership or authorization to change a warehouse record. Invalid archives and unsupported features can still raise zipfile exceptions; the caller needs an error-handling policy.

Verify names, size and decoding

The exact article code was executed with in-memory archives. Tests cover the expected member, extra unrelated content, missing and nested names, a traversal-shaped member name, duplicate expected names, the size boundary, invalid UTF-8, a leading UTF-8 BOM and invalid argument types. No archive is extracted and no customer data is used.

Python's zipfile documentation describes member access and extraction considerations. Reading one explicitly selected member keeps this parsing step separate from the decision to create files or update inventory.

Computer workstations and printing equipment in a warehouse.

Computer workstations and printing equipment in a warehouse.


All Rights Reserved

Viblo
Let's register a Viblo Account to get more interesting posts.