解压zip文件时的unicode错误

8 投票
3 回答
7636 浏览
提问于 2025-04-17 14:11

我有一个小脚本,它的功能是解压一个.zip文件。这个脚本运行得很好,但只对那些文件名里没有像“ä”、“ö”、“ü”等字母的.zip文件有效。如果文件名里有这些字母,我就会遇到这个错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "threading.pyc", line 552, in __bootstrap_inner
  File "install.py", line 92, in run
  File "zipfile.pyc", line 962, in extractall
  File "zipfile.pyc", line 950, in extract
  File "zipfile.pyc", line 979, in _extract_member
  File "ntpath.pyc", line 108, in join
UnicodeDecodeError: 'ascii' codec can't decode byte 0x94 in position 32: ordinal not in range(128)

这是我脚本中解压的部分:

zip = zipfile.ZipFile(path1)
zip.extractall(path2)

我该怎么解决这个问题呢?

3 个回答

-1

这句话说得很明白:信息表明,ASCII 解码器无法处理 非 ASCII 字符。你需要选择其他的字符编码方式。

3

为了方便使用,比如你在Windows上压缩了文件,然后在Linux上解压,你可以把压缩文件里所有的文件路径转换成unicode格式。当你从压缩包中解压时,不要使用 ZipFile.extractall,因为这个方法会默认把文件解压到硬盘上,并且不支持压缩文件中的unicode路径。你可以试试下面的方法:

import zipfile, sys, os,
zf = zipfile.ZipFile(sys.argv[1], 'r')
for m in zf.infolist():
    data = zf.read(m) # extract zipped data into memory
    # convert unicode file path to utf8
    disk_file_name = m.filename.encode('utf8')
    dir_name = os.path.dirname(disk_file_name)
    try:
        os.makedirs(dir_name)
    except OSError as e:
        if e.errno == os.errno.EEXIST:
            pass
        else:
            raise
    except Exception as e:
        raise

    with open(disk_file_name, 'wb') as fd:
        fd.write(data)
zf.close()
5

有一个建议:

当我这样做的时候,我遇到了这个错误:

>>> c = chr(129)
>>> c + u'2'

Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    c + u'2'
UnicodeDecodeError: 'ascii' codec can't decode byte 0x81 in position 0: ordinal not in range(128)

这里面有一个unicode字符串被传递给了join函数。

是不是zip文件的路径是用unicode编码的呢?

你可以试试这样做:

zip = zipfile.ZipFile(str(path1))
zip.extractall(str(path2))

或者这样:

zip = zipfile.ZipFile(unicode(path1))
zip.extractall(unicode(path2))

这是ntpath文件中的第128行:

def join(a, *p): # 63
    for b in p: # 68
                path += "\\" + b  # 128

第二个建议:

from ntpath import *

def join(a, *p):
    """Join two or more pathname components, inserting "\\" as needed.
    If any component is an absolute path, all previous path components
    will be discarded."""
    path = a
    for b in p:
        b_wins = 0  # set to 1 iff b makes path irrelevant
        if path == "":
            b_wins = 1

        elif isabs(b):
            # This probably wipes out path so far.  However, it's more
            # complicated if path begins with a drive letter:
            #     1. join('c:', '/a') == 'c:/a'
            #     2. join('c:/', '/a') == 'c:/a'
            # But
            #     3. join('c:/a', '/b') == '/b'
            #     4. join('c:', 'd:/') = 'd:/'
            #     5. join('c:/', 'd:/') = 'd:/'
            if path[1:2] != ":" or b[1:2] == ":":
                # Path doesn't start with a drive letter, or cases 4 and 5.
                b_wins = 1

            # Else path has a drive letter, and b doesn't but is absolute.
            elif len(path) > 3 or (len(path) == 3 and
                                   path[-1] not in "/\\"):
                # case 3
                b_wins = 1

        if b_wins:
            path = b
        else:
            # Join, and ensure there's a separator.
            assert len(path) > 0
            if path[-1] in "/\\":
                if b and b[0] in "/\\":
                    path += b[1:]
                else:
                    path += b
            elif path[-1] == ":":
                path += b
            elif b:
                if b[0] in "/\\":
                    path += b
                else:
                    # !!! modify the next line so it works !!!
                    path += "\\" + b
            else:
                # path is not empty and does not end with a backslash,
                # but b is empty; since, e.g., split('a/') produces
                # ('a', ''), it's best if join() adds a backslash in
                # this case.
                path += '\\'

    return path

import ntpath
ntpath.join = join

撰写回答