Skip to content

Provide a way to skip magic-number search in ZipFile(mode='a') #86760

Open
@BartRobinson

Description

@BartRobinson
BPO 42594

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2020-12-07.22:40:05.720>
labels = ['type-feature', 'library', '3.10']
title = "Provide a way to skip magic-number search in ZipFile(mode='a')"
updated_at = <Date 2020-12-07.22:40:05.720>
user = 'https://bugs.python.org/BartRobinson'

bugs.python.org fields:

activity = <Date 2020-12-07.22:40:05.720>
actor = 'Bart Robinson'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2020-12-07.22:40:05.720>
creator = 'Bart Robinson'
dependencies = []
files = []
hgrepos = []
issue_num = 42594
keywords = []
message_count = 1.0
messages = ['382691']
nosy_count = 1.0
nosy_names = ['Bart Robinson']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue42594'
versions = ['Python 3.10']

Activity

BartRobinson

BartRobinson commented on Dec 7, 2020

@BartRobinson
MannequinAuthor

When a ZipFile object is created with mode='a', the existing file contents are checked for the magic number b"PK\005\006" near the end of the file. If a non-zipfile just happens to contain this magic number, it can confuse the library into assuming the file is a zipfile when it is not. It would be great if ZipFile.__init__() provided a way to skip the magic-number check and force a new central directory to be appended to the file.

This could take the form of an additional named argument like ZipFile.__init__(force_append=True), or an additional character in the mode string, like 'a+'. Either of these options should be backward-compatible with existing code.

Currently, my company has code that uses monkey-patching to work around the lack of this feature in the standard library. We use mode='a' to append metadata to files in existing formats that can contain arbitrary binary data and so occasionally include the magic number.

added
3.10only security fixes
stdlibPython modules in the Lib dir
type-featureA feature request or enhancement
on Dec 7, 2020
transferred this issue fromon Apr 10, 2022
thatch

thatch commented on May 20, 2025

@thatch
Contributor

I suspect #134250 will change this behavior, but without a repro of some sort don't know if it will fix things for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10only security fixesstdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Provide a way to skip magic-number search in ZipFile(mode='a') · Issue #86760 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant