Skip to content

Zip extract fails for zips created with Windows #117084

Closed
@fliiiix

Description

@fliiiix

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions