Skip to content

Commit c8faabc

Browse files
[3.5] Revert bpo-26293 for zipfile breakage. See also bpo-29094. (GH-1484). (#1486)
(cherry picked from commit 3763ea8)
1 parent 0c9aa6f commit c8faabc

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

Lib/zipfile.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,6 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True):
10281028
# set the modified flag so central directory gets written
10291029
# even if no files are added to the archive
10301030
self._didModify = True
1031-
self._start_disk = 0
10321031
try:
10331032
self.start_dir = self.fp.tell()
10341033
except (AttributeError, OSError):
@@ -1054,7 +1053,7 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True):
10541053
# set the modified flag so central directory gets written
10551054
# even if no files are added to the archive
10561055
self._didModify = True
1057-
self.start_dir = self._start_disk = self.fp.tell()
1056+
self.start_dir = self.fp.tell()
10581057
else:
10591058
raise RuntimeError("Mode must be 'r', 'w', 'x', or 'a'")
10601059
except:
@@ -1098,18 +1097,17 @@ def _RealGetContents(self):
10981097
offset_cd = endrec[_ECD_OFFSET] # offset of central directory
10991098
self._comment = endrec[_ECD_COMMENT] # archive comment
11001099

1101-
# self._start_disk: Position of the start of ZIP archive
1102-
# It is zero, unless ZIP was concatenated to another file
1103-
self._start_disk = endrec[_ECD_LOCATION] - size_cd - offset_cd
1100+
# "concat" is zero, unless zip was concatenated to another file
1101+
concat = endrec[_ECD_LOCATION] - size_cd - offset_cd
11041102
if endrec[_ECD_SIGNATURE] == stringEndArchive64:
11051103
# If Zip64 extension structures are present, account for them
1106-
self._start_disk -= (sizeEndCentDir64 + sizeEndCentDir64Locator)
1104+
concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator)
11071105

11081106
if self.debug > 2:
1109-
inferred = self._start_disk + offset_cd
1110-
print("given, inferred, offset", offset_cd, inferred, self._start_disk)
1107+
inferred = concat + offset_cd
1108+
print("given, inferred, offset", offset_cd, inferred, concat)
11111109
# self.start_dir: Position of start of central directory
1112-
self.start_dir = offset_cd + self._start_disk
1110+
self.start_dir = offset_cd + concat
11131111
fp.seek(self.start_dir, 0)
11141112
data = fp.read(size_cd)
11151113
fp = io.BytesIO(data)
@@ -1149,7 +1147,7 @@ def _RealGetContents(self):
11491147
t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
11501148

11511149
x._decodeExtra()
1152-
x.header_offset = x.header_offset + self._start_disk
1150+
x.header_offset = x.header_offset + concat
11531151
self.filelist.append(x)
11541152
self.NameToInfo[x.filename] = x
11551153

@@ -1629,10 +1627,11 @@ def _write_end_record(self):
16291627
file_size = zinfo.file_size
16301628
compress_size = zinfo.compress_size
16311629

1632-
header_offset = zinfo.header_offset - self._start_disk
1633-
if header_offset > ZIP64_LIMIT:
1634-
extra.append(header_offset)
1630+
if zinfo.header_offset > ZIP64_LIMIT:
1631+
extra.append(zinfo.header_offset)
16351632
header_offset = 0xffffffff
1633+
else:
1634+
header_offset = zinfo.header_offset
16361635

16371636
extra_data = zinfo.extra
16381637
min_version = 0
@@ -1679,7 +1678,7 @@ def _write_end_record(self):
16791678
# Write end-of-zip-archive record
16801679
centDirCount = len(self.filelist)
16811680
centDirSize = pos2 - self.start_dir
1682-
centDirOffset = self.start_dir - self._start_disk
1681+
centDirOffset = self.start_dir
16831682
requires_zip64 = None
16841683
if centDirCount > ZIP_FILECOUNT_LIMIT:
16851684
requires_zip64 = "Files count"

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Extension Modules
4949
Library
5050
-------
5151

52+
- Revert bpo-26293 for zipfile breakage. See also bpo-29094.
53+
5254
- bpo-30243: Removed the __init__ methods of _json's scanner and encoder.
5355
Misusing them could cause memory leaks or crashes. Now scanner and encoder
5456
objects are completely initialized in the __new__ methods.

0 commit comments

Comments
 (0)