diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-09-17 13:08:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-17 13:08:45 (GMT) |
commit | efdf316d23c2bc81f499c46faaba5e1b49509afa (patch) | |
tree | 385f98d44ab18eab3e2401435cbf339feb0f1f3d /Lib/zipfile.py | |
parent | e2c0aea670d603b187733606a4601d77f355bc47 (diff) | |
download | cpython-efdf316d23c2bc81f499c46faaba5e1b49509afa.zip cpython-efdf316d23c2bc81f499c46faaba5e1b49509afa.tar.gz cpython-efdf316d23c2bc81f499c46faaba5e1b49509afa.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>
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 2757ce9..9f88512 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): @@ -1813,6 +1834,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 |