]> granicus.if.org Git - python/commitdiff
Backport commit 33543b4e0e5d from Python 3.2: #10801: In zipfile, support
authorVictor Stinner <victor.stinner@haypocalc.com>
Wed, 18 May 2011 11:43:23 +0000 (13:43 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Wed, 18 May 2011 11:43:23 +0000 (13:43 +0200)
different encodings for the header and the filenames.  Patch by MvL, test by
Eli Bendersky.

Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/NEWS

index 34d0fbcb7225ff5c675ec6c2abec36a84082beb1..825fba1c199b9dc130e4b8f23513a644945e1257 100644 (file)
@@ -3,7 +3,13 @@ try:
     import zlib
 except ImportError:
     zlib = None
-import zipfile, os, unittest, sys, shutil, struct, io
+import io
+import os
+import shutil
+import struct
+import sys
+import unittest
+import zipfile
 
 from tempfile import TemporaryFile
 from random import randint, random
@@ -14,6 +20,7 @@ from test.support import TESTFN, run_unittest, findfile
 TESTFN2 = TESTFN + "2"
 TESTFNDIR = TESTFN + "d"
 FIXEDTEST_SIZE = 1000
+DATAFILES_DIR = 'zipfile_datafiles'
 
 SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
                    ('ziptest2dir/_ziptest2', 'qawsedrftg'),
@@ -387,9 +394,25 @@ class TestsWithSourceFile(unittest.TestCase):
             orig_zip.writestr(zinfo, data)
         orig_zip.close()
 
+    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')
+
+        print(fname)
+        zipfp = zipfile.ZipFile(fname)
+        try:
+            zipfp.extractall()
+        finally:
+            zipfp.close()
+
     def tearDown(self):
-        os.remove(TESTFN)
-        os.remove(TESTFN2)
+        support.unlink(TESTFN)
+        support.unlink(TESTFN2)
 
 class TestZip64InSmallFiles(unittest.TestCase):
     # These tests test the ZIP64 functionality without using large files,
index 2ec630605f67018de1ce51dfdfd1eba61785aad6..a382383e1773d5e97a8fa83f6a3e8e0095718937 100644 (file)
@@ -928,7 +928,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:
             raise BadZipfile(
                   'File name in directory %r and header %r differ.'
                   % (zinfo.orig_filename, fname))
index 8f994b9cee75e449f384a63cff72c2e860f6c02b..40317f3377786f74c6305085ff3718c9d0a87aac 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -72,6 +72,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10801: In zipfile, support different encodings for the header and
+  the filenames.
+
 - Issue #10154, #10090: change the normalization of UTF-8 to "UTF-8" instead
   of "UTF8" in the locale module as the latter is not supported MacOSX and OpenBSD.