diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-09-17 12:36:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-17 12:36:40 (GMT) |
commit | 9bdb7be482aef8f60daa1d36606568a132dcb616 (patch) | |
tree | 4674ee096eb080080b2d0f435e7e6a9e1808f277 /Lib/zipfile.py | |
parent | da8d72c953369b872a12c13f136ada77a786714a (diff) | |
download | cpython-9bdb7be482aef8f60daa1d36606568a132dcb616.zip cpython-9bdb7be482aef8f60daa1d36606568a132dcb616.tar.gz cpython-9bdb7be482aef8f60daa1d36606568a132dcb616.tar.bz2 |
bpo-34341: Fix appending to ZIP archives with the ZIP64 extension. (GH-8683)
Diffstat (limited to 'Lib/zipfile.py')
-rw-r--r-- | Lib/zipfile.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 7f23778..89df90b 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -159,6 +159,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): @@ -1819,6 +1840,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 |