Skip to content

Commit e159f14

Browse files
committed
gh-136170: Use earliest zinfo.header_offset as ZipFile.data_offset
1 parent 23caccf commit e159f14

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Lib/test/test_zipfile/test_core.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,12 +3505,28 @@ def test_data_offset_write_with_prefix(self):
35053505
fp.write(b"this is a prefix")
35063506
with zipfile.ZipFile(fp, "w") as zipfp:
35073507
self.assertEqual(zipfp.data_offset, 16)
3508+
fp.seek(0)
3509+
with zipfile.ZipFile(fp) as zipfp:
3510+
self.assertEqual(zipfp.data_offset, 16)
3511+
3512+
def test_data_offset_write_with_prefix_and_entry(self):
3513+
with io.BytesIO() as fp:
3514+
fp.write(b"this is a prefix")
3515+
with zipfile.ZipFile(fp, "w") as zipfp:
3516+
self.assertEqual(zipfp.data_offset, 16)
3517+
zipfp.writestr("test.txt", "content")
3518+
fp.seek(0)
3519+
with zipfile.ZipFile(fp) as zipfp:
3520+
self.assertEqual(zipfp.data_offset, 16)
35083521

35093522
def test_data_offset_append_with_bad_zip(self):
35103523
with io.BytesIO() as fp:
35113524
fp.write(b"this is a prefix")
35123525
with zipfile.ZipFile(fp, "a") as zipfp:
35133526
self.assertEqual(zipfp.data_offset, 16)
3527+
fp.seek(0)
3528+
with zipfile.ZipFile(fp) as zipfp:
3529+
self.assertEqual(zipfp.data_offset, 16)
35143530

35153531
def test_data_offset_write_no_tell(self):
35163532
# The initializer in ZipFile checks if tell raises AttributeError or

Lib/zipfile/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,10 +1535,6 @@ def _RealGetContents(self):
15351535
# self.start_dir: Position of start of central directory
15361536
self.start_dir = offset_cd + concat
15371537

1538-
# store the offset to the beginning of data for the
1539-
# .data_offset property
1540-
self._data_offset = concat
1541-
15421538
if self.start_dir < 0:
15431539
raise BadZipFile("Bad offset for central directory")
15441540
fp.seek(self.start_dir, 0)
@@ -1594,11 +1590,19 @@ def _RealGetContents(self):
15941590
print("total", total)
15951591

15961592
end_offset = self.start_dir
1593+
zinfo = None
15971594
for zinfo in reversed(sorted(self.filelist,
15981595
key=lambda zinfo: zinfo.header_offset)):
15991596
zinfo._end_offset = end_offset
16001597
end_offset = zinfo.header_offset
16011598

1599+
# store the offset to the beginning of data for the
1600+
# .data_offset property
1601+
if zinfo is None:
1602+
self._data_offset = self.start_dir
1603+
else:
1604+
self._data_offset = zinfo.header_offset
1605+
16021606
@property
16031607
def data_offset(self):
16041608
"""The offset to the start of zip data in the file or None if

0 commit comments

Comments
 (0)