【Python小随笔】解压压缩包(处理中文乱码问题)

支持中文编码

import os
import shutil
import tarfile
import zipfile
import gzip

# 解压压缩包
def support_gbk(zip_file):
    """ 支持中文编码 """
    name_to_info = zip_file.NameToInfo
    # copy map first
    for name, info in name_to_info.copy().items():
        try:
            real_name = name.encode('cp437').decode('gbk')
        except:
            real_name = name
        if real_name != name:
            info.filename = real_name
            del name_to_info[name]
            name_to_info[real_name] = info
    return zip_file


def extract_compressed_file(file_path):
    # 解压目标路径
    extract_path = os.path.dirname(file_path)
    # 获取文件扩展名
    file_extension = os.path.splitext(file_path)[1].lower()
    # 根据文件扩展名选择解压方式
    if file_extension == '.zip':
        # 解压zip文件,支持中文编码
        with zipfile.ZipFile(file_path, 'r') as zfp:
            with support_gbk(zfp) as supported_zfp:
                supported_zfp.extractall(extract_path)
    elif file_extension == '.rar':
        # 解压rar文件,确保中文编码
        import rarfile
        with rarfile.RarFile(file_path, 'r') as rfp:
            rfp.extractall(extract_path)
    elif file_extension == '.tar':
        # 解压tar文件
        with tarfile.open(file_path, 'r') as tfp:
            tfp.extractall(extract_path)
    elif file_extension == '.gz':
        # 解压gzip文件
        extract_file = os.path.splitext(os.path.basename(file_path))[0]
        with gzip.open(file_path, 'rb') as gfp:
            with open(os.path.join(extract_path, extract_file), 'wb') as fp:
                shutil.copyfileobj(gfp, fp)
    else:
        raise ValueError("Unsupported file format")


posted @ 2024-01-18 10:53  PythonNew_Mr.Wang  Views(302)  Comments(0Edit  收藏  举报