zip文件解压 python

python写法

 1 # coding=UTF-8
 2 import os
 3 import sys
 4 import zipfile
 5 reload(sys)
 6 sys.setdefaultencoding('gb18030')
 7 flowFile = session.get()
 8 # 出现乱码时解码
 9 def recode(raw):
10     try:
11         return raw.encode('cp437').decode('gbk')
12     except:
13         return raw.encode('utf-8').decode('utf-8')
14 def mkdir(path):
15     folder = os.path.exists(path)
16     if not folder:
17         os.makedirs(path)
18 pathZip = 'E:\\qinshanxian.zip'
19 obj = 'E:\\a'
20 mkdir(obj)
21 zipFile = zipfile.ZipFile(pathZip)          # 压缩包路径
22 zipFileList = zipFile.namelist()            # 获取压缩包里所有文件
23 for f in zipFileList:
24     zipFile.extract(recode(f), obj)                 # 循环解压文件到指定目录
25 zipFile.close()
26 session.transfer(flowFile, REL_SUCCESS)

nifi写法

 1 # coding=UTF-8
 2 import os
 3 import sys
 4 import zipfile
 5 reload(sys)
 6 sys.setdefaultencoding('gb18030')
 7 flowFile = session.get()
 8 name = flowFile.getAttribute('zip.file.name'.decode('utf8'))
 9 path = flowFile.getAttribute('zip.file.path'.decode('utf8'))
10 # 出现乱码时解码
11 def recode(raw):
12     try:
13         return raw.encode('cp437').decode('gbk')
14     except:
15         return raw.encode('utf-8').decode('utf-8')
16 def mkdir(path):
17     folder = os.path.exists(path)
18     if not folder:
19         os.makedirs(path)
20 pathZip = path+name
21 obj = path
22 mkdir(obj)
23 zipFile = zipfile.ZipFile(pathZip)          # 压缩包路径
24 zipFile.extractall(obj)
25 zipFile.close()
26 session.transfer(flowFile, REL_SUCCESS)

 

posted @ 2023-01-11 15:31  木章  阅读(178)  评论(0)    收藏  举报