Python 官方文档:入门教程 => 点击学习
ZIP文件格式是一种常见的存档和压缩标准,这个zipfile模块提供了工具来创建、读取、写入、附加和列出一个ZIP文件。使用ZIP64扩展(即压缩文件大小超过4G),它能解压加密的ZIP文件,解密过程很慢。1、测试是否为ZIP文件is_zi
ZIP文件格式是一种常见的存档和压缩标准,这个zipfile模块提供了工具来创建、读取、写入、附加和列出一个ZIP文件。使用ZIP64扩展(即压缩文件大小超过4G),它能解压加密的ZIP文件,解密过程很慢。
1、测试是否为ZIP文件
is_zipfile()函数会返回一个布尔值来表示是否为ZIP文件,代码如下:
#!/usr/bin/python
import zipfile
for filename in ['print_name.py', 'Python.zip', 'uwsgi', 'admin']:
print '%20s %s' % (filename, zipfile.is_zipfile(filename))
如果文件不存在或者不是ZIP文件会返回False。
[root@www home]# python zipfile_is_zipfile.py
print_name.py False
python.zip True
uwsgi False
admin False
2、读取ZIP文件的内容
#!/usr/bin/env python
import zipfile
zf = zipfile.ZipFile('python.zip', 'r')
print zf.namelist()
使用namelist() 函数,返回结果是一个列表
[root@www home]# 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'
print
if __name__ == '__main__':
print_info('python.zip')
执行上面代码显示如下结果:
[root@www home]# 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)
如果需要查找的不存在ZIP文档里,会返回一个KeyError错误。
[root@www home]# python zipfile_getinfo.py
test.txt is 419430400 bytes
ERROR: Did not find notthere.txt in zip file
3、从一个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
要提取的文件会被自动解压:
[root@www home]# 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 file
4、创建一个新的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
print_info('zipfile_write.zip')
默认情况下不会对文件进行压缩:
[root@www home]# 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[compression]
zf.write('text.txt', compress_type=compression)
finally:
print 'closing'
zf.close()
print
print_info('zipfile_write_compression.zip')
这次文件被压缩:
[root@www home]# 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')
结果显示原始文件名已经被替换了:
[root@www home]# 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/
--结束END--
本文标题: python zipfile模块学习笔记
本文链接: https://lsjlt.com/news/191222.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0