前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于文件的压缩与解压

关于文件的压缩与解压

作者头像
Gxjun
发布2018-03-27 12:07:13
7820
发布2018-03-27 12:07:13
举报
文章被收录于专栏:mlml
代码语言:javascript
复制
 1 #coding:utf-8
 2 import tarfile
 3 import zipfile
 4 import rarfile
 5 import os
 6 import shutil
 7 class parsePackage:
 8     def __init__(self):
 9         self.funcMap={'tar':self.untar,
10                       'zip':self.unzip,
11                       'rar':self.unrar,
12                       'gz':self.untar}
13     def parse(self,fname,dirs):
14         try:
15             pkg_type = fname.split('.')[-1]
16             if self.funcMap.get(pkg_type) is None:
17                 print  "%s is not exist", pkg_type
18                 return -1
19             self.funcMap[pkg_type](fname, dirs)
20         except Exception ,e:
21             print e
22         return dirs #解压文件路径
23 
24     def untar(self,fname, dirs):
25         try:
26             t = tarfile.open(fname)
27             t.extractall(path=dirs)
28         except Exception ,e:
29             print  e
30 
31 
32     def unzip(self,fname, dirs):
33         try:
34             f = zipfile.ZipFile(fname, 'r')
35             for file in f.namelist():
36                 print dirs.split('/')[-1] + '/'
37                 f.extract(file, dirs.split('/')[-1] + '/')
38         except Exception ,e:
39             print e
40 
41     def unrar(self,fname, dirs):
42         try:
43             file = rarfile.RarFile(fname) 
44             file.extractall(dirs)  
45         except Exception,e:
46             print e
47 
48     def tar(self,fname):
49         t = tarfile.open(fname + ".tar.gz", "w:gz")
50         for root, dir, files in os.walk(fname):
51             print root, dir, files
52             for file in files:
53                 fullpath = os.path.join(root, file)
54                 t.add(fullpath)
55         t.close()
56         root_dest_path=os.path.join('./tools/static',fname.split('/')[-1])+'.tar.gz'
57         shutil.move(fname + ".tar.gz", root_dest_path)
58 
59         return root_dest_path
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-04-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档