diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-09-17 13:11:42 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-17 13:11:42 (GMT) |
commit | 83a0985165abf340294c10d732840e9d46ba218c (patch) | |
tree | 2eed0286caf4ec50f221037f5afbd241c269b964 | |
parent | 1e7193bd027a5a4aa1a387e3f201a3e465c25f01 (diff) | |
download | cpython-83a0985165abf340294c10d732840e9d46ba218c.zip cpython-83a0985165abf340294c10d732840e9d46ba218c.tar.gz cpython-83a0985165abf340294c10d732840e9d46ba218c.tar.bz2 |
bpo-34341: Fix appending to ZIP archives with the ZIP64 extension. (GH-8683)
(cherry picked from commit 9bdb7be482aef8f60daa1d36606568a132dcb616)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r-- | Lib/test/test_zipfile.py | 14 | ||||
-rw-r--r-- | Lib/zipfile.py | 22 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2018-08-06-11-01-18.bpo-34341.E0b9p2.rst | 2 |
3 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index d09ad96..e62b82e 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -718,6 +718,20 @@ class StoredTestZip64InSmallFiles(AbstractTestZip64InSmallFiles, with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: self.assertEqual(zipfp.namelist(), ["absolute"]) + def test_append(self): + # Test that appending to the Zip64 archive doesn't change + # extra fields of existing entries. + with zipfile.ZipFile(TESTFN2, "w", allowZip64=True) as zipfp: + zipfp.writestr("strfile", self.data) + with zipfile.ZipFile(TESTFN2, "r", allowZip64=True) as zipfp: + zinfo = zipfp.getinfo("strfile") + extra = zinfo.extra + with zipfile.ZipFile(TESTFN2, "a", allowZip64=True) as zipfp: + zipfp.writestr("strfile2", self.data) + with zipfile.ZipFile(TESTFN2, "r", allowZip64=True) as zipfp: + zinfo = zipfp.getinfo("strfile") + self.assertEqual(zinfo.extra, extra) + @requires_zlib class DeflateTestZip64InSmallFiles(AbstractTestZip64InSmallFiles, unittest.TestCase): diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 9164f8a..bc757a3 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -164,6 +164,27 @@ _CD64_NUMBER_ENTRIES_TOTAL = 7 _CD64_DIRECTORY_SIZE = 8 _CD64_OFFSET_START_CENTDIR = 9 +_EXTRA_FIELD_STRUCT = struct.Struct('<HH') + +def _strip_extra(extra, xids): + # Remove Extra Fields with specified IDs. + unpack = _EXTRA_FIELD_STRUCT.unpack + modified = False + buffer = [] + start = i = 0 + while i + 4 <= len(extra): + xid, xlen = unpack(extra[i : i + 4]) + j = i + 4 + xlen + if xid in xids: + if i != start: + buffer.append(extra[start : i]) + start = j + modified = True + i = j + if not modified: + return extra + return b''.join(buffer) + def _check_zipfile(fp): try: if _EndRecData(fp): @@ -1710,6 +1731,7 @@ class ZipFile: min_version = 0 if extra: # Append a ZIP64 field to the extra's + extra_data = _strip_extra(extra_data, (1,)) extra_data = struct.pack( '<HH' + 'Q'*len(extra), 1, 8*len(extra), *extra) + extra_data diff --git a/Misc/NEWS.d/next/Library/2018-08-06-11-01-18.bpo-34341.E0b9p2.rst b/Misc/NEWS.d/next/Library/2018-08-06-11-01-18.bpo-34341.E0b9p2.rst new file mode 100644 index 0000000..46a9cde --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-08-06-11-01-18.bpo-34341.E0b9p2.rst @@ -0,0 +1,2 @@ +Appending to the ZIP archive with the ZIP64 extension no longer grows the +size of extra fields of existing entries. |