Commit 873c5832 authored by R David Murray's avatar R David Murray

#10694: zipfile now ignores garbage at the end of a zipfile.

Original fix by 'rep', final patch (with tests) by Xuanji Li.
parent 5446f08c
...@@ -335,6 +335,24 @@ class TestsWithSourceFile(unittest.TestCase): ...@@ -335,6 +335,24 @@ class TestsWithSourceFile(unittest.TestCase):
with zipfile.ZipFile(f, "r") as zipfp: with zipfile.ZipFile(f, "r") as zipfp:
self.assertEqual(zipfp.namelist(), [TESTFN]) 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): def test_write_default_name(self):
"""Check that calling ZipFile.write without arcname specified """Check that calling ZipFile.write without arcname specified
produces the expected result.""" produces the expected result."""
......
...@@ -236,10 +236,8 @@ def _EndRecData(fpin): ...@@ -236,10 +236,8 @@ def _EndRecData(fpin):
# found the magic number; attempt to unpack and interpret # found the magic number; attempt to unpack and interpret
recData = data[start:start+sizeEndCentDir] recData = data[start:start+sizeEndCentDir]
endrec = list(struct.unpack(structEndArchive, recData)) endrec = list(struct.unpack(structEndArchive, recData))
comment = data[start+sizeEndCentDir:] commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file
# check that comment length is correct comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize]
if endrec[_ECD_COMMENT_SIZE] == len(comment):
# Append the archive comment and start offset
endrec.append(comment) endrec.append(comment)
endrec.append(maxCommentStart + start) endrec.append(maxCommentStart + start)
......
...@@ -16,6 +16,8 @@ Core and Builtins ...@@ -16,6 +16,8 @@ Core and Builtins
Library Library
------- -------
- Issue #10694: zipfile now ignores garbage at the end of a zipfile.
- Issue #11583: Speed up os.path.isdir on Windows by using GetFileAttributes - Issue #11583: Speed up os.path.isdir on Windows by using GetFileAttributes
instead of os.stat. instead of os.stat.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment