]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112795: Allow `/` folder in a zipfile (#112932)
authorAN Long <aisk@users.noreply.github.com>
Sun, 7 Jan 2024 01:14:18 +0000 (09:14 +0800)
committerGitHub <noreply@github.com>
Sun, 7 Jan 2024 01:14:18 +0000 (01:14 +0000)
Allow extraction (no-op) of a "/" folder in a zipfile, they are commonly added by some archive creation tools.

Co-authored-by: Erlend E. Aasland <erlend@python.org>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Lib/test/test_zipfile/_path/test_path.py
Lib/zipfile/__init__.py
Misc/NEWS.d/next/Library/2024-01-07-00-56-41.gh-issue-112932.OfhUu7.rst [new file with mode: 0644]

index c66cb3cba69ebd05de2fb0a51b796d3e7f1a05c9..171ab6fdb5fc28e65a995c25bdc0db830f9c4f3c 100644 (file)
@@ -577,3 +577,15 @@ class TestPath(unittest.TestCase):
         zipfile.Path(alpharep)
         with self.assertRaises(KeyError):
             alpharep.getinfo('does-not-exist')
+
+    def test_root_folder_in_zipfile(self):
+        """
+        gh-112795: Some tools or self constructed codes will add '/' folder to
+        the zip file, this is a strange behavior, but we should support it.
+        """
+        in_memory_file = io.BytesIO()
+        zf = zipfile.ZipFile(in_memory_file, "w")
+        zf.mkdir('/')
+        zf.writestr('./a.txt', 'aaa')
+        tmpdir = pathlib.Path(self.fixtures.enter_context(temp_dir()))
+        zf.extractall(tmpdir)
index fe629ed1cf2fc5cb1882983c9ab43037426c19ef..1c415a2eb7bc094790aacad70bacf97949510e2a 100644 (file)
@@ -1772,7 +1772,7 @@ class ZipFile:
             # filter illegal characters on Windows
             arcname = self._sanitize_windows_name(arcname, os.path.sep)
 
-        if not arcname:
+        if not arcname and not member.is_dir():
             raise ValueError("Empty filename.")
 
         targetpath = os.path.join(targetpath, arcname)
diff --git a/Misc/NEWS.d/next/Library/2024-01-07-00-56-41.gh-issue-112932.OfhUu7.rst b/Misc/NEWS.d/next/Library/2024-01-07-00-56-41.gh-issue-112932.OfhUu7.rst
new file mode 100644 (file)
index 0000000..c61525c
--- /dev/null
@@ -0,0 +1,3 @@
+Restore the ability for :mod:`zipfile` to ``extractall`` from zip files with
+a "/" directory entry in them as is commonly added to zips by some wiki or
+bug tracker data exporters.