Skip to content

zipfile.is_zipfile() raises ValueError for file-like objects with a negative central directory offset #161094

Closed

Description

@gpshead

Bug report

Bug description:

Since 3.14, zipfile.is_zipfile() raises ValueError: negative seek value instead of returning False when given a file-like object such as io.BytesIO whose end of central directory record places the central directory before offset 0. A real file returns False, because the OSError from seek() is caught there; the ValueError also skips restoring the caller's file position.

import io, struct, zipfile

buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
    zf.writestr("a.txt", b"hello")
data = bytearray(buf.getvalue())
struct.pack_into("<L", data, len(data) - 22 + 12, len(data))  # inflate CD size
zipfile.is_zipfile(io.BytesIO(data))  # ValueError: negative seek value -...

Expected False, as for the same bytes on disk. This was introduced by gh-134250 (gh-72680).

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. self-assigned this
    on Nov 1, 2026
  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 1, 2026
  3. added a commit that references this issue on Nov 1, 2026
  4. added 2 commits that reference this issue on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions