Skip to content

Commit 7ea1056

Browse files
authored
gh-98286: handle empty filename in ZipFile/ZipInfo properly (#98346)
effectively code modernization and a meaningful exception.
1 parent 0023f51 commit 7ea1056

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Lib/zipfile.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def from_file(cls, filename, arcname=None, *, strict_timestamps=True):
553553

554554
def is_dir(self):
555555
"""Return True if this archive member is a directory."""
556-
return self.filename[-1] == '/'
556+
return self.filename.endswith('/')
557557

558558

559559
# ZIP encryption uses the CRC32 one-byte primitive for scrambling some
@@ -1731,6 +1731,9 @@ def _extract_member(self, member, targetpath, pwd):
17311731
# filter illegal characters on Windows
17321732
arcname = self._sanitize_windows_name(arcname, os.path.sep)
17331733

1734+
if not arcname:
1735+
raise ValueError("Empty filename.")
1736+
17341737
targetpath = os.path.join(targetpath, arcname)
17351738
targetpath = os.path.normpath(targetpath)
17361739

@@ -1820,7 +1823,7 @@ def writestr(self, zinfo_or_arcname, data,
18201823
date_time=time.localtime(time.time())[:6])
18211824
zinfo.compress_type = self.compression
18221825
zinfo._compresslevel = self.compresslevel
1823-
if zinfo.filename[-1] == '/':
1826+
if zinfo.filename.endswith('/'):
18241827
zinfo.external_attr = 0o40775 << 16 # drwxrwxr-x
18251828
zinfo.external_attr |= 0x10 # MS-DOS directory flag
18261829
else:

0 commit comments

Comments
 (0)