Closed
Description
Bug report
Bug description:
import os
from pathlib import Path
from zipfile import ZipFile
def unzip(target_file, destination=Path("/tmp/")):
os.path.altsep = "\\"
with ZipFile(target_file, "r") as zipf:
zipf.extractall(path=destination)
Im building a small web service which allows user to upload zip files containing docs https://github.com/docat-org/docat/blob/main/docat/docat/utils.py#L36 which get unpackt and hosted.
Because of that people create zip files on windows which we try to unpack on linux.
Which fails:
Traceback (most recent call last):
File "/home/user/Downloads/example/test.py", line 71, in <module>
unzip(Path("/home/user/Downloads/example/devdocu.zip"))
File "/home/user/Downloads/example/test.py", line 66, in unzip
zipf.extractall(path=destination)
File "/home/user/.asdf/installs/python/3.12.2/lib/python3.12/zipfile/__init__.py", line 1734, in extractall
self._extract_member(zipinfo, path, pwd)
File "/home/user/.asdf/installs/python/3.12.2/lib/python3.12/zipfile/__init__.py", line 1784, in _extract_member
os.makedirs(upperdirs)
File "<frozen os>", line 225, in makedirs
NotADirectoryError: [Errno 20] Not a directory: '/tmp/DevDocu/articles/XXXX/FBRT/BackupAndRestore'
Unfortunately i can't share the actual zip file as it contains sensitive data.
But zip -T
thinks it is ok and unzip
unpacks this zip without issues.
A bit of digging showed the problem as far as i can tell is in is_dir
which claims that
self.filename='DevDocu\\articles\\XXX\\FBRT\\' is_dir? False
is a file and creates an empty file instead of a dir.
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
- gh-117084: Fix zip extraction bug #117085
- gh-117084: Fix ZIP file extraction for directory entry names with backslashes on Windows #117129
- [3.12] gh-117084: Fix ZIP file extraction for directory entry names with backslashes on Windows (GH-117129) #117162
- [3.11] gh-117084: Fix ZIP file extraction for directory entry names with backslashes on Windows (GH-117129) (GH-117162) #117165
Metadata
Metadata
Assignees
Projects
Status
Done