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