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

Skip to content

Commit 51fcb81

Browse files
committed
Merge #10694: zipfile now ignores garbage at the end of a zipfile.
2 parents 0a9f16b + 4fbb9db commit 51fcb81

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
@@ -345,6 +345,24 @@ def test_append_to_non_zip_file(self):
345345
with zipfile.ZipFile(f, "r") as zipfp:
346346
self.assertEqual(zipfp.namelist(), [TESTFN])
347347

348+
def test_ignores_newline_at_end(self):
349+
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
350+
zipfp.write(TESTFN, TESTFN)
351+
with open(TESTFN2, 'a') as f:
352+
f.write("\r\n\00\00\00")
353+
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
354+
self.assertIsInstance(zipfp, zipfile.ZipFile)
355+
356+
def test_ignores_stuff_appended_past_comments(self):
357+
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
358+
zipfp.comment = b"this is a comment"
359+
zipfp.write(TESTFN, TESTFN)
360+
with open(TESTFN2, 'a') as f:
361+
f.write("abcdef\r\n")
362+
with zipfile.ZipFile(TESTFN2, "r") as zipfp:
363+
self.assertIsInstance(zipfp, zipfile.ZipFile)
364+
self.assertEqual(zipfp.comment, b"this is a comment")
365+
348366
def test_write_default_name(self):
349367
"""Check that calling ZipFile.write without arcname specified
350368
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
@@ -187,6 +187,8 @@ Core and Builtins
187187
Library
188188
-------
189189

190+
- Issue #10694: zipfile now ignores garbage at the end of a zipfile.
191+
190192
- Issue #12283: Fixed regression in smtplib quoting of leading dots in DATA.
191193

192194
- Issue #10424: Argparse now includes the names of the missing required

0 commit comments

Comments
 (0)