This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author BoppreH
Recipients BoppreH, paul.moore, serhiy.storchaka, steve.dower, tim.golden, zach.ware
Date 2017-12-04.00:25:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1512347116.02.0.213398074469.issue30213@psf.upfronthosting.co.za>
In-reply-to
Content
I'm not sure this is a duplicate of issue29094. That issue includes random data at the start of the file, while this issue uses the 'ab' mode solely for a creating the file if it doesn't exist (and thus is either empty or already a valid zip file). It's not clear to me why 'wb' should work but not 'ab' if the file was empty/missing to begin with.

[Windows 10, Python 3.6.3, 64 bit] still has the same problem.

Here's a more complete test case, starting with no existing files:

    from zipfile import ZipFile

    # Append mode:         v
    with open('file.zip', 'ab') as f:
        with ZipFile(f, 'a') as zip:
                zip.writestr('file.txt', 'contents')
    with open('file.zip', 'rb') as f:
        with ZipFile(f, 'r') as zip:
                print(zip.read('file.txt'))
                # Fails with "zipfile.BadZipFile: Bad magic number for file header"

    # Write mode:          v
    with open('file.zip', 'wb') as f:
        with ZipFile(f, 'a') as zip:
                zip.writestr('file.txt', 'contents')
    with open('file.zip', 'rb') as f:
        with ZipFile(f, 'r') as zip:
                print(zip.read('file.txt'))
                # Works.
History
Date User Action Args
2017-12-04 00:25:16BoppreHsetrecipients: + BoppreH, paul.moore, tim.golden, zach.ware, serhiy.storchaka, steve.dower
2017-12-04 00:25:16BoppreHsetmessageid: <1512347116.02.0.213398074469.issue30213@psf.upfronthosting.co.za>
2017-12-04 00:25:16BoppreHlinkissue30213 messages
2017-12-04 00:25:15BoppreHcreate