Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.12] gh-112795: Allow / folder in a zipfile (GH-112932) #113789

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_zipfile/_path/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1758,7 +1758,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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.