Credit: Paul Prescod
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 files are a popular, cross-platform way of archiving files.
Python’s standard library comes with a
zipfile
module to access
them easily:
import zipfile z = zipfile.ZipFile("zipfile.zip", "r") for filename in z.namelist( ): print 'File:', filename, bytes = z.read(filename) print 'has',len(bytes),'bytes'
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
.
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.