Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
# set the modified flag so central directory gets written
# even if no files are added to the archive
self._didModify = True
self._start_disk = 0
elif key == 'a':
try:
# See if file is a zip file
Expand All @@ -786,7 +785,6 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
# set the modified flag so central directory gets written
# even if no files are added to the archive
self._didModify = True
self._start_disk = self.fp.tell()
else:
raise RuntimeError('Mode must be "r", "w" or "a"')
except:
Expand Down Expand Up @@ -817,18 +815,17 @@ def _RealGetContents(self):
offset_cd = endrec[_ECD_OFFSET] # offset of central directory
self._comment = endrec[_ECD_COMMENT] # archive comment

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

if self.debug > 2:
inferred = self._start_disk + offset_cd
print "given, inferred, offset", offset_cd, inferred, self._start_disk
inferred = concat + offset_cd
print "given, inferred, offset", offset_cd, inferred, concat
# self.start_dir: Position of start of central directory
self.start_dir = offset_cd + self._start_disk
self.start_dir = offset_cd + concat
fp.seek(self.start_dir, 0)
data = fp.read(size_cd)
fp = cStringIO.StringIO(data)
Expand Down Expand Up @@ -858,7 +855,7 @@ def _RealGetContents(self):
t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )

x._decodeExtra()
x.header_offset = x.header_offset + self._start_disk
x.header_offset = x.header_offset + concat
x.filename = x._decodeFilename()
self.filelist.append(x)
self.NameToInfo[x.filename] = x
Expand Down Expand Up @@ -1201,7 +1198,7 @@ def write(self, filename, arcname=None, compress_type=None):
raise RuntimeError('Compressed size larger than uncompressed size')
# Seek backwards and write file header (which will now include
# correct CRC and file sizes)
position = self.fp.tell() # Preserve current position in file
position = self.fp.tell() # Preserve current position in file
self.fp.seek(zinfo.header_offset, 0)
self.fp.write(zinfo.FileHeader(zip64))
self.fp.seek(position, 0)
Expand Down Expand Up @@ -1287,10 +1284,11 @@ def close(self):
file_size = zinfo.file_size
compress_size = zinfo.compress_size

header_offset = zinfo.header_offset - self._start_disk
if header_offset > ZIP64_LIMIT:
extra.append(header_offset)
if zinfo.header_offset > ZIP64_LIMIT:
extra.append(zinfo.header_offset)
header_offset = 0xffffffffL
else:
header_offset = zinfo.header_offset

extra_data = zinfo.extra
if extra:
Expand Down Expand Up @@ -1334,7 +1332,7 @@ def close(self):
# Write end-of-zip-archive record
centDirCount = len(self.filelist)
centDirSize = pos2 - pos1
centDirOffset = pos1 - self._start_disk
centDirOffset = pos1
requires_zip64 = None
if centDirCount > ZIP_FILECOUNT_LIMIT:
requires_zip64 = "Files count"
Expand Down
5 changes: 2 additions & 3 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Extension Modules
Library
-------

- Revert bpo-26293 for zipfile breakage. See also bpo-29094.

- bpo-30070: Fixed leaks and crashes in errors handling in the parser module.

- bpo-30061: Fixed crashes in IOBase methods next() and readlines() when
Expand Down Expand Up @@ -91,9 +93,6 @@ Library
leading dots could match related hostnames again (e.g. .b.c matches a.b.c).
Patch by Milan Oberkirch.

- Issue #29094: Offsets in a ZIP file created with extern file object and mode
"w" now are relative to the start of the file.

- Issue #13051: Fixed recursion errors in large or resized
curses.textpad.Textbox. Based on patch by Tycho Andersen.

Expand Down