文章来自《Python cookbook》. 翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!
-- 61.182.251.99 [DateTime(2004-09-23T19:23:58Z)] TableOfContents = 描述 =
Reading Data from ZIP Files
读取ZIP文件
Credit: Paul Prescod
问题 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压缩文件, 不需要解压,直接检查其包含的部分或全部文件信息
解决 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文件:
讨论 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文件的数据,可以压缩包内的
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.
参考 See Also
Documentation for the zipfile module in the Library Reference.