分类分类
2014-06-09 09:06作者:下载吧
# -*- coding: gbk -*-
#zipfile模块
#读取或写入ZIP压缩包
#读取ZIP压缩包的文件列表
import zipfile
import os
import glob
file = zipfile.ZipFile("zipfile_module.zip", "r")
print '''列出文件'''
for name in file.namelist():
print name
print '''列出文件信息'''
for info in file.infolist():
print info.filename,"文件名"
print info.date_time,"修改时间"
print info.file_size,"大小"
print info.compress_type,"压缩级别"
print info.compress_size ,"压缩数据大小"
print info.file_size,"未压缩文件大小"
for i in range(1, 10):
print ""
print '''解压操作 unzip'''
for name in file.namelist():
file.extract(name,os.getcwd()+"/zipfile_module") #解压到当前目录
#关闭压缩文件句柄
file.close()
print '''压缩操作 zip'''
#获取当前目录树
fileLists=[]
for root,dirs,files in os.walk("zipfile_module"):
for name in dirs:
fileLists.append(os.path.join(root, name)) #目录入列表
for name in files:
fileLists.append(os.path.join(root,name)) #文件入列表
#创建压缩文件
file2=zipfile.ZipFile(os.getcwd()+"/zipfile_module2"+".zip",'w',zipfile.ZIP_DEFLATED) #zipfile.zlib.DEFLATED 亦可
#写入目录树
for name in fileLists:
file2.write(name)
#关闭压缩文件
file2.close()
相关教程