Python: unZip

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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()

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
@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()

  

 

调用:

1
2
3
4
5
6
7
8
9
10
#中文文件名乱码
#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 @   ®Geovin Du Dream Park™  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2023-04-30 cpp: Simple Factory Pattern
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示