Skip to content

Commit f99e994

Browse files
Merge pull request geekcomputers#345 from morganwang010/master
merger
2 parents b337c6a + dd99929 commit f99e994

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

tar.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import gzip
2+
import os
3+
import tarfile , zipfile, rarfile
4+
5+
from library.utils.file import get_filetype
6+
from library.utils.path import make_dir
7+
from library.utils.type_conv import random_str
8+
9+
def uncompress(src_file, dest_dir):
10+
result = get_filetype(src_file)
11+
if not result[0] :
12+
return (False, result[1], '')
13+
filefmt = result[1]
14+
15+
result = make_dir(dest_dir)
16+
if not result :
17+
return (False, '创建解压目录失败', filefmt)
18+
19+
if filefmt in ('tgz', 'tar') :
20+
try :
21+
tar = tarfile.open(src_file)
22+
names = tar.getnames()
23+
for name in names:
24+
tar.extract(name, dest_dir)
25+
tar.close()
26+
except Exception as e :
27+
return (False, e, filefmt)
28+
elif filefmt == 'zip':
29+
try :
30+
zip_file = zipfile.ZipFile(src_file)
31+
for names in zip_file.namelist():
32+
zip_file.extract(names, dest_dir)
33+
zip_file.close()
34+
except Exception as e :
35+
return (False, e, filefmt)
36+
elif filefmt == 'rar' :
37+
try :
38+
rar = rarfile.RarFile(src_file)
39+
os.chdir(dest_dir)
40+
rar.extractall()
41+
rar.close()
42+
except Exception as e :
43+
return (False, e, filefmt)
44+
elif filefmt == 'gz' :
45+
try :
46+
47+
f_name = dest_dir + '/' + os.path.basename(src_file)
48+
# 获取文件的名称,去掉
49+
g_file = gzip.GzipFile(src_file)
50+
# 创建gzip对象
51+
open(f_name, "w+").write(g_file.read())
52+
# gzip对象用read()打开后,写入open()建立的文件中。
53+
g_file.close()
54+
# 关闭gzip对象
55+
56+
result = get_filetype(src_file)
57+
if not result[0] :
58+
new_filefmt = '未知'
59+
else :
60+
new_filefmt = result[1]
61+
return (True, '解压后的文件格式为:' + new_filefmt, filefmt)
62+
except Exception as e :
63+
return (False, e, filefmt)
64+
else :
65+
return (False, '文件格式不支持或者不是压缩文件', filefmt)
66+
67+
return (True, '', filefmt)

0 commit comments

Comments
 (0)