首页 > 代码库 > 关于文件的压缩与解压
关于文件的压缩与解压
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_type18 return -119 self.funcMap[pkg_type](fname, dirs)20 except Exception ,e:21 print e22 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 e30 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 e40 41 def unrar(self,fname, dirs):42 try:43 file = rarfile.RarFile(fname) 44 file.extractall(dirs) 45 except Exception,e:46 print e47 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, files52 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
关于文件的压缩与解压
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。