Skip to content

ZipFile.open(force_zip64=True) generates inconsistent zip64 extra fields #152486

Description

@danny0838

Bug report

Bug description:

1. force_zip64=True is ignored when writing central directory

Zip64 extra field in the central directory ignores force_zip64=True. It only generates zip64 when the file size, compressed size, or header offset really exceeds zipfile.ZIP64_LIMIT.

import io
import zipfile

fh = io.BytesIO()
with zipfile.ZipFile(fh, 'w') as zh:
    with zh.open('strfile', 'w', force_zip64=True) as zi:
        pass

fh.seek(0)
with zipfile.ZipFile(fh, 'r') as zh:
    zinfo = zh.getinfo('strfile')
    print(zinfo.extra)

The result is b'', while it should be b'\x01\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'.

2. The local file entry may generate duplicated Zip64 extra fields

The local file entry may contain duplicated Zip64 extra fields if its ZipInfo already has one in the extra attribute.

import io
import struct
import zipfile

fh = io.BytesIO()
with zipfile.ZipFile(fh, 'w') as zh:
    zinfo = zipfile.ZipInfo('strfile')
    zinfo.extra = (
        b'\x01\x00\x10\x00'
        b'\x00\x00\x00\x00\x00\x00\x00\x00'
        b'\x00\x00\x00\x00\x00\x00\x00\x00'
    )
    with zh.open(zinfo, 'w', force_zip64=True) as zi:
        pass

fh.seek(0)
fh.seek(zinfo.header_offset)
entry = fh.read(zh.start_dir - zinfo.header_offset - zinfo.compress_size)
header = struct.unpack_from(zipfile.structFileHeader, entry)
extra_bytes = entry[-header[zipfile._FH_EXTRA_FIELD_LENGTH]:]
print(extra_bytes)

The result is double b'\x01\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', while only one should exist.

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions