@@ -772,6 +772,7 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
772772 # set the modified flag so central directory gets written
773773 # even if no files are added to the archive
774774 self ._didModify = True
775+ self ._start_disk = self .fp .tell ()
775776 elif key == 'a' :
776777 try :
777778 # See if file is a zip file
@@ -785,6 +786,7 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
785786 # set the modified flag so central directory gets written
786787 # even if no files are added to the archive
787788 self ._didModify = True
789+ self ._start_disk = self .fp .tell ()
788790 else :
789791 raise RuntimeError ('Mode must be "r", "w" or "a"' )
790792 except :
@@ -815,17 +817,18 @@ def _RealGetContents(self):
815817 offset_cd = endrec [_ECD_OFFSET ] # offset of central directory
816818 self ._comment = endrec [_ECD_COMMENT ] # archive comment
817819
818- # "concat" is zero, unless zip was concatenated to another file
819- concat = endrec [_ECD_LOCATION ] - size_cd - offset_cd
820+ # self._start_disk: Position of the start of ZIP archive
821+ # It is zero, unless ZIP was concatenated to another file
822+ self ._start_disk = endrec [_ECD_LOCATION ] - size_cd - offset_cd
820823 if endrec [_ECD_SIGNATURE ] == stringEndArchive64 :
821824 # If Zip64 extension structures are present, account for them
822- concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator )
825+ self . _start_disk -= (sizeEndCentDir64 + sizeEndCentDir64Locator )
823826
824827 if self .debug > 2 :
825- inferred = concat + offset_cd
826- print "given, inferred, offset" , offset_cd , inferred , concat
828+ inferred = self . _start_disk + offset_cd
829+ print "given, inferred, offset" , offset_cd , inferred , self . _start_disk
827830 # self.start_dir: Position of start of central directory
828- self .start_dir = offset_cd + concat
831+ self .start_dir = offset_cd + self . _start_disk
829832 fp .seek (self .start_dir , 0 )
830833 data = fp .read (size_cd )
831834 fp = cStringIO .StringIO (data )
@@ -855,7 +858,7 @@ def _RealGetContents(self):
855858 t >> 11 , (t >> 5 )& 0x3F , (t & 0x1F ) * 2 )
856859
857860 x ._decodeExtra ()
858- x .header_offset = x .header_offset + concat
861+ x .header_offset = x .header_offset + self . _start_disk
859862 x .filename = x ._decodeFilename ()
860863 self .filelist .append (x )
861864 self .NameToInfo [x .filename ] = x
@@ -1198,7 +1201,7 @@ def write(self, filename, arcname=None, compress_type=None):
11981201 raise RuntimeError ('Compressed size larger than uncompressed size' )
11991202 # Seek backwards and write file header (which will now include
12001203 # correct CRC and file sizes)
1201- position = self .fp .tell () # Preserve current position in file
1204+ position = self .fp .tell () # Preserve current position in file
12021205 self .fp .seek (zinfo .header_offset , 0 )
12031206 self .fp .write (zinfo .FileHeader (zip64 ))
12041207 self .fp .seek (position , 0 )
@@ -1284,11 +1287,10 @@ def close(self):
12841287 file_size = zinfo .file_size
12851288 compress_size = zinfo .compress_size
12861289
1287- if zinfo .header_offset > ZIP64_LIMIT :
1288- extra .append (zinfo .header_offset )
1290+ header_offset = zinfo .header_offset - self ._start_disk
1291+ if header_offset > ZIP64_LIMIT :
1292+ extra .append (header_offset )
12891293 header_offset = 0xffffffffL
1290- else :
1291- header_offset = zinfo .header_offset
12921294
12931295 extra_data = zinfo .extra
12941296 if extra :
@@ -1332,7 +1334,7 @@ def close(self):
13321334 # Write end-of-zip-archive record
13331335 centDirCount = len (self .filelist )
13341336 centDirSize = pos2 - pos1
1335- centDirOffset = pos1
1337+ centDirOffset = pos1 - self . _start_disk
13361338 requires_zip64 = None
13371339 if centDirCount > ZIP_FILECOUNT_LIMIT :
13381340 requires_zip64 = "Files count"
0 commit comments