]> granicus.if.org Git - python/commitdiff
#10801: In zipfile, support different encodings for the header and the filenames...
authorGeorg Brandl <georg@python.org>
Sat, 1 Jan 2011 10:09:32 +0000 (10:09 +0000)
committerGeorg Brandl <georg@python.org>
Sat, 1 Jan 2011 10:09:32 +0000 (10:09 +0000)
Lib/test/test_zipfile.py
Lib/test/zip_cp437_header.zip [new file with mode: 0644]
Lib/zipfile.py
Misc/NEWS

index d90e771ccbc641e5693dea08bcc0b5b6ca81306e..e8aec4fdf9ebeef5baadedb94ac09ed2a0b9d6cb 100644 (file)
@@ -6,6 +6,7 @@ except ImportError:
 
 import io
 import os
+import sys
 import imp
 import time
 import shutil
@@ -23,6 +24,7 @@ from test.support import TESTFN, run_unittest, findfile, unlink
 TESTFN2 = TESTFN + "2"
 TESTFNDIR = TESTFN + "d"
 FIXEDTEST_SIZE = 1000
+DATAFILES_DIR = 'zipfile_datafiles'
 
 SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
                    ('ziptest2dir/_ziptest2', 'qawsedrftg'),
@@ -487,6 +489,18 @@ class TestsWithSourceFile(unittest.TestCase):
         except zipfile.BadZipFile:
             self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
 
+    def test_unicode_filenames(self):
+        if __name__ == '__main__':
+            myfile = sys.argv[0]
+        else:
+            myfile = __file__
+
+        mydir = os.path.dirname(myfile) or os.curdir
+        fname = os.path.join(mydir, 'zip_cp437_header.zip')
+
+        with zipfile.ZipFile(fname) as zipfp:
+            zipfp.extractall()
+
     def tearDown(self):
         unlink(TESTFN)
         unlink(TESTFN2)
diff --git a/Lib/test/zip_cp437_header.zip b/Lib/test/zip_cp437_header.zip
new file mode 100644 (file)
index 0000000..f7c6cf1
Binary files /dev/null and b/Lib/test/zip_cp437_header.zip differ
index 197f0bc6e1b4c5bcc00e7b0658988266fcd07cc9..50f484873ad85990fe76a624fded11a269e99392 100644 (file)
@@ -930,7 +930,13 @@ class ZipFile:
         if fheader[_FH_EXTRA_FIELD_LENGTH]:
             zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
 
-        if fname != zinfo.orig_filename.encode("utf-8"):
+        if zinfo.flag_bits & 0x800:
+            # UTF-8 filename
+            fname_str = fname.decode("utf-8")
+        else:
+            fname_str = fname.decode("cp437")
+
+        if fname_str != zinfo.orig_filename:
             if not self._filePassed:
                 zef_file.close()
             raise BadZipFile(
index fd21f730060e2932c485b33362e4c4dc67a3f9c7..c6f5337252fdc3d1151735642482900b0357677d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -20,6 +20,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10801: In zipfile, support different encodings for the header and
+  the filenames.
+
 - Issue #6285: IDLE no longer crashes on missing help file; patch by Scott
   David Daniels.