diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2024-03-22 18:35:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-22 18:35:12 (GMT) |
commit | f3fee231d359979133e1d58085f43277c41476d0 (patch) | |
tree | d5a28e909420cf969bbf35951b5bb2334afba5c5 /Lib | |
parent | 135a698d5cbb9090fa980efcbb15bcf6063ad5b7 (diff) | |
download | cpython-f3fee231d359979133e1d58085f43277c41476d0.zip cpython-f3fee231d359979133e1d58085f43277c41476d0.tar.gz cpython-f3fee231d359979133e1d58085f43277c41476d0.tar.bz2 |
[3.12] gh-117084: Fix ZIP file extraction for directory entry names with backslashes on Windows (GH-117129) (GH-117162)
(cherry picked from commit 567ab3bd15398c8c7b791f3e376ae3e3c0bbe079)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_zipfile/test_core.py | 16 | ||||
-rw-r--r-- | Lib/test/zipdir_backslash.zip | bin | 0 -> 192 bytes | |||
-rw-r--r-- | Lib/zipfile/__init__.py | 10 |
3 files changed, 25 insertions, 1 deletions
diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index ae58145..5b32f80 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -2937,6 +2937,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) diff --git a/Lib/test/zipdir_backslash.zip b/Lib/test/zipdir_backslash.zip Binary files differnew file mode 100644 index 0000000..979126e --- /dev/null +++ b/Lib/test/zipdir_backslash.zip diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py index 8918484..a05c97a 100644 --- a/Lib/zipfile/__init__.py +++ b/Lib/zipfile/__init__.py @@ -582,7 +582,15 @@ class ZipInfo (object): def is_dir(self): """Return True if this archive member is a directory.""" - return self.filename.endswith('/') + 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 |