@@ -1950,6 +1950,45 @@ def test_repack_data_descriptor_no_sig(self):
1950
1950
# check file size
1951
1951
self .assertEqual (os .path .getsize (TESTFN ), expected_size )
1952
1952
1953
+ def test_repack_data_descriptor_no_sig_strict (self ):
1954
+ """Should skip data descriptor without signature when `strict_descriptor` is set."""
1955
+ for ii in ([0 ], [0 , 1 ]):
1956
+ with self .subTest (remove = ii ):
1957
+ # calculate the expected results
1958
+ with open (TESTFN , 'wb' ) as fh :
1959
+ with mock .patch ('zipfile.struct.pack' , side_effect = struct_pack_no_dd_sig ):
1960
+ self ._prepare_zip_from_test_files (Unseekable (fh ), self .test_files )
1961
+ with zipfile .ZipFile (TESTFN , 'a' ) as zh :
1962
+ for i in ii :
1963
+ zh .remove (self .test_files [i ][0 ])
1964
+ expected_zinfos = [ComparableZipInfo (zi ) for zi in zh .infolist ()]
1965
+ expected_size = os .path .getsize (TESTFN )
1966
+
1967
+ # do the removal and check the result
1968
+ with open (TESTFN , 'wb' ) as fh :
1969
+ with mock .patch ('zipfile.struct.pack' , side_effect = struct_pack_no_dd_sig ):
1970
+ zinfos = self ._prepare_zip_from_test_files (Unseekable (fh ), self .test_files )
1971
+ with zipfile .ZipFile (TESTFN , 'a' , self .compression ) as zh :
1972
+ # make sure data descriptor bit is really set (by making zipfile unseekable)
1973
+ for zi in zh .infolist ():
1974
+ self .assertTrue (zi .flag_bits & 8 , f'data descriptor flag not set: { zi .filename } ' )
1975
+
1976
+ for i in ii :
1977
+ zh .remove (self .test_files [i ][0 ])
1978
+ zh .repack (strict_descriptor = True )
1979
+
1980
+ # check infolist
1981
+ self .assertEqual (
1982
+ [ComparableZipInfo (zi ) for zi in zh .infolist ()],
1983
+ expected_zinfos ,
1984
+ )
1985
+
1986
+ # make sure the zip file is still valid
1987
+ self .assertIsNone (zh .testzip ())
1988
+
1989
+ # check file size
1990
+ self .assertEqual (os .path .getsize (TESTFN ), expected_size )
1991
+
1953
1992
def test_repack_data_descriptor_no_sig_and_zip64 (self ):
1954
1993
"""Should correctly handle file entries using data descriptor without signature and zip64."""
1955
1994
for ii in ([0 ], [0 , 1 ], [1 ], [2 ]):
0 commit comments