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.11] gh-117084: Fix ZIP file extraction for directory entry names with backslashes on Windows (GH-117129) (GH-117162) #117165

Merged
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
16 changes: 16 additions & 0 deletions Lib/test/test_zipfile.py
Expand Up @@ -2882,6 +2882,22 @@ def test_bug_6050(self):
os.mkdir(os.path.join(TESTFN2, "a"))
self.test_extract_dir()

def test_extract_dir_backslash(self):
zfname = findfile("zipdir_backslash.zip")
with zipfile.ZipFile(zfname) as zipf:
zipf.extractall(TESTFN2)
if os.name == 'nt':
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "a", "b", "c")))
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "d")))
self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "d", "e")))
else:
self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "a\\b\\c")))
self.assertTrue(os.path.isfile(os.path.join(TESTFN2, "d\\e\\")))
self.assertFalse(os.path.exists(os.path.join(TESTFN2, "a")))
self.assertFalse(os.path.exists(os.path.join(TESTFN2, "d")))

def test_write_dir(self):
dirpath = os.path.join(TESTFN2, "x")
os.mkdir(dirpath)
Expand Down
Binary file added Lib/test/zipdir_backslash.zip
Binary file not shown.
10 changes: 9 additions & 1 deletion Lib/zipfile.py
Expand Up @@ -559,7 +559,15 @@ def from_file(cls, filename, arcname=None, *, strict_timestamps=True):

def is_dir(self):
"""Return True if this archive member is a directory."""
return self.filename[-1] == '/'
if self.filename.endswith('/'):
return True
# The ZIP format specification requires to use forward slashes
# as the directory separator, but in practice some ZIP files
# created on Windows can use backward slashes. For compatibility
# with the extraction code which already handles this:
if os.path.altsep:
return self.filename.endswith((os.path.sep, os.path.altsep))
return False


# ZIP encryption uses the CRC32 one-byte primitive for scrambling some
Expand Down
@@ -0,0 +1,2 @@
Fix :mod:`zipfile` extraction for directory entries with the name containing
backslashes on Windows.