-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZipFileUtil.py
More file actions
72 lines (65 loc) · 2.55 KB
/
ZipFileUtil.py
File metadata and controls
72 lines (65 loc) · 2.55 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Author: AsherYang
Email : ouyangfan1991@gmail.com
Date : 2018/12/20
Desc : 文件压缩工具
"""
import zipfile
import tarfile
import FileUtil
from EncodeUtil import _translate, _translateUtf8
class ZipFileUtil:
def __init__(self, call_back=None):
self.call_back = call_back
"""
解压缩
"""
def recursiveUnZipFile(self, src_dir):
if not src_dir:
self.doCallBack(u'请输入需要解压缩的路径')
return
dest_dir = None
zip_ref = None
tar_ref = None
try:
allZipFileList = FileUtil.getAllFilesByExt(src_dir, 'zip')
allGzFileList = FileUtil.getAllFilesByExt(src_dir, 'gz')
# print '--->src_dir:%s allGzFileList:%s ' % (src_dir, allGzFileList)
if not allZipFileList and not allGzFileList:
logStr = u'在目录 ' + _translateUtf8(src_dir) + u' 及子目录下未找到 zip 文件'
self.doCallBack(logStr)
return
for file_path in allZipFileList:
log_txt = u'正在解压文件: ' + _translateUtf8(file_path)
self.doCallBack(log_txt)
# print str(_translateUtf8(file_path))
dest_dir = _translateUtf8(FileUtil.getFilePathWithName(file_path))
zip_ref = zipfile.ZipFile(unicode(file_path), 'r')
# FileUtil.mkdirNotExist(str(dest_dir)) # zipfile 会自动创建
zip_ref.extractall(unicode(dest_dir))
zip_ref.close()
self.recursiveUnZipFile(dest_dir)
for file_path in allGzFileList:
log_txt = u'正在解压文件: ' + _translateUtf8(file_path)
self.doCallBack(log_txt)
dest_dir = str(_translateUtf8(FileUtil.getFilePathWithName(file_path)))
if FileUtil.getFileExt(dest_dir).find('tar') != -1:
dest_dir = str(_translateUtf8(FileUtil.getFilePathWithName(dest_dir)))
# print '----dest_dir: ', dest_dir
tar_ref = tarfile.open(unicode(file_path))
tar_ref.extractall(unicode(dest_dir))
tar_ref.close()
self.recursiveUnZipFile(dest_dir)
except:
if dest_dir:
self.recursiveUnZipFile(dest_dir)
finally:
if zip_ref:
zip_ref.close()
if tar_ref:
tar_ref.close()
def doCallBack(self, msg):
if self.call_back:
self.call_back(msg)