File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -718,6 +718,20 @@ def test_absolute_arcnames(self):
718
718
with zipfile .ZipFile (TESTFN2 , "r" , zipfile .ZIP_STORED ) as zipfp :
719
719
self .assertEqual (zipfp .namelist (), ["absolute" ])
720
720
721
+ def test_append (self ):
722
+ # Test that appending to the Zip64 archive doesn't change
723
+ # extra fields of existing entries.
724
+ with zipfile .ZipFile (TESTFN2 , "w" , allowZip64 = True ) as zipfp :
725
+ zipfp .writestr ("strfile" , self .data )
726
+ with zipfile .ZipFile (TESTFN2 , "r" , allowZip64 = True ) as zipfp :
727
+ zinfo = zipfp .getinfo ("strfile" )
728
+ extra = zinfo .extra
729
+ with zipfile .ZipFile (TESTFN2 , "a" , allowZip64 = True ) as zipfp :
730
+ zipfp .writestr ("strfile2" , self .data )
731
+ with zipfile .ZipFile (TESTFN2 , "r" , allowZip64 = True ) as zipfp :
732
+ zinfo = zipfp .getinfo ("strfile" )
733
+ self .assertEqual (zinfo .extra , extra )
734
+
721
735
@requires_zlib
722
736
class DeflateTestZip64InSmallFiles (AbstractTestZip64InSmallFiles ,
723
737
unittest .TestCase ):
Original file line number Diff line number Diff line change @@ -164,6 +164,27 @@ class LargeZipFile(Exception):
164
164
_CD64_DIRECTORY_SIZE = 8
165
165
_CD64_OFFSET_START_CENTDIR = 9
166
166
167
+ _EXTRA_FIELD_STRUCT = struct .Struct ('<HH' )
168
+
169
+ def _strip_extra (extra , xids ):
170
+ # Remove Extra Fields with specified IDs.
171
+ unpack = _EXTRA_FIELD_STRUCT .unpack
172
+ modified = False
173
+ buffer = []
174
+ start = i = 0
175
+ while i + 4 <= len (extra ):
176
+ xid , xlen = unpack (extra [i : i + 4 ])
177
+ j = i + 4 + xlen
178
+ if xid in xids :
179
+ if i != start :
180
+ buffer .append (extra [start : i ])
181
+ start = j
182
+ modified = True
183
+ i = j
184
+ if not modified :
185
+ return extra
186
+ return b'' .join (buffer )
187
+
167
188
def _check_zipfile (fp ):
168
189
try :
169
190
if _EndRecData (fp ):
@@ -1710,6 +1731,7 @@ def _write_end_record(self):
1710
1731
min_version = 0
1711
1732
if extra :
1712
1733
# Append a ZIP64 field to the extra's
1734
+ extra_data = _strip_extra (extra_data , (1 ,))
1713
1735
extra_data = struct .pack (
1714
1736
'<HH' + 'Q' * len (extra ),
1715
1737
1 , 8 * len (extra ), * extra ) + extra_data
Original file line number Diff line number Diff line change
1
+ Appending to the ZIP archive with the ZIP64 extension no longer grows the
2
+ size of extra fields of existing entries.
You can’t perform that action at this time.
0 commit comments