mirror of https://github.com/python/cpython
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 <erlend@python.org> Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
parent
84d1f76092
commit
541c5dbb81
|
@ -577,3 +577,15 @@ class TestPath(unittest.TestCase):
|
||||||
zipfile.Path(alpharep)
|
zipfile.Path(alpharep)
|
||||||
with self.assertRaises(KeyError):
|
with self.assertRaises(KeyError):
|
||||||
alpharep.getinfo('does-not-exist')
|
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)
|
||||||
|
|
|
@ -1772,7 +1772,7 @@ class ZipFile:
|
||||||
# filter illegal characters on Windows
|
# filter illegal characters on Windows
|
||||||
arcname = self._sanitize_windows_name(arcname, os.path.sep)
|
arcname = self._sanitize_windows_name(arcname, os.path.sep)
|
||||||
|
|
||||||
if not arcname:
|
if not arcname and not member.is_dir():
|
||||||
raise ValueError("Empty filename.")
|
raise ValueError("Empty filename.")
|
||||||
|
|
||||||
targetpath = os.path.join(targetpath, arcname)
|
targetpath = os.path.join(targetpath, arcname)
|
||||||
|
|
|
@ -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.
|
Loading…
Reference in New Issue