summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2010-10-14 06:59:45 (GMT)
committerGeorg Brandl <georg@python.org>2010-10-14 06:59:45 (GMT)
commit268e4d4cf36ad79e71438fd864160892b335388d (patch)
tree38a3a4341983fa5caeb6dbaf1489587d665bae79 /Lib
parent77658bd9ad81674235f15cf120b195250b5b7c4d (diff)
downloadcpython-268e4d4cf36ad79e71438fd864160892b335388d.zip
cpython-268e4d4cf36ad79e71438fd864160892b335388d.tar.gz
cpython-268e4d4cf36ad79e71438fd864160892b335388d.tar.bz2
#1710703: write zipfile structures also in the case of closing a new, but empty, archive.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_zipfile.py25
-rw-r--r--Lib/zipfile.py27
2 files changed, 47 insertions, 5 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 380e63b..c02d873 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -959,6 +959,31 @@ class OtherTests(unittest.TestCase):
def test_read_return_size_deflated(self):
self.check_read_return_size(zipfile.ZIP_DEFLATED)
+ 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
+ zipf = zipfile.ZipFile(TESTFN, mode="w")
+ zipf.close()
+ try:
+ zipf = zipfile.ZipFile(TESTFN, mode="r")
+ except zipfile.BadZipFile:
+ 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)
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index bcdb2b8..ad04cca 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -167,7 +167,13 @@ def _EndRecData64(fpin, offset, endrec):
"""
Read the ZIP64 end-of-archive records and use that to update endrec
"""
- fpin.seek(offset - sizeEndCentDir64Locator, 2)
+ try:
+ fpin.seek(offset - sizeEndCentDir64Locator, 2)
+ except IOError:
+ # If the seek fails, the file is not large enough to contain a ZIP64
+ # end-of-archive record, so just return the end record we were given.
+ return endrec
+
data = fpin.read(sizeEndCentDir64Locator)
sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)
if sig != stringEndArchive64Locator:
@@ -705,14 +711,22 @@ class ZipFile:
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()
@@ -739,7 +753,10 @@ class ZipFile:
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: