zipfile 解压,py3 win下中文乱码

python3.6.1中解压zip,window下文件名乱码问题的解决办法

 

解压代码:

import sys,os
filename="路径下压缩包文件"

print(filename)
# 解压zip
import zipfile
def un_zip(file_name):
    """unzip zip file"""
    zip_file = zipfile.ZipFile(file_name)
    print(zip_file)
    if os.path.isdir(file_name + "_files"):
        pass
    else:
        os.mkdir(file_name + "_files")
    print("zip_file",zip_file.namelist())
    for names in zip_file.namelist():
        print("names:",names)
        # print("names:",type(names))
        zip_file.extract(names,file_name + "_files/")
    zip_file.close()

# 解压调用
un_zip(filename)

中文乱码,修改源码,因为源码默认只识别  utf8和cp437 两种 ,需要修改的有两处,分别位1202行,1385行

            if flags & 0x800:
                # UTF-8 file names extension
                filename = filename.decode('utf-8')
            else:
                # Historical ZIP filename encoding
                # 源码,注释掉,改为 win默认的gbk
                # filename = filename.decode('cp437')
                filename = filename.decode('gbk')

 

posted @ 2017-12-07 20:24  mlj0503  阅读(746)  评论(0编辑  收藏  举报