GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ZipInfo filename is mangled when os.sep is not '/' · Issue #94529 · python/cpython

ZipInfo filename is mangled when os.sep is not '/' #94529

New issue
Copy link
New issue
Copy link
Open
Open
ZipInfo filename is mangled when os.sep is not '/'#94529
Copy link
Labels
OS-unsupportedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@gerph
gerph
opened on Jul 3, 2022
Issue body actions

Bug report

The ZipInfo object within ZipFile performs an explicit translation of the filename.

cpython/Lib/zipfile.py

Lines 378 to 382 in 7db1d2e

# This is used to ensure paths in generated ZIP files always use
# forward slashes as the directory separator, as required by the
# ZIP format specification.
if os.sep != "/" and os.sep in filename:
filename = filename.replace(os.sep, "/")

I believe this is intended to make it easy to use on Windows where you might pass an explicit pathname to the ZipInfo object creation. On Windows the filesystem separator is commonly \ (although it supports / in many cases), so that this foces the the filename attribute to contain a filename in the unix form.
This logic is used whether the ZipInfo object is created manually (usually to add a new file), or when the filename has been taken from an archive that is being extracted.

However, the logic is broken on systems where the os.sep is anything else other than \ or /. On systems where the os.sep is . this means that if you try to create an archive with a file containing a . extension the filename in the archive will be mangled. On such a system, extracting an archive will also mangle the filename.

To demonstrate this, it is possible to do a very simple command line example:

>>> import zipfile
>>> import os
>>> os.sep = '.'
>>> zipfile.ZipInfo('hello.txt')
<ZipInfo filename='hello/txt' file_size=0>

In the real world, this breaks any possibility of using this module on RISC OS where the filesystem separator in os.sep is .. In the current Python 3 on RISC OS, the ZipFile module will always mangle filenames that have standard extensions.

I believe that the intention of the object is that:

  • the filename initialiser on the object and attribute is in unicode format (this has been enforced since Python 3 by the explicit decodes in the archive member reading code).
  • the filename attribute is formed as would be stored in the archive, using / as a directory separator (stated by documentation filename should be the full name of the archive member).
  • the filename initialiser on the object is allowed to be supplied a path name on unix and windows systems, as a convenience (the referenced code will have been relied on by existing software).

As such, I believe the referenced code is broken, and to retain the above assumptions and to allow the handling of zip archives on systems where os.sep is not / or \, the code should instead read:

        if os.sep == "\\" and os.sep in filename:
            filename = filename.replace(os.sep, "/")

This removes the overzealous replacement of os.sep in the creation of the ZipInfo object.

Further problems exist with the from_file method which I shall raise separately.

There are some issues which might be related to this (but this change does not preclude them): #90139 and #92184.

Your environment

  • CPython versions tested on: Python 3.9, 3.10
  • Operating system and architecture: On OS X, simulating the problem seen on RISC OS.

Activity

  1. gerph
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jul 3, 2022
  2. gerph
    changed the title [-]ZipInfo filename is mangled when os.sep is not '\'[/-] [+]ZipInfo filename is mangled when os.sep is not '.'[/+] on Jul 3, 2022
  3. gerph
    changed the title [-]ZipInfo filename is mangled when os.sep is not '.'[/-] [+]ZipInfo filename is mangled when os.sep is not '/'[/+] on Jul 3, 2022
  4. serhiy-storchaka
    added this to Zipfile issues 📦on Jul 7, 2022
  5. iritkatriel
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 24, 2023
  6. emmatyping
    added
    OS-unsupported
    on Oct 20, 2025
  7. github-project-automation
    added this to Unsupported platformson Oct 20, 2025
  8. emmatyping commented on Oct 20, 2025

    @emmatyping
    emmatyping
    on Oct 20, 2025
    Member
    More actions

    It's not clear to me how a filesystem with . as a path separator expects programs to differentiate between a.b being either "file b in folder a" and "file a with extension b". In #94533 you give

    Application.Directory.Filename/txt
    

    as an example path, where it seems like . is os.sep, and / is the extension separator.

    The zipfile documentation states "filename should be the path to a file or directory on the filesystem" (emphasis mine)

    So then why would file.txt to mean anything other than "file 'txt' in folder 'file'"? Shouldn't you pass something like file/txt if you want a file that gets stored as file.txt?

    Perhaps the real issue here is that you want to convert the extension from RISC-OS format to zipfile format so you can specify RISC OS paths?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-unsupportedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • Zipfile issues 📦
      Status
      No status
      Show more project fields
    • Unsupported platforms
      Status
      No status
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.