]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-117084: Fix ZIP file extraction for directory entry names with backslashes...
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 22 Mar 2024 18:35:12 +0000 (20:35 +0200)
committerGitHub <noreply@github.com>
Fri, 22 Mar 2024 18:35:12 +0000 (18:35 +0000)
(cherry picked from commit 567ab3bd15398c8c7b791f3e376ae3e3c0bbe079)

Lib/test/test_zipfile/test_core.py
Lib/test/zipdir_backslash.zip [new file with mode: 0644]
Lib/zipfile/__init__.py
Misc/NEWS.d/next/Library/2024-03-21-17-07-38.gh-issue-117084.w1mTpT.rst [new file with mode: 0644]

index ae58145d3c72c8b8469275bf71161b00029e6bba..5b32f80d80bb7824af58d0829d21547407af6b38 100644 (file)
@@ -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
new file mode 100644 (file)
index 0000000..979126e
Binary files /dev/null and b/Lib/test/zipdir_backslash.zip differ
index 8918484207a326ad2edcaba81add476b47f2ba31..a05c97ab89460c355caf0d903bdd0250f647ba1f 100644 (file)
@@ -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
diff --git a/Misc/NEWS.d/next/Library/2024-03-21-17-07-38.gh-issue-117084.w1mTpT.rst b/Misc/NEWS.d/next/Library/2024-03-21-17-07-38.gh-issue-117084.w1mTpT.rst
new file mode 100644 (file)
index 0000000..6e7790e
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :mod:`zipfile` extraction for directory entries with the name containing
+backslashes on Windows.