From 197a744aaca1f9efd6e0805c34113bdf3b6cc592 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 26 Jan 2020 08:38:49 +0530 Subject: [PATCH] zipfile: Fix regression that broke handling of zipfiles with internal filenames not encoded in UTF-8. Fixes #1860889 [Decoding fails when opening a file with a specific file name in the viewer](https://bugs.launchpad.net/calibre/+bug/1860889) --- src/calibre/utils/zipfile.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/calibre/utils/zipfile.py b/src/calibre/utils/zipfile.py index c25a0a4fc6..93034cbcba 100644 --- a/src/calibre/utils/zipfile.py +++ b/src/calibre/utils/zipfile.py @@ -11,7 +11,7 @@ from tempfile import SpooledTemporaryFile from calibre import sanitize_file_name from calibre.constants import filesystem_encoding from calibre.ebooks.chardet import detect -from polyglot.builtins import unicode_type, string_or_bytes, getcwd, map +from polyglot.builtins import unicode_type, string_or_bytes, getcwd, map, as_bytes try: import zlib # We may need its compression method @@ -327,8 +327,12 @@ class ZipInfo (object): # This is used to ensure paths in generated ZIP files always use # forward slashes as the directory separator, as required by the # ZIP format specification. - if os.sep != "/" and os.sep in filename: - filename = filename.replace(os.sep, "/") + if os.sep != '/': + os_sep, sep = os.sep, '/' + if isinstance(filename, bytes): + os_sep, sep = as_bytes(os_sep), b'/' + if os_sep in filename: + filename = filename.replace(os_sep, sep) self.filename = filename # Normalized file name self.date_time = date_time # year, month, day, hour, min, sec