diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py index d817bdd8769e7d7..afb1f47665a2d5b 100644 --- a/Lib/zipfile/__init__.py +++ b/Lib/zipfile/__init__.py @@ -2084,7 +2084,10 @@ def _RealGetContents(self): flags = centdir[_CD_FLAG_BITS] if flags & _MASK_UTF_FILENAME: # UTF-8 file names extension - filename = filename.decode('utf-8') + try: + filename = filename.decode('utf-8') + except UnicodeDecodeError as e: + raise BadZipFile("Invalid UTF-8 filename in central directory") from e else: # Historical ZIP filename encoding filename = filename.decode(self.metadata_encoding or 'cp437') @@ -2264,7 +2267,10 @@ def open(self, name, mode="r", pwd=None, *, force_zip64=False): if fheader[_FH_GENERAL_PURPOSE_FLAG_BITS] & _MASK_UTF_FILENAME: # UTF-8 filename + try: fname_str = fname.decode("utf-8") + except UnicodeDecodeError as e: + raise BadZipFile("Invalid UTF-8 filename in local file header") from e else: fname_str = fname.decode(self.metadata_encoding or "cp437")