[issue10614] ZipFile: add a filename_encoding argument

Sergey Dorofeev report at bugs.python.org
Sat Oct 19 00:08:51 CEST 2013


Sergey Dorofeev added the comment:

OK, here you are:

--- zipfile.py-orig     2013-09-18 16:45:56.000000000 +0400
+++ zipfile.py  2013-10-19 01:59:07.444346674 +0400
@@ -885,7 +885,7 @@
     fp = None                   # Set here since __del__ checks it
     _windows_illegal_name_trans_table = None

-    def __init__(self, file, mode="r", compression=ZIP_STORED,
allowZip64=False):
+    def __init__(self, file, mode="r", compression=ZIP_STORED,
allowZip64=False, encoding='cp437'):
         """Open the ZIP file with mode read "r", write "w" or append
"a"."""
         if mode not in ("r", "w", "a"):
             raise RuntimeError('ZipFile() requires mode "r", "w", or "a"')
@@ -901,6 +901,7 @@
         self.mode = key = mode.replace('b', '')[0]
         self.pwd = None
         self._comment = b''
+        self.encoding = encoding

         # Check if we were passed a file-like object
         if isinstance(file, str):
@@ -1001,8 +1002,8 @@
                 # UTF-8 file names extension
                 filename = filename.decode('utf-8')
             else:
-                # Historical ZIP filename encoding
-                filename = filename.decode('cp437')
+                # Historical ZIP filename encoding, default is CP437
+                filename = filename.decode(self.encoding)
             # Create ZipInfo instance to store file information
             x = ZipInfo(filename)
             x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH])
@@ -1157,7 +1158,7 @@
                 # UTF-8 filename
                 fname_str = fname.decode("utf-8")
             else:
-                fname_str = fname.decode("cp437")
+                fname_str = fname.decode(self.encoding)

             if fname_str != zinfo.orig_filename:
                 raise BadZipFile(

On Fri, Oct 18, 2013 at 11:47 AM, STINNER Victor <report at bugs.python.org>wrote:

>
> STINNER Victor added the comment:
>
> Please rename codepage to encoding. By the way, 437 is a codepage, cp437 is
> a (python) encoding.
>
> I don't think that ZIP is limited to windows. I uncompressed zip files many
> times on various OSes, github also produces zip (and github is probably not
> using windows). And codepage term is only used on windows. Mac OS 9 users
> might produce mac roman filenames.
>
> ----------
>
> _______________________________________
> Python tracker <report at bugs.python.org>
> <http://bugs.python.org/issue10614>
> _______________________________________
>

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10614>
_______________________________________


More information about the Python-bugs-list mailing list