@@ -1082,6 +1082,159 @@ def test_generated_valid_zip64_extra(self):
1082
1082
self .assertEqual (zinfo .header_offset , expected_header_offset )
1083
1083
self .assertEqual (zf .read (zinfo ), expected_content )
1084
1084
1085
+ def test_force_zip64 (self ):
1086
+ """Test that forcing zip64 extensions correctly notes this in the zip file"""
1087
+
1088
+ # GH-103861 describes an issue where forcing a small file to use zip64
1089
+ # extensions would add a zip64 extra record, but not change the data
1090
+ # sizes to 0xFFFFFFFF to indicate to the extractor that the zip64
1091
+ # record should be read. Additionally, it would not set the required
1092
+ # version to indicate that zip64 extensions are required to extract it.
1093
+ # This test replicates the situation and reads the raw data to specifically ensure:
1094
+ # - The required extract version is always >= ZIP64_VERSION
1095
+ # - The compressed and uncompressed size in the file headers are both
1096
+ # 0xFFFFFFFF (ie. point to zip64 record)
1097
+ # - The zip64 record is provided and has the correct sizes in it
1098
+ # Other aspects of the zip are checked as well, but verifying the above is the main goal.
1099
+ # Because this is hard to verify by parsing the data as a zip, the raw
1100
+ # bytes are checked to ensure that they line up with the zip spec.
1101
+ # The spec for this can be found at: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
1102
+ # The relevent sections for this test are:
1103
+ # - 4.3.7 for local file header
1104
+ # - 4.5.3 for zip64 extra field
1105
+
1106
+ data = io .BytesIO ()
1107
+ with zipfile .ZipFile (data , mode = "w" , allowZip64 = True ) as zf :
1108
+ with zf .open ("text.txt" , mode = "w" , force_zip64 = True ) as zi :
1109
+ zi .write (b"_" )
1110
+
1111
+ zipdata = data .getvalue ()
1112
+
1113
+ # pull out and check zip information
1114
+ (
1115
+ header , vers , os , flags , comp , csize , usize , fn_len ,
1116
+ ex_total_len , filename , ex_id , ex_len , ex_usize , ex_csize , cd_sig
1117
+ ) = struct .unpack ("<4sBBHH8xIIHH8shhQQx4s" , zipdata [:63 ])
1118
+
1119
+ self .assertEqual (header , b"PK\x03 \x04 " ) # local file header
1120
+ self .assertGreaterEqual (vers , zipfile .ZIP64_VERSION ) # requires zip64 to extract
1121
+ self .assertEqual (os , 0 ) # compatible with MS-DOS
1122
+ self .assertEqual (flags , 0 ) # no flags
1123
+ self .assertEqual (comp , 0 ) # compression method = stored
1124
+ self .assertEqual (csize , 0xFFFFFFFF ) # sizes are in zip64 extra
1125
+ self .assertEqual (usize , 0xFFFFFFFF )
1126
+ self .assertEqual (fn_len , 8 ) # filename len
1127
+ self .assertEqual (ex_total_len , 20 ) # size of extra records
1128
+ self .assertEqual (ex_id , 1 ) # Zip64 extra record
1129
+ self .assertEqual (ex_len , 16 ) # 16 bytes of data
1130
+ self .assertEqual (ex_usize , 1 ) # uncompressed size
1131
+ self .assertEqual (ex_csize , 1 ) # compressed size
1132
+ self .assertEqual (cd_sig , b"PK\x01 \x02 " ) # ensure the central directory header is next
1133
+
1134
+ z = zipfile .ZipFile (io .BytesIO (zipdata ))
1135
+ zinfos = z .infolist ()
1136
+ self .assertEqual (len (zinfos ), 1 )
1137
+ self .assertGreaterEqual (zinfos [0 ].extract_version , zipfile .ZIP64_VERSION ) # requires zip64 to extract
1138
+
1139
+ def test_unseekable_zip_unknown_filesize (self ):
1140
+ """Test that creating a zip with/without seeking will raise a RuntimeError if zip64 was required but not used"""
1141
+
1142
+ def make_zip (fp ):
1143
+ with zipfile .ZipFile (fp , mode = "w" , allowZip64 = True ) as zf :
1144
+ with zf .open ("text.txt" , mode = "w" , force_zip64 = False ) as zi :
1145
+ zi .write (b"_" * (zipfile .ZIP64_LIMIT + 1 ))
1146
+
1147
+ self .assertRaises (RuntimeError , make_zip , io .BytesIO ())
1148
+ self .assertRaises (RuntimeError , make_zip , Unseekable (io .BytesIO ()))
1149
+
1150
+ def test_zip64_required_not_allowed_fail (self ):
1151
+ """Test that trying to add a large file to a zip that doesn't allow zip64 extensions fails on add"""
1152
+ def make_zip (fp ):
1153
+ with zipfile .ZipFile (fp , mode = "w" , allowZip64 = False ) as zf :
1154
+ # pretend zipfile.ZipInfo.from_file was used to get the name and filesize
1155
+ info = zipfile .ZipInfo ("text.txt" )
1156
+ info .file_size = zipfile .ZIP64_LIMIT + 1
1157
+ zf .open (info , mode = "w" )
1158
+
1159
+ self .assertRaises (zipfile .LargeZipFile , make_zip , io .BytesIO ())
1160
+ self .assertRaises (zipfile .LargeZipFile , make_zip , Unseekable (io .BytesIO ()))
1161
+
1162
+ def test_unseekable_zip_known_filesize (self ):
1163
+ """Test that creating a zip without seeking will use zip64 extensions if the file size is provided up-front"""
1164
+
1165
+ # This test ensures that the zip will use a zip64 data descriptor (same
1166
+ # as a regular data descriptor except the sizes are 8 bytes instead of
1167
+ # 4) record to communicate the size of a file if the zip is being
1168
+ # written to an unseekable stream.
1169
+ # Because this sort of thing is hard to verify by parsing the data back
1170
+ # in as a zip, this test looks at the raw bytes created to ensure that
1171
+ # the correct data has been generated.
1172
+ # The spec for this can be found at: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
1173
+ # The relevent sections for this test are:
1174
+ # - 4.3.7 for local file header
1175
+ # - 4.3.9 for the data descriptor
1176
+ # - 4.5.3 for zip64 extra field
1177
+
1178
+ file_size = zipfile .ZIP64_LIMIT + 1
1179
+
1180
+ def make_zip (fp ):
1181
+ with zipfile .ZipFile (fp , mode = "w" , allowZip64 = True ) as zf :
1182
+ # pretend zipfile.ZipInfo.from_file was used to get the name and filesize
1183
+ info = zipfile .ZipInfo ("text.txt" )
1184
+ info .file_size = file_size
1185
+ with zf .open (info , mode = "w" , force_zip64 = False ) as zi :
1186
+ zi .write (b"_" * file_size )
1187
+ return fp
1188
+
1189
+ # check seekable file information
1190
+ seekable_data = make_zip (io .BytesIO ()).getvalue ()
1191
+ (
1192
+ header , vers , os , flags , comp , csize , usize , fn_len ,
1193
+ ex_total_len , filename , ex_id , ex_len , ex_usize , ex_csize ,
1194
+ cd_sig
1195
+ ) = struct .unpack ("<4sBBHH8xIIHH8shhQQ{}x4s" .format (file_size ), seekable_data [:62 + file_size ])
1196
+
1197
+ self .assertEqual (header , b"PK\x03 \x04 " ) # local file header
1198
+ self .assertGreaterEqual (vers , zipfile .ZIP64_VERSION ) # requires zip64 to extract
1199
+ self .assertEqual (os , 0 ) # compatible with MS-DOS
1200
+ self .assertEqual (flags , 0 ) # no flags set
1201
+ self .assertEqual (comp , 0 ) # compression method = stored
1202
+ self .assertEqual (csize , 0xFFFFFFFF ) # sizes are in zip64 extra
1203
+ self .assertEqual (usize , 0xFFFFFFFF )
1204
+ self .assertEqual (fn_len , 8 ) # filename len
1205
+ self .assertEqual (ex_total_len , 20 ) # size of extra records
1206
+ self .assertEqual (ex_id , 1 ) # Zip64 extra record
1207
+ self .assertEqual (ex_len , 16 ) # 16 bytes of data
1208
+ self .assertEqual (ex_usize , file_size ) # uncompressed size
1209
+ self .assertEqual (ex_csize , file_size ) # compressed size
1210
+ self .assertEqual (cd_sig , b"PK\x01 \x02 " ) # ensure the central directory header is next
1211
+
1212
+ # check unseekable file information
1213
+ unseekable_data = make_zip (Unseekable (io .BytesIO ())).fp .getvalue ()
1214
+ (
1215
+ header , vers , os , flags , comp , csize , usize , fn_len ,
1216
+ ex_total_len , filename , ex_id , ex_len , ex_usize , ex_csize ,
1217
+ dd_header , dd_usize , dd_csize , cd_sig
1218
+ ) = struct .unpack ("<4sBBHH8xIIHH8shhQQ{}x4s4xQQ4s" .format (file_size ), unseekable_data [:86 + file_size ])
1219
+
1220
+ self .assertEqual (header , b"PK\x03 \x04 " ) # local file header
1221
+ self .assertGreaterEqual (vers , zipfile .ZIP64_VERSION ) # requires zip64 to extract
1222
+ self .assertEqual (os , 0 ) # compatible with MS-DOS
1223
+ self .assertEqual ("{:b}" .format (flags ), "1000" ) # streaming flag set
1224
+ self .assertEqual (comp , 0 ) # compression method = stored
1225
+ self .assertEqual (csize , 0xFFFFFFFF ) # sizes are in zip64 extra
1226
+ self .assertEqual (usize , 0xFFFFFFFF )
1227
+ self .assertEqual (fn_len , 8 ) # filename len
1228
+ self .assertEqual (ex_total_len , 20 ) # size of extra records
1229
+ self .assertEqual (ex_id , 1 ) # Zip64 extra record
1230
+ self .assertEqual (ex_len , 16 ) # 16 bytes of data
1231
+ self .assertEqual (ex_usize , 0 ) # uncompressed size - 0 to defer to data descriptor
1232
+ self .assertEqual (ex_csize , 0 ) # compressed size - 0 to defer to data descriptor
1233
+ self .assertEqual (dd_header , b"PK\07 \x08 " ) # data descriptor
1234
+ self .assertEqual (dd_usize , file_size ) # file size (8 bytes because zip64)
1235
+ self .assertEqual (dd_csize , file_size ) # compressed size (8 bytes because zip64)
1236
+ self .assertEqual (cd_sig , b"PK\x01 \x02 " ) # ensure the central directory header is next
1237
+
1085
1238
1086
1239
@requires_zlib ()
1087
1240
class DeflateTestZip64InSmallFiles (AbstractTestZip64InSmallFiles ,
0 commit comments