https://github.com/python/cpython/commit/541c5dbb81c784afd587406be2cc82645979a107
commit: 541c5dbb81c784afd587406be2cc82645979a107
branch: main
author: AN Long <[email protected]>
committer: gpshead <[email protected]>
date: 2024-01-07T01:14:18Z
summary:

gh-112795: Allow `/` folder in a zipfile (#112932)

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 <[email protected]>
Co-authored-by: Gregory P. Smith <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-01-07-00-56-41.gh-issue-112932.OfhUu7.rst
M Lib/test/test_zipfile/_path/test_path.py
M Lib/zipfile/__init__.py

diff --git a/Lib/test/test_zipfile/_path/test_path.py 
b/Lib/test/test_zipfile/_path/test_path.py
index c66cb3cba69ebd..171ab6fdb5fc28 100644
--- a/Lib/test/test_zipfile/_path/test_path.py
+++ b/Lib/test/test_zipfile/_path/test_path.py
@@ -577,3 +577,15 @@ def test_getinfo_missing(self, alpharep):
         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)
diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py
index fe629ed1cf2fc5..1c415a2eb7bc09 100644
--- a/Lib/zipfile/__init__.py
+++ b/Lib/zipfile/__init__.py
@@ -1772,7 +1772,7 @@ def _extract_member(self, member, targetpath, pwd):
             # 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
index 00000000000000..c61525ca67d2b0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-01-07-00-56-41.gh-issue-112932.OfhUu7.rst
@@ -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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to