Closed
Description
Bug report
ZipFile.writestr()
raises IndexError
for empty filenames unless one creates the ZipInfo
manually:
>>> import zipfile
>>> zf = zipfile.ZipFile("x.zip", "w")
>>> zf.writestr("", "Hello, World!")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.10/zipfile.py", line 1790, in writestr
if zinfo.filename[-1] == '/':
IndexError: string index out of range
>>> zf.writestr(zipfile.ZipInfo(""), "Hello, World!")
>>> zf.namelist()
['']
Now, one could argue that empty file names should not be allowed (and I'm sure they will cause bugs somewhere, if only failing to extract), but that should presumably then be enforced correctly and result in a different exception.
And apparently, ZipInfo.__repr__()
also raises IndexError
for empty filenames via ZipInfo.is_dir()
:
>>> zf.infolist()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.10/zipfile.py", line 399, in __repr__
isdir = self.is_dir()
File "/usr/lib/python3.10/zipfile.py", line 530, in is_dir
return self.filename[-1] == '/'
IndexError: string index out of range
ZipFile.extract()
probably should fail in this case, but again with a different exception and not just via ZipInfo.is_dir()
's IndexError
I think:
>>> zf.extract("", "some_dir")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.10/zipfile.py", line 1628, in extract
return self._extract_member(member, path, pwd)
File "/usr/lib/python3.10/zipfile.py", line 1693, in _extract_member
if member.is_dir():
File "/usr/lib/python3.10/zipfile.py", line 530, in is_dir
return self.filename[-1] == '/'
IndexError: string index out of range
Your environment
- CPython versions tested on: 3.10.7
- Operating system and architecture: Debian amd64