Author Ian.Stevens
Recipients Ian.Stevens, docs@python
Date 2010-12-21.16:30:24
SpamBayes Score 6.92212e-11
Marked as misclassified No
Message-id <1292949028.6.0.314113507916.issue10748@psf.upfronthosting.co.za>
In-reply-to
Content
The zipfile documentation (http://docs.python.org/library/zipfile.html) states:

"If the file is created with mode 'a' or 'w' and then close()d without adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file."

This is not the case, eg.::

    >>> from StringIO import StringIO
    >>> import zipfile
    >>> s = StringIO()
    >>> z = zipfile.ZipFile(s, 'w')
    >>> z.close()
    >>> s.len
    0

The code for zipfile (http://svn.python.org/projects/python/trunk/Lib/zipfile.py) does not support the documentation either. The ending records are written only if ZipFile._didModify is True, and that attribute is only set to True if writestr() or write() are called.

Either the code should be fixed to support writing the ending records on an empty zip, or the documentation should be changed to reflect the existing behaviour.

Test case (for Lib/test/test_zipfile)::

    def test_close_empty_zip_creates_valid_zip(self):
        # Test that close() called on a ZipFile without write creates a valid ZIP.
        zf = zipfile.ZipFile(TESTFN, "w")
        zf.close()
        chk = zipfile.is_zipfile(TESTFN)
        self.assertTrue(chk)
History
Date User Action Args
2010-12-21 16:30:28Ian.Stevenssetrecipients: + Ian.Stevens, docs@python
2010-12-21 16:30:28Ian.Stevenssetmessageid: <1292949028.6.0.314113507916.issue10748@psf.upfronthosting.co.za>
2010-12-21 16:30:24Ian.Stevenslinkissue10748 messages
2010-12-21 16:30:24Ian.Stevenscreate