mirror of
https://github.com/python/cpython.git
synced 2026-01-26 21:03:34 +00:00
[3.11] gh-117084: Fix ZIP file extraction for directory entry names with backslashes on Windows (GH-117129) (GH-117162) (GH-117165)
(cherry picked from commit f3fee231d359979133e1d58085f43277c41476d0) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> (cherry picked from commit 567ab3bd15398c8c7b791f3e376ae3e3c0bbe079)
This commit is contained in:
parent
da5efeba93
commit
0d82047645
@ -2882,6 +2882,22 @@ class TestWithDirectory(unittest.TestCase):
|
||||
os.mkdir(os.path.join(TESTFN2, "a"))
|
||||
self.test_extract_dir()
|
||||
|
||||
def test_extract_dir_backslash(self):
|
||||
zfname = findfile("zipdir_backslash.zip")
|
||||
with zipfile.ZipFile(zfname) as zipf:
|
||||
zipf.extractall(TESTFN2)
|
||||
if os.name == 'nt':
|
||||
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
|
||||
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "a", "b", "c")))
|
||||
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "d")))
|
||||
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "d", "e")))
|
||||
else:
|
||||
self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "a\\b\\c")))
|
||||
self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "d\\e\\")))
|
||||
self.assertFalse(os.path.exists(os.path.join(TESTFN2, "a")))
|
||||
self.assertFalse(os.path.exists(os.path.join(TESTFN2, "d")))
|
||||
|
||||
def test_write_dir(self):
|
||||
dirpath = os.path.join(TESTFN2, "x")
|
||||
os.mkdir(dirpath)
|
||||
|
||||
BIN
Lib/test/zipdir_backslash.zip
Normal file
BIN
Lib/test/zipdir_backslash.zip
Normal file
Binary file not shown.
@ -559,7 +559,15 @@ class ZipInfo (object):
|
||||
|
||||
def is_dir(self):
|
||||
"""Return True if this archive member is a directory."""
|
||||
return self.filename[-1] == '/'
|
||||
if self.filename.endswith('/'):
|
||||
return True
|
||||
# The ZIP format specification requires to use forward slashes
|
||||
# as the directory separator, but in practice some ZIP files
|
||||
# created on Windows can use backward slashes. For compatibility
|
||||
# with the extraction code which already handles this:
|
||||
if os.path.altsep:
|
||||
return self.filename.endswith((os.path.sep, os.path.altsep))
|
||||
return False
|
||||
|
||||
|
||||
# ZIP encryption uses the CRC32 one-byte primitive for scrambling some
|
||||
|
||||
@ -0,0 +1,2 @@
|
||||
Fix :mod:`zipfile` extraction for directory entries with the name containing
|
||||
backslashes on Windows.
|
||||
Loading…
x
Reference in New Issue
Block a user