python类库31[压缩与解压]
2013-03-24 19:24
267 查看
一 python压缩解压libs
zlib:infozip免费的压缩lib。
bzip2:读写bz压缩文件,与bzip2和bunzip2压缩程序兼容。
gzip: 读写gz压缩文件,与GNU压缩程序gzip和gunzip兼容。
zipfile:读写zip压缩文件,与zip,unzip,pkzip,pkunzip,winzip等程序兼容。
tar:读写tar包文件。7z等程序可以大包和解包tar。
二 zip压缩解压实例
import os
import zipfile
filename='c:/test.zip'
curdir="C:/test/"
os.chdir(curdir)
#Create the zip file
tFile = zipfile.ZipFile(filename, 'w')
#Write directory contents to the zip file
files = os.listdir(curdir)
for f in files:
tFile.write(f)
#List archived files
for f in tFile.namelist():
print ("Added %s" % f)
#close the zip file
tFile.close()
#whether the file is zip file
print(zipfile.is_zipfile(filename))
#list all file in the zip file
tFile = zipfile.ZipFile(filename, 'r')
for file in tFile.namelist():
print(file)
#List info for archived file
tinfo=tFile.getinfo("test.log")
print(tinfo.comment)
print(tinfo.compress_size)
print(tinfo.date_time)
print(tinfo.file_size)
print(tinfo.compress_type)
print(tinfo.filename)
#Read zipped file into a buffer
buffer = tFile.read("test.log")
print (buffer)
#close the zip file
tFile.close()
完!
zlib:infozip免费的压缩lib。
bzip2:读写bz压缩文件,与bzip2和bunzip2压缩程序兼容。
gzip: 读写gz压缩文件,与GNU压缩程序gzip和gunzip兼容。
zipfile:读写zip压缩文件,与zip,unzip,pkzip,pkunzip,winzip等程序兼容。
tar:读写tar包文件。7z等程序可以大包和解包tar。
二 zip压缩解压实例
import os
import zipfile
filename='c:/test.zip'
curdir="C:/test/"
os.chdir(curdir)
#Create the zip file
tFile = zipfile.ZipFile(filename, 'w')
#Write directory contents to the zip file
files = os.listdir(curdir)
for f in files:
tFile.write(f)
#List archived files
for f in tFile.namelist():
print ("Added %s" % f)
#close the zip file
tFile.close()
#whether the file is zip file
print(zipfile.is_zipfile(filename))
#list all file in the zip file
tFile = zipfile.ZipFile(filename, 'r')
for file in tFile.namelist():
print(file)
#List info for archived file
tinfo=tFile.getinfo("test.log")
print(tinfo.comment)
print(tinfo.compress_size)
print(tinfo.date_time)
print(tinfo.file_size)
print(tinfo.compress_type)
print(tinfo.filename)
#Read zipped file into a buffer
buffer = tFile.read("test.log")
print (buffer)
#close the zip file
tFile.close()
完!
相关文章推荐
- python类库31[压缩与解压]
- python类库31[logging]
- python类库31[进程subprocess与管道pipe]
- Python下使用tarfile模块来实现文件归档压缩与解压
- python类库31[命令行解析]
- python通过zlib实现压缩与解压字符串的方法
- python类库31[XML RPC实例]
- python使用zlib实现压缩与解压字符串
- 压缩/解压(ICSharpCode.SharpZipLib 类库)
- python类库31[进程subprocess]
- python类库31[读写文件]
- python 多级目录压缩解压文件
- python类库31[进程subprocess与管道pipe]
- [置顶] 使用Python3解压gz、tar、tgz、zip、rar五种格式的压缩文件例子
- 关于几种语言(c#,php,python,javascript)字符串的gzip压缩与解压的整理
- python类库31[时间和日期]
- python类库31[文件和目录os+os.path+shutil]
- python类库31[使用minidom读写xml]
- Python zlib()实现压缩和解压
- python压缩与解压