7.15.1 ZipFile Objects

ZipFile (filename[, mode[, compression]])
Open a ZIP file named filename. The mode parameter should be 'r' to read an existing file, 'w' to truncate and write a new file, or 'a' to append to an existing file. For mode is 'a' and filename refers to an existing ZIP file, then additional files are added to it. If filename does not refer to a ZIP file, then a new ZIP archive is appended to the file. This is meant for adding a ZIP archive to another file, such as python.exe. Using
cat myzip.zip >> python.exe
also works, and at least WinZip can read such files. compression is the ZIP compression method to use when writing the archive, and should be ZIP_STORED or ZIP_DEFLATED; unrecognized values will cause ValueError to be raised. The default is ZIP_STORED.

XXX explain the "extra" string for the ZIP format

TOC
A read-only dictionary whose keys are the names in the archive, and whose values are tuples as follows:

Index  Meaning 
0 File data seek offset
1 ZIP file "extra" data as a string
2 ZIP file bit flags
3 ZIP file compression type
4 File modification time in DOS format
5 File modification date in DOS format
6 The CRC-32 of the uncompressed data
7 The compressed size of the file
8 The uncompressed size of the file

The class ZipFile has these methods:

listdir ()
Return a list of names in the archive. Equivalent to zipfile.TOC.keys().

printdir ()
Print a table of contents for the archive to stdout.

read (name)
Return the bytes of the file in the archive. The archive must be open for read or append.

writestr (bytes, arcname, year, month, day, hour, minute, second[, extra])
Write the string bytes and the other data to the archive, and give the archive member the name arcname. extra is the ZIP extra data string. The archive must be opened with mode 'w' or 'a'.

write (filename, arcname[, extra])
Write the file named filename to the archive, giving it the archive name arcname. extra is the ZIP extra data string. The archive must be open with mode 'w' or 'a'.

writepy (pathname[, basename])
Search for files *.py and add the corresponding file to the archive. The corresponding file is a *.pyo file if available, else a *.pyc file, compiling if necessary. If the pathname is a file, the filename must end with .py, and just the (corresponding *.py[oc]) file is added at the top level (no path information). If it is a directory, and the directory is not a package directory, then all the files *.py[oc] are added at the top level. If the directory is a package directory, then all *.py[oc] are added under the package name as a file path, and if any subdirectories are package directories, all of these are added recursively. basename is intended for internal use only. The writepy() method makes archives with file names like this:

    string.pyc                                # Top level name 
    test/__init__.pyc                         # Package directory 
    test/testall.pyc                          # Module test.testall
    test/bogus/__init__.pyc                   # Subpackage directory 
    test/bogus/myfile.pyc                     # Submodule test.bogus.myfile

close ()
Close the archive file. You must call close() before exiting your program or essential records will not be written.

Ver Sobre este documento... para obtener información sobre sugerencias.