Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4fbb9db

Browse files
committed
#10694: zipfile now ignores garbage at the end of a zipfile.
Original fix by 'rep', final patch (with tests) by Xuanji Li.
1 parent 0f663d0 commit 4fbb9db

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

Lib/test/test_zipfile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,24 @@ def test_append_to_non_zip_file(self):
351351
with zipfile.ZipFile(f, "r") as zipfp:
352352
self.assertEqual(zipfp.namelist(), [TESTFN])
353353

354+
def test_ignores_newline_at_end(self):
355+
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
356+
zipfp.write(TESTFN, TESTFN)
357+
with open(TESTFN2, 'a') as f:
358+
f.write("\r\n\00\00\00")
359+
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
360+
self.assertIsInstance(zipfp, zipfile.ZipFile)
361+
362+
def test_ignores_stuff_appended_past_comments(self):
363+
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
364+
zipfp.comment = b"this is a comment"
365+
zipfp.write(TESTFN, TESTFN)
366+
with open(TESTFN2, 'a') as f:
367+
f.write("abcdef\r\n")
368+
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
369+
self.assertIsInstance(zipfp, zipfile.ZipFile)
370+
self.assertEqual(zipfp.comment, b"this is a comment")
371+
354372
def test_write_default_name(self):
355373
"""Check that calling ZipFile.write without arcname specified
356374
produces the expected result."""

Lib/zipfile.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,14 @@ def _EndRecData(fpin):
246246
# found the magic number; attempt to unpack and interpret
247247
recData = data[start:start+sizeEndCentDir]
248248
endrec = list(struct.unpack(structEndArchive, recData))
249-
comment = data[start+sizeEndCentDir:]
250-
# check that comment length is correct
251-
if endrec[_ECD_COMMENT_SIZE] == len(comment):
252-
# Append the archive comment and start offset
253-
endrec.append(comment)
254-
endrec.append(maxCommentStart + start)
255-
256-
# Try to read the "Zip64 end of central directory" structure
257-
return _EndRecData64(fpin, maxCommentStart + start - filesize,
258-
endrec)
249+
commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file
250+
comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize]
251+
endrec.append(comment)
252+
endrec.append(maxCommentStart + start)
253+
254+
# Try to read the "Zip64 end of central directory" structure
255+
return _EndRecData64(fpin, maxCommentStart + start - filesize,
256+
endrec)
259257

260258
# Unable to find a valid end of central directory structure
261259
return

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Core and Builtins
2222
Library
2323
-------
2424

25+
- Issue #10694: zipfile now ignores garbage at the end of a zipfile.
26+
2527
- Issue #12283: Fixed regression in smtplib quoting of leading dots in DATA.
2628

2729
- Issue #12168: SysLogHandler now allows NUL termination to be controlled using

0 commit comments

Comments
 (0)