import gzip#压缩g = gzip.GzipFile(filename="", mode='wb', compresslevel=9, fileobj=open(r'r:\test.log.gz','wb'))g.write(open(r'r:\test.log').read())g.close()#解压g = gzip.GzipFile(mode=’rb’, fileobj=open(r’r:\popopo.gz’,’rb’))open(r’r:\test.log’,’wb’).write(g.read())
import osimport tarfile#压缩,创建tar.gz包#创建压缩包名tar = tarfile.open("/tmp/tartest.tar.gz","w:gz")#创建压缩包for root,dir,files in os.walk("/tmp/tartest"):for file in files:fullpath = os.path.join(root,file)tar.add(fullpath)tar.close()#解压tar.gz包方法一:解压到指定目录tar = tarfile.open(tar_path,"r:gz")file_names = tar.getnames()for file_name in file_names: tar.extract(file_name,target_path) tar.close()方法二:tar = tarfile.open(“tartest.tar.gz”)tar.extract("/tmp")tar.close()