@@ -1082,6 +1082,25 @@ def test_generated_valid_zip64_extra(self):
1082
1082
1083
1083
def test_force_zip64 (self ):
1084
1084
"""Test that forcing zip64 extensions correctly notes this in the zip file"""
1085
+
1086
+ # GH-103861 describes an issue where forcing a small file to use zip64
1087
+ # extensions would add a zip64 extra record, but not change the data
1088
+ # sizes to 0xFFFFFFFF to indicate to the extractor that the zip64
1089
+ # record should be read. Additionally, it would not set the required
1090
+ # version to indicate that zip64 extensions are required to extract it.
1091
+ # This test replicates the situation and reads the raw data to specifically ensure:
1092
+ # - The required extract version is always >= ZIP64_VERSION
1093
+ # - The compressed and uncompressed size in the file headers are both
1094
+ # 0xFFFFFFFF (ie. point to zip64 record)
1095
+ # - The zip64 record is provided and has the correct sizes in it
1096
+ # Other aspects of the zip are checked as well, but verifying the above is the main goal.
1097
+ # Because this is hard to verify by parsing the data as a zip, the raw
1098
+ # bytes are checked to ensure that they line up with the zip spec.
1099
+ # The spec for this can be found at: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
1100
+ # The relevent sections for this test are:
1101
+ # - 4.3.7 for local file header
1102
+ # - 4.5.3 for zip64 extra field
1103
+
1085
1104
data = io .BytesIO ()
1086
1105
with zipfile .ZipFile (data , mode = "w" , allowZip64 = True ) as zf :
1087
1106
with zf .open ("text.txt" , mode = "w" , force_zip64 = True ) as zi :
@@ -1141,6 +1160,19 @@ def make_zip(fp):
1141
1160
def test_unseekable_zip_known_filesize (self ):
1142
1161
"""Test that creating a zip without seeking will use zip64 extensions if the file size is provided up-front"""
1143
1162
1163
+ # This test ensures that the zip will use a zip64 data descriptor (same
1164
+ # as a regular data descriptor except the sizes are 8 bytes instead of
1165
+ # 4) record to communicate the size of a file if the zip is being
1166
+ # written to an unseekable stream.
1167
+ # Because this sort of thing is hard to verify by parsing the data back
1168
+ # in as a zip, this test looks at the raw bytes created to ensure that
1169
+ # the correct data has been generated.
1170
+ # The spec for this can be found at: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
1171
+ # The relevent sections for this test are:
1172
+ # - 4.3.7 for local file header
1173
+ # - 4.3.9 for the data descriptor
1174
+ # - 4.5.3 for zip64 extra field
1175
+
1144
1176
file_size = zipfile .ZIP64_LIMIT + 1
1145
1177
1146
1178
def make_zip (fp ):
@@ -1160,7 +1192,7 @@ def make_zip(fp):
1160
1192
cd_sig
1161
1193
) = struct .unpack ("<4sBBHH8xIIHH8shhQQ{}x4s" .format (file_size ), seekable_data [:62 + file_size ])
1162
1194
1163
- self .assertEqual (header , b"PK\x03 \x04 " )
1195
+ self .assertEqual (header , b"PK\x03 \x04 " ) # local file header
1164
1196
self .assertGreaterEqual (vers , zipfile .ZIP64_VERSION ) # requires zip64 to extract
1165
1197
self .assertEqual (os , 0 ) # compatible with MS-DOS
1166
1198
self .assertEqual (flags , 0 ) # no flags set
@@ -1183,7 +1215,7 @@ def make_zip(fp):
1183
1215
dd_header , dd_usize , dd_csize , cd_sig
1184
1216
) = struct .unpack ("<4sBBHH8xIIHH8shhQQ{}x4s4xQQ4s" .format (file_size ), unseekable_data [:86 + file_size ])
1185
1217
1186
- self .assertEqual (header , b"PK\x03 \x04 " )
1218
+ self .assertEqual (header , b"PK\x03 \x04 " ) # local file header
1187
1219
self .assertGreaterEqual (vers , zipfile .ZIP64_VERSION ) # requires zip64 to extract
1188
1220
self .assertEqual (os , 0 ) # compatible with MS-DOS
1189
1221
self .assertEqual ("{:b}" .format (flags ), "1000" ) # streaming flag set
0 commit comments