cpython: 64a19fe3a16a (original) (raw)
Mercurial > cpython
changeset 104352:64a19fe3a16a
Issue #26293: Fixed writing ZIP files that starts not from the start of the file. Offsets in ZIP file now are relative to the start of the archive in conforming to the specification. [#26293]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Fri, 07 Oct 2016 22:25:05 +0300 |
parents | def217aaad2f(current diff)b06e75ae1981(diff) |
children | a60d0e80cc1d |
files | Lib/zipfile.py Misc/NEWS |
diffstat | 3 files changed, 62 insertions(+), 15 deletions(-)[+] [-] Lib/test/test_zipfile.py 43 Lib/zipfile.py 30 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -420,6 +420,49 @@ class StoredTestsWithSourceFile(Abstract f.seek(len(data)) with zipfile.ZipFile(f, "r") as zipfp: self.assertEqual(zipfp.namelist(), [TESTFN])
self.assertEqual(zipfp.read(TESTFN), self.data)[](#l1.7)
with open(TESTFN2, 'rb') as f:[](#l1.8)
self.assertEqual(f.read(len(data)), data)[](#l1.9)
zipfiledata = f.read()[](#l1.10)
with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp:[](#l1.11)
self.assertEqual(zipfp.namelist(), [TESTFN])[](#l1.12)
self.assertEqual(zipfp.read(TESTFN), self.data)[](#l1.13)
- def test_read_concatenated_zip_file(self):
with io.BytesIO() as bio:[](#l1.16)
with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp:[](#l1.17)
zipfp.write(TESTFN, TESTFN)[](#l1.18)
zipfiledata = bio.getvalue()[](#l1.19)
data = b'I am not a ZipFile!'*10[](#l1.20)
with open(TESTFN2, 'wb') as f:[](#l1.21)
f.write(data)[](#l1.22)
f.write(zipfiledata)[](#l1.23)
with zipfile.ZipFile(TESTFN2) as zipfp:[](#l1.25)
self.assertEqual(zipfp.namelist(), [TESTFN])[](#l1.26)
self.assertEqual(zipfp.read(TESTFN), self.data)[](#l1.27)
- def test_append_to_concatenated_zip_file(self):
with io.BytesIO() as bio:[](#l1.30)
with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp:[](#l1.31)
zipfp.write(TESTFN, TESTFN)[](#l1.32)
zipfiledata = bio.getvalue()[](#l1.33)
data = b'I am not a ZipFile!'*1000000[](#l1.34)
with open(TESTFN2, 'wb') as f:[](#l1.35)
f.write(data)[](#l1.36)
f.write(zipfiledata)[](#l1.37)
with zipfile.ZipFile(TESTFN2, 'a') as zipfp:[](#l1.39)
self.assertEqual(zipfp.namelist(), [TESTFN])[](#l1.40)
zipfp.writestr('strfile', self.data)[](#l1.41)
with open(TESTFN2, 'rb') as f:[](#l1.43)
self.assertEqual(f.read(len(data)), data)[](#l1.44)
zipfiledata = f.read()[](#l1.45)
with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp:[](#l1.46)
self.assertEqual(zipfp.namelist(), [TESTFN, 'strfile'])[](#l1.47)
self.assertEqual(zipfp.read(TESTFN), self.data)[](#l1.48)
self.assertEqual(zipfp.read('strfile'), self.data)[](#l1.49)
def test_ignores_newline_at_end(self): with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
--- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -1103,10 +1103,10 @@ class ZipFile: # even if no files are added to the archive self._didModify = True try:
self.start_dir = self.fp.tell()[](#l2.7)
self.start_dir = self._start_disk = self.fp.tell()[](#l2.8) except (AttributeError, OSError):[](#l2.9) self.fp = _Tellable(self.fp)[](#l2.10)
self.start_dir = 0[](#l2.11)
self.start_dir = self._start_disk = 0[](#l2.12) self._seekable = False[](#l2.13) else:[](#l2.14) # Some file-like objects can provide tell() but not seek()[](#l2.15)
@@ -1127,7 +1127,7 @@ class ZipFile: # set the modified flag so central directory gets written # even if no files are added to the archive self._didModify = True
self.start_dir = self.fp.tell()[](#l2.20)
self.start_dir = self._start_disk = self.fp.tell()[](#l2.21) else:[](#l2.22) raise ValueError("Mode must be 'r', 'w', 'x', or 'a'")[](#l2.23) except:[](#l2.24)
@@ -1171,17 +1171,18 @@ class ZipFile: offset_cd = endrec[_ECD_OFFSET] # offset of central directory self._comment = endrec[_ECD_COMMENT] # archive comment
# "concat" is zero, unless zip was concatenated to another file[](#l2.29)
concat = endrec[_ECD_LOCATION] - size_cd - offset_cd[](#l2.30)
# self._start_disk: Position of the start of ZIP archive[](#l2.31)
# It is zero, unless ZIP was concatenated to another file[](#l2.32)
self._start_disk = endrec[_ECD_LOCATION] - size_cd - offset_cd[](#l2.33) if endrec[_ECD_SIGNATURE] == stringEndArchive64:[](#l2.34) # If Zip64 extension structures are present, account for them[](#l2.35)
concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator)[](#l2.36)
self._start_disk -= (sizeEndCentDir64 + sizeEndCentDir64Locator)[](#l2.37)
inferred = concat + offset_cd[](#l2.40)
print("given, inferred, offset", offset_cd, inferred, concat)[](#l2.41)
inferred = self._start_disk + offset_cd[](#l2.42)
print("given, inferred, offset", offset_cd, inferred, self._start_disk)[](#l2.43) # self.start_dir: Position of start of central directory[](#l2.44)
self.start_dir = offset_cd + concat[](#l2.45)
self.start_dir = offset_cd + self._start_disk[](#l2.46) fp.seek(self.start_dir, 0)[](#l2.47) data = fp.read(size_cd)[](#l2.48) fp = io.BytesIO(data)[](#l2.49)
@@ -1221,7 +1222,7 @@ class ZipFile: t>>11, (t>>5)&0x3F, (t&0x1F) * 2 ) x._decodeExtra()
x.header_offset = x.header_offset + concat[](#l2.54)
x.header_offset = x.header_offset + self._start_disk[](#l2.55) self.filelist.append(x)[](#l2.56) self.NameToInfo[x.filename] = x[](#l2.57)
@@ -1685,11 +1686,10 @@ class ZipFile: file_size = zinfo.file_size compress_size = zinfo.compress_size
if zinfo.header_offset > ZIP64_LIMIT:[](#l2.63)
extra.append(zinfo.header_offset)[](#l2.64)
header_offset = zinfo.header_offset - self._start_disk[](#l2.65)
if header_offset > ZIP64_LIMIT:[](#l2.66)
extra.append(header_offset)[](#l2.67) header_offset = 0xffffffff[](#l2.68)
else:[](#l2.69)
header_offset = zinfo.header_offset[](#l2.70)
extra_data = zinfo.extra min_version = 0 @@ -1736,7 +1736,7 @@ class ZipFile: # Write end-of-zip-archive record centDirCount = len(self.filelist) centDirSize = pos2 - self.start_dir
centDirOffset = self.start_dir[](#l2.78)
centDirOffset = self.start_dir - self._start_disk[](#l2.79) requires_zip64 = None[](#l2.80) if centDirCount > ZIP_FILECOUNT_LIMIT:[](#l2.81) requires_zip64 = "Files count"[](#l2.82)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -64,6 +64,10 @@ Core and Builtins Library ------- +- Issue #26293: Fixed writing ZIP files that starts not from the start of the