classification
Title: Zipfile with leading slashes
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: FFY00, andrei.avk, garrison.taylor, lukasz.langa, miss-islington
Priority: normal Keywords: patch

Created on 2021-01-29 20:28 by garrison.taylor, last changed 2021-08-04 20:02 by lukasz.langa. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26899 merged andrei.avk, 2021-06-24 14:27
PR 27605 merged miss-islington, 2021-08-04 19:34
PR 27606 merged miss-islington, 2021-08-04 19:34
Messages (7)
msg385934 - (view) Author: Garrison Taylor (garrison.taylor) Date: 2021-01-29 20:28
Currently the zipfile library allows you to create invalid zip files. The following code is an example:

from zipfile import ZipFile
import tempfile
temporary_file = tempfile.NamedTemporaryFile()
my_zip = ZipFile(temporary_file.name, 'w')
my_zip.writestr('/some_folder/some_file.txt', 'Some content')
my_zip.close()

The generated zipfile contains "/some_folder/some_file.txt". However, according to the specification for zip files, this is invalid. See below, from the .ZIP File Format Specification version 6.3.9:

       4.4.17.1 The name of the file, with optional relative path.
       The path stored MUST NOT contain a drive or
       device letter, or a leading slash.  All slashes
       MUST be forward slashes '/' as opposed to
       backwards slashes '\' for compatibility with Amiga
       and UNIX file systems etc.  If input came from standard
       input, there is no file name field.  

This is significant because the default Windows Explorer zip file extractor cannot handle zip files that contain a leading slash, producing an error that "The compressed (zipped) folder is invalid."
msg396187 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-06-20 19:21
I propose fixing this in documentation.

Raising an error is probably not warranted because zip files are often created on one system and used on another, so you can't raise an error based on current OS, and having a leading slash in the name is both useful and does work in MacOS and Unix. It probably works in some unzip programs on Windows as well (if anyone can test that would be great).

Therefore I propose adding the following notes to ZipInfo, ZipFile.write() and ZipFile.writestr():

Note: a leading slash in the archive / filename may lead to the archive being un-openable in some zip programs on Windows systems.

If that sounds good I can make a PR.
msg396489 - (view) Author: Garrison Taylor (garrison.taylor) Date: 2021-06-24 14:12
That addition to the documentation sounds appropriate to me. Thanks!
msg398943 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-04 19:35
New changeset 7c5dab4340032eb15d3797d8b601ef11649bbab3 by andrei kulakov in branch 'main':
[doc] bpo-43066: zipfile - add note on leading slash in the filename arg (GH-26899)
https://github.com/python/cpython/commit/7c5dab4340032eb15d3797d8b601ef11649bbab3
msg398946 - (view) Author: miss-islington (miss-islington) Date: 2021-08-04 19:59
New changeset 98f6a72ff621c2033e8f2c38df410e9bfa2b772a by Miss Islington (bot) in branch '3.10':
[doc] bpo-43066: zipfile - add note on leading slash in the filename arg (GH-26899)
https://github.com/python/cpython/commit/98f6a72ff621c2033e8f2c38df410e9bfa2b772a
msg398947 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-04 20:00
New changeset 1a2c0ecfa2ef8542baee951f12b60afd165be9cf by Miss Islington (bot) in branch '3.9':
[doc] bpo-43066: zipfile - add note on leading slash in the filename arg (GH-26899) (GH-27606)
https://github.com/python/cpython/commit/1a2c0ecfa2ef8542baee951f12b60afd165be9cf
msg398948 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-04 20:02
Thanks for the report, Garrison. And Andrei for the patch! ✨ 🍰 ✨
History
Date User Action Args
2021-08-04 20:02:09lukasz.langasetstatus: open -> closed
versions: + Python 3.10, Python 3.11
messages: + msg398948

resolution: fixed
stage: patch review -> resolved
2021-08-04 20:00:58lukasz.langasetmessages: + msg398947
2021-08-04 19:59:11miss-islingtonsetmessages: + msg398946
2021-08-04 19:35:02lukasz.langasetnosy: + lukasz.langa
messages: + msg398943
2021-08-04 19:34:48miss-islingtonsetpull_requests: + pull_request26101
2021-08-04 19:34:43miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26100
2021-06-24 14:27:24andrei.avksetkeywords: + patch
stage: patch review
pull_requests: + pull_request25475
2021-06-24 14:12:38garrison.taylorsetmessages: + msg396489
2021-06-20 19:21:07andrei.avksetnosy: + andrei.avk
messages: + msg396187
2021-05-24 18:44:59FFY00setnosy: + FFY00
2021-01-29 20:28:48garrison.taylorcreate