Skip to content

Commit 3763ea8

Browse files
Revert bpo-26293 for zipfile breakage. See also bpo-29094. (#1484)
1 parent a12df7b commit 3763ea8

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

Lib/zipfile.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,6 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True):
11091109
# set the modified flag so central directory gets written
11101110
# even if no files are added to the archive
11111111
self._didModify = True
1112-
self._start_disk = 0
11131112
try:
11141113
self.start_dir = self.fp.tell()
11151114
except (AttributeError, OSError):
@@ -1135,7 +1134,7 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True):
11351134
# set the modified flag so central directory gets written
11361135
# even if no files are added to the archive
11371136
self._didModify = True
1138-
self.start_dir = self._start_disk = self.fp.tell()
1137+
self.start_dir = self.fp.tell()
11391138
else:
11401139
raise ValueError("Mode must be 'r', 'w', 'x', or 'a'")
11411140
except:
@@ -1179,18 +1178,17 @@ def _RealGetContents(self):
11791178
offset_cd = endrec[_ECD_OFFSET] # offset of central directory
11801179
self._comment = endrec[_ECD_COMMENT] # archive comment
11811180

1182-
# self._start_disk: Position of the start of ZIP archive
1183-
# It is zero, unless ZIP was concatenated to another file
1184-
self._start_disk = endrec[_ECD_LOCATION] - size_cd - offset_cd
1181+
# "concat" is zero, unless zip was concatenated to another file
1182+
concat = endrec[_ECD_LOCATION] - size_cd - offset_cd
11851183
if endrec[_ECD_SIGNATURE] == stringEndArchive64:
11861184
# If Zip64 extension structures are present, account for them
1187-
self._start_disk -= (sizeEndCentDir64 + sizeEndCentDir64Locator)
1185+
concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator)
11881186

11891187
if self.debug > 2:
1190-
inferred = self._start_disk + offset_cd
1191-
print("given, inferred, offset", offset_cd, inferred, self._start_disk)
1188+
inferred = concat + offset_cd
1189+
print("given, inferred, offset", offset_cd, inferred, concat)
11921190
# self.start_dir: Position of start of central directory
1193-
self.start_dir = offset_cd + self._start_disk
1191+
self.start_dir = offset_cd + concat
11941192
fp.seek(self.start_dir, 0)
11951193
data = fp.read(size_cd)
11961194
fp = io.BytesIO(data)
@@ -1230,7 +1228,7 @@ def _RealGetContents(self):
12301228
t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
12311229

12321230
x._decodeExtra()
1233-
x.header_offset = x.header_offset + self._start_disk
1231+
x.header_offset = x.header_offset + concat
12341232
self.filelist.append(x)
12351233
self.NameToInfo[x.filename] = x
12361234

@@ -1701,10 +1699,11 @@ def _write_end_record(self):
17011699
file_size = zinfo.file_size
17021700
compress_size = zinfo.compress_size
17031701

1704-
header_offset = zinfo.header_offset - self._start_disk
1705-
if header_offset > ZIP64_LIMIT:
1706-
extra.append(header_offset)
1702+
if zinfo.header_offset > ZIP64_LIMIT:
1703+
extra.append(zinfo.header_offset)
17071704
header_offset = 0xffffffff
1705+
else:
1706+
header_offset = zinfo.header_offset
17081707

17091708
extra_data = zinfo.extra
17101709
min_version = 0
@@ -1751,7 +1750,7 @@ def _write_end_record(self):
17511750
# Write end-of-zip-archive record
17521751
centDirCount = len(self.filelist)
17531752
centDirSize = pos2 - self.start_dir
1754-
centDirOffset = self.start_dir - self._start_disk
1753+
centDirOffset = self.start_dir
17551754
requires_zip64 = None
17561755
if centDirCount > ZIP_FILECOUNT_LIMIT:
17571756
requires_zip64 = "Files count"

Misc/NEWS

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,6 @@ Library
602602
- Issue #28985: Update authorizer constants in sqlite3 module.
603603
Patch by Dingyuan Wang.
604604

605-
- Issue #29094: Offsets in a ZIP file created with extern file object and modes
606-
"w" and "x" now are relative to the start of the file.
607-
608605
- Issue #29079: Prevent infinite loop in pathlib.resolve() on Windows
609606

610607
- Issue #13051: Fixed recursion errors in large or resized
@@ -784,10 +781,6 @@ Library
784781

785782
- Issue #28317: The disassembler now decodes FORMAT_VALUE argument.
786783

787-
- Issue #26293: Fixed writing ZIP files that starts not from the start of the
788-
file. Offsets in ZIP file now are relative to the start of the archive in
789-
conforming to the specification.
790-
791784
- Issue #28380: unittest.mock Mock autospec functions now properly support
792785
assert_called, assert_not_called, and assert_called_once.
793786

0 commit comments

Comments
 (0)