Python: unZip

 

import os
import socket
import struct
from unidecode import unidecode
import re
import json
import requests
from bs4 import BeautifulSoup
import gzip
import zipfile
from pathlib import Path
from zipfile import ZipFile

class Czip:
    """
   解压zip文件
    """
    
    @staticmethod
    def unGz(file_name:str):
        """
        ungz zip file  import gzip

        :param file_name:
        :return:
        """
        f_name = file_name.replace(".gz", "")
        # 获取文件的名称,去掉
        g_file = gzip.GzipFile(file_name)
        # 创建gzip对象
        open(f_name, "w+").write(g_file.read())
        # gzip对象用read()打开后,写入open()建立的文件里。
        g_file.close()
        # 关闭gzip对象

    @staticmethod
    def decode(strSource:str)->str:
        """
         编码转换
        :param strcourece:
        :return:
        """

        try:
            strNew = strSource.encode('cp437').decode('gbk')

        except:
            strNew = strSource.encode('utf-8').decode('utf-8')
        return strNew

    @staticmethod
    def sort_dir(e)->int:
        """

        :param e:
        :return:
        """

        return len(e.split("\\"))

    @staticmethod
    def rename(file_list, dir_list):
        """

        :param file_list:
        :param dir_list:
        :return:
        """
        # 按文件夹等级排序
        dir_list.sort(key=Czip.sort_dir, reverse=True)
        # 重命名文件夹
        for dir in dir_list:
            name = Czip.decode(os.path.basename(dir))
            if os.path.basename(dir) != name:
                Path(dir).rename(os.path.join(os.path.dirname(dir), name))
        # 重命名文件
        for file in file_list:
            new_file_path = Czip.decode(file)
            old_file_path = os.path.join(os.path.dirname(new_file_path), os.path.basename(file))
            if old_file_path != new_file_path:
                Path(old_file_path).rename(new_file_path)

    @staticmethod
    def unZipDu(zip_file:str, out_dir:str):
        """
        创建重命名列表,解决中文文件名 这需要考虑用户的语言环境进行转换

        :param zip_file: 需要解压文件名
        :param out_dir:解压的所放的文件夹名
        :return:
        """
        file_list = []
        dir_list = []
        # 解压
        with ZipFile(zip_file, allowZip64=True) as Z:
            for path in Z.namelist():
                path = Z.extract(path, out_dir)
                if os.path.isfile(path):
                    file_list.append(path)
                else:
                    dir_list.append(path)
        # 重命名
        Czip.rename(file_list, dir_list)
        ZipFile.close()

    @staticmethod
    def supportGbk(zip_file: ZipFile):
        """
        from zipfile import ZipFile
        用法:
        with supportGbk(ZipFile(r'./中文.zip')) as zfp:
            zfp.extractall(r'./中文不乱码')
        :param zip_file:
        :return:
        """
        name_to_info = zip_file.NameToInfo
        # copy map first
        for name, info in name_to_info.copy().items():
            real_name = name.encode('cp437').decode('gbk')
            if real_name != name:
                info.filename = real_name
                del name_to_info[name]
                name_to_info[real_name] = info
        return zip_file

    @staticmethod
    def supportGbkDu(zipfile:str,outdir:str):
        """
        from zipfile import ZipFile
        解决中文名称的文件名乱码问题
        用法:
        Common.czip.CzIp.supportGbkDu("geovindu.zip","geovinZip")
        :param zipfile: 所要解压的压缩文件
        :param outdir:解压所放至的文件夹名
        :return:
        """

        unZipFile = ZipFile(zipfile)
        name_to_info = unZipFile.NameToInfo
        # copy map first
        for name, info in name_to_info.copy().items():
            real_name = name.encode('cp437').decode('gbk')
            if real_name != name:
                info.filename = real_name
                del name_to_info[name]
                name_to_info[real_name] = info

        unZipFile.extractall(outdir)
        unZipFile.close()
       

    @staticmethod
    def unZip(file_name:str):
        """
        unzip zip file  import zipfile
        存在中文乱码
        :param file_name:
        :return:
        """
        zip_file = zipfile.ZipFile(file_name)
        if os.path.isdir(file_name + "_files"):
            pass
        else:
            os.mkdir(file_name + "_files")
        for names in zip_file.namelist():
            zip_file.extract(names, file_name + "_files/")
        zip_file.close()

  

    @staticmethod
    def unGz(file_name:str):
        """
        ungz zip file  import gzip

        :param file_name:
        :return:
        """
        f_name = file_name.replace(".gz", "")
        # 获取文件的名称,去掉
        g_file = gzip.GzipFile(file_name)
        # 创建gzip对象
        open(f_name, "w+").write(g_file.read())
        # gzip对象用read()打开后,写入open()建立的文件里。
        g_file.close()
        # 关闭gzip对象

    @staticmethod
    def decode(strSource:str)->str:
        """
         编码转换
        :param strcourece:
        :return:
        """

        try:
            strNew = strSource.encode('cp437').decode('gbk')

        except:
            strNew = strSource.encode('utf-8').decode('utf-8')
        return strNew

    @staticmethod
    def sort_dir(e)->int:
        """

        :param e:
        :return:
        """

        return len(e.split("\\"))

    @staticmethod
    def rename(file_list, dir_list):
        """

        :param file_list:
        :param dir_list:
        :return:
        """
        # 按文件夹等级排序
        dir_list.sort(key=CzIp.sort_dir, reverse=True)
        # 重命名文件夹
        for dir in dir_list:
            name = CzIp.decode(os.path.basename(dir))
            if os.path.basename(dir) != name:
                Path(dir).rename(os.path.join(os.path.dirname(dir), name))
        # 重命名文件
        for file in file_list:
            new_file_path = CzIp.decode(file)
            old_file_path = os.path.join(os.path.dirname(new_file_path), os.path.basename(file))
            if old_file_path != new_file_path:
                Path(old_file_path).rename(new_file_path)

    @staticmethod
    def unZipDu(zip_file:str, out_dir:str):
        """
        创建重命名列表,解决中文文件名 这需要考虑用户的语言环境进行转换

        :param zip_file: 需要解压文件名
        :param out_dir:解压的所放的文件夹名
        :return:
        """
        file_list = []
        dir_list = []
        # 解压
        with ZipFile(zip_file, allowZip64=True) as Z:
            for path in Z.namelist():
                path = Z.extract(path, out_dir)
                if os.path.isfile(path):
                    file_list.append(path)
                else:
                    dir_list.append(path)
        # 重命名
        CzIp.rename(file_list, dir_list)
        ZipFile.close()

    @staticmethod
    def supportGbk(zip_file: ZipFile):
        """
        from zipfile import ZipFile
        用法:
        with support_gbk(ZipFile(r'./中文.zip')) as zfp:
            zfp.extractall(r'./中文不乱码')
        :param zip_file:
        :return:
        """
        name_to_info = zip_file.NameToInfo
        # copy map first
        for name, info in name_to_info.copy().items():
            real_name = name.encode('cp437').decode('gbk')
            if real_name != name:
                info.filename = real_name
                del name_to_info[name]
                name_to_info[real_name] = info
        return zip_file

    @staticmethod
    def supportGbkDu(zipfile:str,outdir:str):
        """
        from zipfile import ZipFile
        解决中文名称的文件名乱码问题
        用法:
        Common.czip.CzIp.supportGbkDu("geovindu.zip","geovinZip")
        :param zipfile: 所要解压的压缩文件
        :param outdir:解压所放至的文件夹名
        :return:
        """

        unZipFile = ZipFile(zipfile)
        name_to_info = unZipFile.NameToInfo
        # copy map first
        for name, info in name_to_info.copy().items():
            real_name = name.encode('cp437').decode('gbk')
            if real_name != name:
                info.filename = real_name
                del name_to_info[name]
                name_to_info[real_name] = info

        unZipFile.extractall(outdir)
        unZipFile.close()


    @staticmethod
    def unZip(file_name:str):
        """
        unzip zip file  import zipfile
        存在中文乱码
        :param file_name:
        :return:
        """
        zip_file = zipfile.ZipFile(file_name)
        if os.path.isdir(file_name + "_files"):
            pass
        else:
            os.mkdir(file_name + "_files")
        for names in zip_file.namelist():
            zip_file.extract(names, file_name + "_files/")
        zip_file.close()

    @staticmethod
    def unZipDu(file_name:str,out_dir:str):
        """
        unzip zip file  import zipfile
        存在中文乱码
        :param file_name:
        :param out_dir:
        :return:
        """
        zip_file = zipfile.ZipFile(file_name)
        if os.path.isdir(out_dir):
            pass
        else:
            os.mkdir(out_dir)
        for names in zip_file.namelist():
            zip_file.extract(names, out_dir)
        zip_file.close()

  

 

调用:

    #中文文件名乱码
    #Common.czip.CzIp.unZip("geovindu.zip")
    #中文无乱码
    #如果文件夹不存在,继续解压
    if not os.path.exists(os.path.splitext("geovinduZip")[0]):
        Common.czip.Czip.unZipDu("geovindu.zip","geovinduZip")

    #with Common.czip.Czip.supportGbk(ZipFile(r'./geovindu.zip')) as zfp:
    #   zfp.extractall(r'./中文不乱码')
    Common.czip.Czip.supportGbkDu("geovindu.zip","geovinZip")

  

posted @ 2024-04-30 15:45  ®Geovin Du Dream Park™  阅读(12)  评论(0编辑  收藏  举报