Skip to content

zipfiles.ZipFile's internal ZipInfo entry cache has no internal bounds #153460

Description

@hzhreal

Bug report

Bug description:

ZIP files with substantially many entries will exhaust memory when initialized using the zipfile module. As far as I know, there is no way to prevent this using the zipfile.ZipFile API.

The following code produces a ZIP file of 427 MB:

import zipfile
import tracemalloc

N = 5_000_000

tracemalloc.start()
try:
    with zipfile.ZipFile("test.zip", "w") as zf:
        for i in range(N):
            zf.writestr(str(i), "")
finally:
    m, _ = tracemalloc.get_traced_memory()
    print(f"{m >> 20} MB")

The following code on the produced ZIP file outputs 2408 MB on my machine:

import zipfile
import tracemalloc

tracemalloc.start()
try:
    with zipfile.ZipFile("test.zip", "r") as zf:
        pass
finally:
    m, _ = tracemalloc.get_traced_memory()
    print(f"{m >> 20} MB")

CPython versions tested on:

CPython main branch, 3.14

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions