python zipfile模块学习笔记(一)
ZIP文件格式是一种常见的存档和压缩标准,这个zipfile模块提供了工具来创建、读取、写入、附加和列出一个ZIP文件。使用ZIP64扩展(即压缩文件大小超过4G),它能解压加密的ZIP文件,解密过程很慢。1、测试是否为ZIP文件is_zipfile()函数会返回一个布尔值来表示是否为ZIP文件,代码如下:#!/usr/bin/pythonimport zipfile
for filename in ['print_name.py', 'python.zip', 'uwsgi', 'admin']:
print '%20s %s' % (filename, zipfile.is_zipfile(filename))如果文件不存在或者不是ZIP文件会返回False。# python zipfile_is_zipfile.py
print_name.py False
python.zip True
uwsgi False
admin False</pre><span style="color:#444444;font-family:georgia, times, 'times new roman', serif;font-size:13px;line-height:22px;background-color:#ffffff;"></span><strong style="margin:0px;padding:0px;border:0px;outline:0px;font-size:large;vertical-align:baseline;background-color:#ffffff;color:#444444;font-family:georgia, times, 'times new roman', serif;line-height:30px;">2、读取ZIP文件的内容</strong><strong style="margin:0px;padding:0px;border:0px;outline:0px;font-size:large;vertical-align:baseline;background-color:#ffffff;color:#444444;font-family:georgia, times, 'times new roman', serif;line-height:30px;"></strong><pre class="brush:python;toolbar:false;">#!/usr/bin/env python
import zipfile
zf = zipfile.ZipFile('python.zip', 'r')
print zf.namelist()使用namelist() 函数,返回结果是一个列表# python zipfile_namelist.py
['test.txt']#这只能查看ZIP文件的部分内容,使用infolist() 或者 getinfo() 可以从ZIP文件中获取更多信息,代码如下:import datetime
import zipfile
def print_info(archive_name):
zf = zipfile.ZipFile(archive_name)
for info in zf.infolist():
print info.filename
print '\tComment:\t', info.comment
print '\tModified:\t', datetime.datetime(*info.date_time)
print '\tSystem:\t\t', info.create_system, '(0 = Windows, 3 = Unix)'
print '\tZIP version:\t', info.create_version
print '\tCompressed:\t', info.compress_size, 'bytes'
print '\tUncompressed:\t', info.file_size, 'bytes'
if __name__ == '__main__':
print_info('python.zip')执行上面代码显示如下结果:# python zipfile_infolist.py
test.txt
Comment:
Modified: 2013-09-06 20:09:58
System: 3 (0 = Windows, 3 = Unix)
ZIP version: 20
Compressed: 419430400 bytes
Uncompressed: 419430400 bytes使用getinfo()函数可以在ZIP文件内查找内容,代码如下:#!/usr/bin/env python
import zipfile
zf = zipfile.ZipFile('python.zip')
for filename in ['test.txt', 'notthere.txt']:
try:
info = zf.getinfo(filename)
except KeyError:
print 'ERROR: Did not find %s in zip file' % filename
else:
print "%s is %d bytes" % (info.filename, info.file_size)</pre><span style="color:#444444;font-family:georgia, times, 'times new roman', serif;font-size:13px;line-height:22px;background-color:#ffffff;"></span><span style="color:#444444;font-family:georgia, times, 'times new roman', serif;font-size:13px;line-height:22px;background-color:#ffffff;">如果需要查找的不存在ZIP文档里,会返回一个KeyError错误。</span><span style="color:#444444;font-family:georgia, times, 'times new roman', serif;font-size:13px;line-height:22px;background-color:#ffffff;"></span><pre class="brush:python;toolbar:false;"># python zipfile_getinfo.py
test.txt is 419430400 bytes
ERROR: Did not find notthere.txt in zip file3、从一个ZIP文档中提取文件#!/usr/bin/env python
import zipfile
zf = zipfile.ZipFile('python.zip')
for filename in ['test.txt', 'notihere.txt']:
try:
data = zf.read(filename)
except KeyError:
print 'ERROR: Did not find %s in zip file' % filename
else:
print filename, ':'
print repr(data)
print</pre><strong style="margin:0px;padding:0px;border:0px;outline:0px;font-size:large;vertical-align:baseline;background-color:#ffffff;color:#444444;font-family:georgia, times, 'times new roman', serif;line-height:30px;"></strong><span style="color:#444444;font-family:georgia, times, 'times new roman', serif;font-size:13px;line-height:22px;background-color:#ffffff;">要提取的文件会被自动解压:</span><span style="color:#444444;font-family:georgia, times, 'times new roman', serif;font-size:13px;line-height:22px;background-color:#ffffff;"></span><pre class="brush:plain;toolbar:false;"># python zipfile_read.py
README.txt :
'The examples for the zipfile module use this file and example.zip as data.\n'
ERROR: Did not find notthere.txt in zip file4、创建一个新的ZIP文件创建新的ZIP归档文件代码如下:#!/usr/bin/env python
from zipfile_infolist import print_info
import zipfile
print 'creating archive'
zf = zipfile.ZipFile('zipfile_write.zip', 'w')
try:
print 'adding text.txt'
zf.write('text.txt')
finally:
print 'closing'
zf.close()
print_info('zipfile_write.zip')默认情况下不会对文件进行压缩:# python zipfile_write.py
creating archive
adding text.txt
closing
text.txt
Comment:
Modified: 2013-09-06 20:39:52
System: 3 (0 = Windows, 3 = Unix)
ZIP version: 20
Compressed: 104857600 bytes
Uncompressed: 104857600 bytes如果要对文件进行压缩,zlib模块是必须的,如果zlib可以使用,你可以使用zipfile.ZIP_DEFLATED设置压缩模式为单个文件或者归档一个整体。默认的压缩模式是zipfile.ZIP_STORED。#!/usr/bin/env python
from zipfile_infolist import print_info
import zipfile
try:
import zlib
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
modes = { zipfile.ZIP_DEFLATED: 'deflated',
zipfile.ZIP_STORED: 'stored',
}
print 'creating archive'
zf = zipfile.ZipFile('zipfile_write_compression.zip', mode='w')
try:
print 'adding text.txt with compression mode', modes
zf.write('text.txt', compress_type=compression)
finally:
print 'closing'
zf.close()
print_info('zipfile_write_compression.zip')这次文件被压缩:# python zipfile_write_compression.py
creating archive
adding text.txt with compression mode deflated
closing
text.txt
Comment:
Modified: 2013-09-06 20:39:52
System: 3 (0 = Windows, 3 = Unix)
ZIP version: 20
Compressed: 101923 bytes
Uncompressed: 104857600 bytes替换原始文件名:#!/usr/bin/env python
from zipfile_infolist import print_info
import zipfile
zf = zipfile.ZipFile('zipfile_write_arcname.zip', mode='w')
try:
zf.write('text.txt', arcname='NOT_README.txt')
finally:
zf.close()
print_info('zipfile_write_arcname.zip')结果显示原始文件名已经被替换了:# python zipfile_write_arcname.py
NOT_README.txt
Comment:
Modified: 2013-09-06 20:39:52
System: 3 (0 = Windows, 3 = Unix)
ZIP version: 20
Compressed: 104857600 bytes
Uncompressed: 104857600 bytes更多博文请移步:linux开源技术博客 http://www.chlinux.net本文参考:http://docs.python.org/2/library/zipfile http://pymotw.com/2/zipfile/
页:
[1]