Index: Misc/NEWS =================================================================== --- Misc/NEWS (Revision 83236) +++ Misc/NEWS (Arbeitskopie) @@ -475,6 +475,10 @@ Library ------- +- Issue #1710703: Write structures for an empty ZIP archive when a ZipFile is + created in modes 'a' or 'w' and then closed without adding any files. Raise + BadZipfile (rather than IOError) when opening small non-ZIP files. + - Issue #6630: Allow customizing regex flags when subclassing the string.Template class. Index: Doc/library/zipfile.rst =================================================================== --- Doc/library/zipfile.rst (Revision 83219) +++ Doc/library/zipfile.rst (Arbeitskopie) @@ -129,7 +129,12 @@ .. versionadded:: 3.2 Added the ability to use :class:`ZipFile` as a context manager. + .. versionchanged:: 3.2 + If the file is created with mode ``'a'`` or ``'w'`` and then + :meth:`close`\ d without adding any files to the archive, the appropriate + ZIP structures for an empty archive will be written to the file. + .. method:: ZipFile.close() Close the archive file. You must call :meth:`close` before exiting your program Index: Lib/zipfile.py =================================================================== --- Lib/zipfile.py (Revision 83219) +++ Lib/zipfile.py (Arbeitskopie) @@ -683,14 +683,22 @@ if key == 'r': self._GetContents() elif key == 'w': - pass + # set the modified flag so central directory gets written + # even if no files are added to the archive + self._didModify = True elif key == 'a': - try: # See if file is a zip file + try: + # See if file is a zip file self._RealGetContents() # seek to start of directory and overwrite self.fp.seek(self.start_dir, 0) - except BadZipfile: # file is not a zip file, just append + except BadZipfile: + # file is not a zip file, just append self.fp.seek(0, 2) + + # set the modified flag so central directory gets written + # even if no files are added to the archive + self._didModify = True else: if not self._filePassed: self.fp.close() @@ -717,7 +725,10 @@ def _RealGetContents(self): """Read in the table of contents for the ZIP file.""" fp = self.fp - endrec = _EndRecData(fp) + try: + endrec = _EndRecData(fp) + except IOError: + raise BadZipfile("File is not a zip file") if not endrec: raise BadZipfile("File is not a zip file") if self.debug > 1: Index: Lib/test/test_zipfile.py =================================================================== --- Lib/test/test_zipfile.py (Revision 83219) +++ Lib/test/test_zipfile.py (Arbeitskopie) @@ -875,6 +875,32 @@ with zipfile.ZipFile(TESTFN, mode="r") as zipfr: self.assertEqual(zipfr.comment, comment2) + def test_empty_zipfile(self): + # Check that creating a file in 'w' or 'a' mode and closing without + # adding any files to the archives creates a valid empty ZIP file + import pdb; pdb.set_trace() + zipf = zipfile.ZipFile(TESTFN, mode="w") + zipf.close() + try: + zipf = zipfile.ZipFile(TESTFN, mode="r") + except: + self.fail("Unable to create empty ZIP file in 'w' mode") + + zipf = zipfile.ZipFile(TESTFN, mode="a") + zipf.close() + try: + zipf = zipfile.ZipFile(TESTFN, mode="r") + except: + self.fail("Unable to create empty ZIP file in 'a' mode") + + def test_open_empty_file(self): + # Issue 1710703: Check that opening a file with less than 22 bytes + # raises a BadZipfile exception (rather than the previously unhelpful + # IOError) + f = open(TESTFN, 'w') + f.close() + self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r') + def tearDown(self): unlink(TESTFN) unlink(TESTFN2)