您的位置:首页 > 编程语言 > Python开发

python zip和tar两种格式的压缩与解压

2017-09-04 18:06 393 查看
def unzip(zip_package, directory):
print "unzip the package ", zip_package, " to ", directory
if zipfile.is_zipfile(zip_package):
unzip_cmd = "python -m zipfile  -e " + zip_package + " " + directory
os.system(unzip_cmd)
else:
print "ERROR: unzip failed, please check the zip package again..."

def zip(zipfilename, directory):
print "zip directory ", directory, " to the ", zipfilename
zip_cmd = "python -m zipfile -c " + zipfilename + " " + directory
print zip_cmd
os.system(zip_cmd)

def tar(tarfilename, directory):
print "tar the package ", tarfilename
with tarfile.open(tarfilename, "w:") as tarObj:
tarObj.add(directory, arcname=os.path.basename(directory))

def untar(tar_package, directory):
print "untar the package ", tar_package, " to ", directory
tarHandle = tarfile.open(tar_package, "r:")
#for filename in tarHandle.getnames():
#print filename
tarHandle.extractall(directory)
tarHandle.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐