@@ -1696,11 +1696,48 @@ def test_bug_6050(self):
1696
1696
os .mkdir (os .path .join (TESTFN2 , "a" ))
1697
1697
self .test_extract_dir ()
1698
1698
1699
- def test_store_dir (self ):
1699
+ def test_write_dir (self ):
1700
+ dirpath = os .path .join (TESTFN2 , "x" )
1701
+ os .mkdir (dirpath )
1702
+ mode = os .stat (dirpath ).st_mode & 0xFFFF
1703
+ with zipfile .ZipFile (TESTFN , "w" ) as zipf :
1704
+ zipf .write (dirpath )
1705
+ zinfo = zipf .filelist [0 ]
1706
+ self .assertTrue (zinfo .filename .endswith ("/x/" ))
1707
+ self .assertEqual (zinfo .external_attr , (mode << 16 ) | 0x10 )
1708
+ zipf .write (dirpath , "y" )
1709
+ zinfo = zipf .filelist [1 ]
1710
+ self .assertTrue (zinfo .filename , "y/" )
1711
+ self .assertEqual (zinfo .external_attr , (mode << 16 ) | 0x10 )
1712
+ with zipfile .ZipFile (TESTFN , "r" ) as zipf :
1713
+ zinfo = zipf .filelist [0 ]
1714
+ self .assertTrue (zinfo .filename .endswith ("/x/" ))
1715
+ self .assertEqual (zinfo .external_attr , (mode << 16 ) | 0x10 )
1716
+ zinfo = zipf .filelist [1 ]
1717
+ self .assertTrue (zinfo .filename , "y/" )
1718
+ self .assertEqual (zinfo .external_attr , (mode << 16 ) | 0x10 )
1719
+ target = os .path .join (TESTFN2 , "target" )
1720
+ os .mkdir (target )
1721
+ zipf .extractall (target )
1722
+ self .assertTrue (os .path .isdir (os .path .join (target , "y" )))
1723
+ self .assertEqual (len (os .listdir (target )), 2 )
1724
+
1725
+ def test_writestr_dir (self ):
1700
1726
os .mkdir (os .path .join (TESTFN2 , "x" ))
1701
- zipf = zipfile .ZipFile (TESTFN , "w" )
1702
- zipf .write (os .path .join (TESTFN2 , "x" ), "x" )
1703
- self .assertTrue (zipf .filelist [0 ].filename .endswith ("x/" ))
1727
+ with zipfile .ZipFile (TESTFN , "w" ) as zipf :
1728
+ zipf .writestr ("x/" , b'' )
1729
+ zinfo = zipf .filelist [0 ]
1730
+ self .assertEqual (zinfo .filename , "x/" )
1731
+ self .assertEqual (zinfo .external_attr , (0o40775 << 16 ) | 0x10 )
1732
+ with zipfile .ZipFile (TESTFN , "r" ) as zipf :
1733
+ zinfo = zipf .filelist [0 ]
1734
+ self .assertTrue (zinfo .filename .endswith ("x/" ))
1735
+ self .assertEqual (zinfo .external_attr , (0o40775 << 16 ) | 0x10 )
1736
+ target = os .path .join (TESTFN2 , "target" )
1737
+ os .mkdir (target )
1738
+ zipf .extractall (target )
1739
+ self .assertTrue (os .path .isdir (os .path .join (target , "x" )))
1740
+ self .assertEqual (os .listdir (target ), ["x" ])
1704
1741
1705
1742
def tearDown (self ):
1706
1743
rmtree (TESTFN2 )
0 commit comments