7

If I am unzipping a Zip file with the Python ZipFile library, where the file was created on Windows, but my code is running on Unix-like, will the path separators always be Unix-style?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Joe
  • 46,419
  • 33
  • 155
  • 245
  • Seems like it has to be since it's not stored as a string. I sure hope so :) – fncomp Nov 18 '11 at 02:12
  • 3
    You would hope so! But when you're dealing with computers, hope has nothing to do with anything... – Joe Nov 18 '11 at 02:41

1 Answers1

17

Internally, ZipFile stores forward slashes as required by the zip file specification.

Externally, they get translated using os.sep so it will match what is normally expected on a given platform.

Code references:

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • Thank you! I assumed this was true but it's good to have evidence. – Joe Nov 18 '11 at 08:46
  • 5
    Note that this conversion is only applied when creating an entry in the ZIP archive. When accessing existing members (e.g. with `zip_file.read('dir/file.txt')`) slashes have to be always used. – Feuermurmel Aug 29 '12 at 15:42
  • 1
    +1 for the link to the zip file spec. Thanks!!! Here's the relevant section: 4.4.17 file name: (Variable) 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. – Howard Rubin Jun 30 '15 at 15:36