@@ -1769,13 +1769,9 @@ def test_write_unicode_filenames(self):
1769
1769
self .assertEqual (zf .filelist [0 ].filename , "foo.txt" )
1770
1770
self .assertEqual (zf .filelist [1 ].filename , "\xf6 .txt" )
1771
1771
1772
- @requires_zlib ()
1773
- def test_read_zipfile_containing_unicode_path_extra_field (self ):
1772
+ def create_zipfile_with_extra_data (self , filename , extra_data_name ):
1774
1773
with zipfile .ZipFile (TESTFN , mode = 'w' ) as zf :
1775
- # create a file with a non-ASCII name
1776
- filename = '이름.txt'
1777
- filename_encoded = filename .encode ('utf-8' )
1778
-
1774
+ filename_encoded = filename .encode ("utf-8" )
1779
1775
# create a ZipInfo object with Unicode path extra field
1780
1776
zip_info = zipfile .ZipInfo (filename )
1781
1777
@@ -1785,17 +1781,32 @@ def test_read_zipfile_containing_unicode_path_extra_field(self):
1785
1781
import zlib
1786
1782
filename_crc = struct .pack ('<L' , zlib .crc32 (filename_encoded ))
1787
1783
1788
- extra_data = version_of_unicode_path + filename_crc + filename_encoded
1784
+ extra_data = version_of_unicode_path + filename_crc + extra_data_name
1789
1785
tsize = len (extra_data ).to_bytes (2 , 'little' )
1790
1786
1791
1787
zip_info .extra = tag_for_unicode_path + tsize + extra_data
1792
1788
1793
1789
# add the file to the ZIP archive
1794
1790
zf .writestr (zip_info , b'Hello World!' )
1795
1791
1792
+ @requires_zlib ()
1793
+ def test_read_zipfile_containing_unicode_path_extra_field (self ):
1794
+ self .create_zipfile_with_extra_data ("이름.txt" , "이름.txt" .encode ("utf-8" ))
1796
1795
with zipfile .ZipFile (TESTFN , "r" ) as zf :
1797
1796
self .assertEqual (zf .filelist [0 ].filename , "이름.txt" )
1798
1797
1798
+ @requires_zlib ()
1799
+ def test_read_zipfile_warning (self ):
1800
+ self .create_zipfile_with_extra_data ("이름.txt" , b"" )
1801
+ with self .assertWarns (UserWarning ):
1802
+ zipfile .ZipFile (TESTFN , "r" ).close ()
1803
+
1804
+ @requires_zlib ()
1805
+ def test_read_zipfile_error (self ):
1806
+ self .create_zipfile_with_extra_data ("이름.txt" , b"\xff " )
1807
+ with self .assertRaises (zipfile .BadZipfile ):
1808
+ zipfile .ZipFile (TESTFN , "r" ).close ()
1809
+
1799
1810
def test_read_after_write_unicode_filenames (self ):
1800
1811
with zipfile .ZipFile (TESTFN2 , 'w' ) as zipfp :
1801
1812
zipfp .writestr ('приклад' , b'sample' )
0 commit comments