Skip to content

Commit 541c5db

Browse files
aiskerlend-aaslandgpshead
authored
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]>
1 parent 84d1f76 commit 541c5db

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Lib/test/test_zipfile/_path/test_path.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,15 @@ def test_getinfo_missing(self, alpharep):
577577
zipfile.Path(alpharep)
578578
with self.assertRaises(KeyError):
579579
alpharep.getinfo('does-not-exist')
580+
581+
def test_root_folder_in_zipfile(self):
582+
"""
583+
gh-112795: Some tools or self constructed codes will add '/' folder to
584+
the zip file, this is a strange behavior, but we should support it.
585+
"""
586+
in_memory_file = io.BytesIO()
587+
zf = zipfile.ZipFile(in_memory_file, "w")
588+
zf.mkdir('/')
589+
zf.writestr('./a.txt', 'aaa')
590+
tmpdir = pathlib.Path(self.fixtures.enter_context(temp_dir()))
591+
zf.extractall(tmpdir)

Lib/zipfile/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@ def _extract_member(self, member, targetpath, pwd):
17721772
# filter illegal characters on Windows
17731773
arcname = self._sanitize_windows_name(arcname, os.path.sep)
17741774

1775-
if not arcname:
1775+
if not arcname and not member.is_dir():
17761776
raise ValueError("Empty filename.")
17771777

17781778
targetpath = os.path.join(targetpath, arcname)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Restore the ability for :mod:`zipfile` to ``extractall`` from zip files with
2+
a "/" directory entry in them as is commonly added to zips by some wiki or
3+
bug tracker data exporters.

0 commit comments

Comments
 (0)