import gzip import os def un_gz(file_name): """ungz zip file""" f_name = file_name.replace(".gz", "") #获取文件的名称,去掉 g_file = gzip.GzipFile(file_name) #创建gzip对象 open(f_name, "w+").write(g_file.read()) #gzip对象用read()打开后,写入open()建立的文件里。 g_file.close() #关闭gzip对象 |
import tarfile def un_tar(file_name): untar zip file""" tar = tarfile.open(file_name) names = tar.getnames() if os.path.isdir(file_name + "_files"): pass else: os.mkdir(file_name + "_files") #因为解压后是很多文件,预先建立同名目录 for name in names: tar.extract(name, file_name + "_files/") tar.close() |
import zipfile def un_zip(file_name): """unzip zip file""" zip_file = zipfile.ZipFile(file_name) if os.path.isdir(file_name + "_files"): pass else: os.mkdir(file_name + "_files") for names in zip_file.namelist(): zip_file.extract(names,file_name + "_files/") zip_file.close() |
import rarfile import os def un_rar(file_name): """unrar zip file""" rar = rarfile.RarFile(file_name) if os.path.isdir(file_name + "_files"): pass else: os.mkdir(file_name + "_files") os.chdir(file_name + "_files"): rar.extractall() rar.close() |
#!/usr/bin/env /usr/local/bin/python # encoding: utf-8 import tarfile import os import time start = time.time() tar=tarfile.open('/path/to/your.tar,'w') for root,dir,files in os.walk('/path/to/dir/'): for file in files: fullpath=os.path.join(root,file) tar.add(fullpath,arcname=file) tar.close() print time.time()-start |
#!/usr/bin/env /usr/local/bin/python # encoding: utf-8 import tarfile import time start = time.time() t = tarfile.open("/path/to/your.tar", "r:") t.extractall(path = '/path/to/extractdir/') t.close() print time.time()-start |
tar = tarfile.open(filename, 'r:gz') for tar_info in tar: file = tar.extractfile(tar_info) do_something_with(file) |
#coding=utf-8 import rarfile path = "E:\\New\\New.rar" path2 = "E:\\New" rf = rarfile.RarFile(path) #待解压文件 rf.extractall(path2) #解压指定文件路径 |