0

Working under windows with python 2.x, files on local drives (not UNC).

It seems ziplib stores filenames inside the zip archive stripping the drive letter and converting the path separator:

C:\msala\test.txt --> msala/test.txt

Questions:

  1. is this behaviour compliant with the specifications of the zip file format, or just a caveat of ziplib ?

  2. how can I check if a given filename is in the archive ?

I prefer to avoid this ugly hack:

if sys.platform == "win32" :
    if filename[1:3] == ":\\" :
        filename = filename[3:]
    filename = filename.replace(os.sep, '/')

if filename in zfh.namelist() :

IMHO it is very un-pythonic (batteries included ?!) to have to manage this...

Massimo
  • 3,171
  • 3
  • 28
  • 41

1 Answers1

1

Question 1: from 4.4.17 (https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT)

All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' for compatibility with Amiga and UNIX file systems etc.

Question 2: many ways to do this, e.g.

zip_filename = os.path.splitdrive(filename)[1].replace('\\', '/')

(should work equally well on windows paths on windows and linux paths on linux).

thebjorn
  • 26,297
  • 11
  • 96
  • 138
  • mmh do you find anything about the stripping of the drive letter ? – Massimo Oct 24 '18 at 07:58
  • See updated answer. Note: it will not work for windows paths on linux, ie. `os.path.splitdrive(r'c:\users\bjorn')` returns `('', 'c:\\users\\bjorn')` on linux (and wsl). – thebjorn Oct 24 '18 at 08:20
  • You should probably pay attention to Eren's answer here too: https://stackoverflow.com/questions/13846000/file-separators-of-path-name-of-zipentry – thebjorn Oct 24 '18 at 08:23