文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- 61.182.251.99 [2004-09-23 19:23:58]

= 描述 =

Reading Data from ZIP Files

读取ZIP文件

Credit: Paul Prescod

1. 问题 Problem

You have an archive in ZIP format, and you want to examine some or all of the files it contains directly, without expanding them on disk.

有ZIP压缩文件, 不需要解压,直接检查其包含的部分或全部文件信息

2. 解决 Solution

ZIP files are a popular, cross-platform way of archiving files. Python's standard library comes with a zipfile module to access them easily:

ZIP文件是流行的跨平台的文件压缩存档方式。使用Python标准库中模块zipfile可以轻松处理ZIP文件:

   1 import zipfile
   2 
   3 z = zipfile.ZipFile("zipfile.zip", "r")
   4 
   5 for filename in z.namelist(  ):
   6     print 'File:', filename,
   7     bytes = z.read(filename)
   8     print 'has',len(bytes),'bytes'

3. 讨论 Discussion

Python can work directly with data in ZIP files. You can look at the list of items in the directory and work with the data files themselves. This recipe is a snippet that lists all of the names and content lengths of the files included in the ZIP archive zipfile.zip.

Python可以直接处理ZIP文件内数据,包括察看文件列表以及直接处理包内文件。食谱中小程序显示了压缩文件zipfile.zip包含的文件名称列表以及各文件的内容大小.

The zipfile module does not currently handle multidisk ZIP files or ZIP files that have appended comments. Take care to use 'r' as the flag argument, not 'rb', which might seem more natural (e.g., on Windows). With ZipFile, the flag is not used the same way as for opening a file, and 'rb' is not recognized. The 'r' flag takes care of the inherently binary nature of the ZIP file on all platforms.

模块zipfile尚不具有处理跨磁盘的ZIP压缩文件能力,也不能处理ZIP文件内附加的注释. 应该适用"r"参数, 而不是"rb",这样获得的结果看起来更自然。 ZipFile模块中也使用参数"r", 但意义不同于普通打开文件时使用的"r"参数, 并且"rb"不会被识别(#不理会?非法?)。 在所有平台上使用参数"r"就可以了,ZIP文件包含的文件是否为2进制文件以及如何打开,模块本身会处理的。

4. 参考 See Also

Documentation for the zipfile module in the Library Reference.

Python库参考文档 模块zipfile部分