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 — Get raw file name in bytes from ZipFile · Issue #90139 · python/cpython

Get raw file name in bytes from ZipFile #90139

New issue
Copy link
New issue
Copy link
Open
Open
Get raw file name in bytes from ZipFile#90139
Copy link
Labels
3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

accelerator0099
mannequin
opened on Dec 4, 2021
Issue body actions
BPO 45981
Nosy @ericvsmith, @danifus

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-12-04.13:39:26.920>
labels = ['type-feature', 'library', '3.10']
title = 'Get raw file name in bytes from ZipFile'
updated_at = <Date 2021-12-15.12:18:57.899>
user = 'https://bugs.python.org/accelerator0099'

bugs.python.org fields:

activity = <Date 2021-12-15.12:18:57.899>
actor = 'accelerator0099'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2021-12-04.13:39:26.920>
creator = 'accelerator0099'
dependencies = []
files = []
hgrepos = []
issue_num = 45981
keywords = []
message_count = 7.0
messages = ['407665', '407666', '407669', '407696', '407697', '407765', '408596']
nosy_count = 3.0
nosy_names = ['eric.smith', 'dhillier', 'accelerator0099']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue45981'
versions = ['Python 3.10']

Activity

  1. accelerator0099 commented on Dec 4, 2021

    @accelerator0099
    accelerator0099mannequin
    on Dec 4, 2021
    MannequinAuthor
    More actions

    It's quite annoying that ZipFile corrupts the filename by simply replacing '\\' with '/', not providing the raw file name in bytes to us.

  2. accelerator0099
    added
    3.10 (EOL)end of lifeend of life
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    type-featureA feature request or enhancementA feature request or enhancement
    on Dec 4, 2021
  3. accelerator0099 commented on Dec 4, 2021

    @accelerator0099
    accelerator0099mannequin
    on Dec 4, 2021
    MannequinAuthor
    More actions

    In file Lib/zipfile.py:
    1357> flags = centdir[5]
    1358> if flags & 0x800:
    1359> # UTF-8 file names extension
    1360> filename = filename.decode('utf-8')
    1361> else:
    1362> # Historical ZIP filename encoding
    1363> filename = filename.decode('cp437')

    ZipFile simply decodes all non-utf8 file names by encoding CP437.

    In file Lib/zipfile.py:
    352> # This is used to ensure paths in generated ZIP files always use
    353> # forward slashes as the directory separator, as required by the
    354> # ZIP format specification.
    355> if os.sep != "/" and os.sep in filename:
    356> filename = filename.replace(os.sep, "/")

    And it replaces every '\\' with '/' on windows.

    Consider we have a file named '\x97\x5c\x92\x9b', which is '予兆' in Japanese encoded in SHIFT_JIS.
    You may have noticed the problem:

    '\x5c' is '\\'(backslash) in ASCII

    So you will see ZipFile decodes the bytes by CP437, and replaces all '\\' with '/'.
    And the Japanese character '予' is replaced partially, it is no longer itself.

    Someone says we can replace '/' with '\\' back, and decode it by CP437 to get the raw bytes.
    But what if both '/'('\x2f') and '\\'('\x5c') appear in the raw filename?

    Simply replacing '\\' in a bytestream without knowning the encoding is by no means a good way.
    Maybe we can provide a rawname field in the ZipInfo struct?

  4. ericvsmith commented on Dec 4, 2021

    @ericvsmith
    ericvsmith
    on Dec 4, 2021
    Member
    More actions

    You would also need to decide what to do with these lines, just before the os.sep test:

            # Terminate the file name at the first null byte.  Null bytes in file
            # names are used as tricks by viruses in archives.
            null_byte = filename.find(chr(0))
            if null_byte >= 0:
                filename = filename[0:null_byte]

    I don't think you'd want to do this on an encoded (raw) filename, but on the other hand the comment makes a good point.

  5. accelerator0099 commented on Dec 5, 2021

    @accelerator0099
    accelerator0099mannequin
    on Dec 5, 2021
    MannequinAuthor
    More actions

    Null bytes appear in abnormal zip files. (I haven't seen any multibyte encoding that represents a character with null bytes)

    But non-utf8 encodings are common in normal zip files, as windows uses different encodings for different language settings. (On the other hand, Linux suggests everyone use UTF8 regardless of their language settings.)

    It's a pity that nowadays few software supports specifying encoding when extracting archives.
    (We have unzip-iconv patch on Linux, even if the patch is never accepted by unzip)

    Changing the language and rebooting my OS makes no sense, and I don't know why.

  6. ericvsmith commented on Dec 5, 2021

    @ericvsmith
    ericvsmith
    on Dec 5, 2021
    Member
    More actions

    UTF-16 uses null bytes. I'm sure there are other encodings that do, too.

    But I don't know if these encodings are permitted or common in zip files.

  7. danifus commented on Dec 6, 2021

    @danifus
    danifusmannequin
    on Dec 6, 2021
    Mannequin
    More actions

    Handling different character sets is not completely supported yet. There are a couple of open issues relating to this: https://bugs.python.org/issue40407 (reading file names), https://bugs.python.org/issue41928 (support for reading and writing filenames using the unicode filename extra field) and https://bugs.python.org/issue40172 (issues with reading and then writing a filename from and back into a zip where the initial filename isn't encoded in cp437).

    Most modern zip programs that deal with characters outside ascii or cp437 either set the utf-8 flag or write both an ascii or cp437 compatible filename (to the original filename field in the zip header) and the actual filename with all non-ascii characters in the unicode filename extra field. I think adding support for the unicode field to Python would probably cover the majority files generated by modern zip programs.

    For complete support, including older zip programs that don't support the utf-8 flag or unicode filename extra field, we may need to provide another parameter in Python's ZipFile's read and write functions to be able to override the charset used for the filename stored directly in the zip file header.

    I've added my thoughts on how to approach this in https://bugs.python.org/issue40172 but haven't had time to implement these myself.

    Reacted by Gregory P. Smith
  8. accelerator0099 commented on Dec 15, 2021

    @accelerator0099
    accelerator0099mannequin
    on Dec 15, 2021
    MannequinAuthor
    More actions

    I do think providing a rawfile field in the ZipInfo struct helps.
    As a library, ZipFile should let users know what they are dealing with.
    Users can get data from zip files, and ZipFile shouldn't corrupt them.
    I don't mean that we should provide everything in raw bytes.
    What I mean is that DATA could be CONVERTED, but couldn't be CORRUPTED.

  9. ezio-melotti
    transferred this issue fromon Apr 10, 2022
  10. serhiy-storchaka
    added this to @serhiy-storchaka's project python-zipfileon May 8, 2022
  11. serhiy-storchaka
    added this to Zipfile issues 📦on May 19, 2022
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

    3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • Zipfile issues 📦
      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.