diff options
author | R David Murray <rdmurray@bitdance.com> | 2011-06-09 19:50:51 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2011-06-09 19:50:51 (GMT) |
commit | 4fbb9dbd3421fca6d4c289d996ef035da0cef330 (patch) | |
tree | 3d74ae019fe03d7b5417373fdc1b3b11c17f8391 /Lib | |
parent | 0f663d07e669c39ce9a7ddfa71ed1293379a358e (diff) | |
download | cpython-4fbb9dbd3421fca6d4c289d996ef035da0cef330.zip cpython-4fbb9dbd3421fca6d4c289d996ef035da0cef330.tar.gz cpython-4fbb9dbd3421fca6d4c289d996ef035da0cef330.tar.bz2 |
#10694: zipfile now ignores garbage at the end of a zipfile.
Original fix by 'rep', final patch (with tests) by Xuanji Li.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_zipfile.py | 18 | ||||
-rw-r--r-- | Lib/zipfile.py | 18 |
2 files changed, 26 insertions, 10 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 90aab86..782020c 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -351,6 +351,24 @@ class TestsWithSourceFile(unittest.TestCase): with zipfile.ZipFile(f, "r") as zipfp: self.assertEqual(zipfp.namelist(), [TESTFN]) + def test_ignores_newline_at_end(self): + with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: + zipfp.write(TESTFN, TESTFN) + with open(TESTFN2, 'a') as f: + f.write("\r\n\00\00\00") + with zipfile.ZipFile(TESTFN2, "r") as zipfp: + self.assertIsInstance(zipfp, zipfile.ZipFile) + + def test_ignores_stuff_appended_past_comments(self): + with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: + zipfp.comment = b"this is a comment" + zipfp.write(TESTFN, TESTFN) + with open(TESTFN2, 'a') as f: + f.write("abcdef\r\n") + with zipfile.ZipFile(TESTFN2, "r") as zipfp: + self.assertIsInstance(zipfp, zipfile.ZipFile) + self.assertEqual(zipfp.comment, b"this is a comment") + def test_write_default_name(self): """Check that calling ZipFile.write without arcname specified produces the expected result.""" diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 50f4848..5cc7816 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -246,16 +246,14 @@ def _EndRecData(fpin): # found the magic number; attempt to unpack and interpret recData = data[start:start+sizeEndCentDir] endrec = list(struct.unpack(structEndArchive, recData)) - comment = data[start+sizeEndCentDir:] - # check that comment length is correct - if endrec[_ECD_COMMENT_SIZE] == len(comment): - # Append the archive comment and start offset - endrec.append(comment) - endrec.append(maxCommentStart + start) - - # Try to read the "Zip64 end of central directory" structure - return _EndRecData64(fpin, maxCommentStart + start - filesize, - endrec) + commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file + comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize] + endrec.append(comment) + endrec.append(maxCommentStart + start) + + # Try to read the "Zip64 end of central directory" structure + return _EndRecData64(fpin, maxCommentStart + start - filesize, + endrec) # Unable to find a valid end of central directory structure return |