blob: 0b1d43d8965e67464efd86c71fe2820ef9249126 [file] [log] [blame]
Fred Drake3c9f9362000-03-31 17:51:10 +00001\section{\module{zipfile} ---
2 Work with ZIP archives}
3
4\modulesynopsis{Read and write ZIP-format archive files.}
5\moduleauthor{James C. Ahlstrom}{jim@interet.com}
6\sectionauthor{James C. Ahlstrom}{jim@interet.com}
7% LaTeX markup by Fred L. Drake, Jr. <fdrake@acm.org>
8
9The ZIP file format is a common archive and compression standard.
10This module provides tools to create, read, write, append, and list a
11ZIP file.
12
13The available attributes of this module are:
14
15\begin{excdesc}{error}
16 The error raised for bad ZIP files.
17\end{excdesc}
18
19\begin{datadesc}{_debug}
20 Level of printing, defaults to \code{1}.
21\end{datadesc}
22
23\begin{classdesc}{ZipFile}{...}
24 The class for reading and writing ZIP files. See
25 ``\citetitle{ZipFile Objects}'' (section \ref{zipfile-objects}) for
26 constructor details.
27\end{classdesc}
28
29\begin{funcdesc}{is_zipfile}{path}
30 Returns true if \var{path} is a valid ZIP file based on its magic
31 number, otherwise returns false. This module does not currently
32 handle ZIP files which have appended comments.
33\end{funcdesc}
34
35\begin{funcdesc}{zip2date}{zdate}
36 Return \code{(\var{year}, \var{month}, \var{day})} for a ZIP date
37 code.
38\end{funcdesc}
39
40\begin{funcdesc}{zip2time}{ztime}
41 Return \code{(\var{hour}, \var{minute}, \var{second})} for a ZIP
42 time code.
43\end{funcdesc}
44
45\begin{funcdesc}{date2zip}{year, month, day}
46 Return a ZIP date code.
47\end{funcdesc}
48
49\begin{funcdesc}{time2zip}{hour, minute, second}
50 Return a ZIP time code.
51\end{funcdesc}
52
53\begin{datadesc}{ZIP_STORED}
54 The numeric constant (\code{0}) for an uncompressed archive member.
55\end{datadesc}
56
57\begin{datadesc}{ZIP_DEFLATED}
58 The numeric constant for the usual ZIP compression method. This
59 requires the zlib module. No other compression methods are
60 currently supported.
61\end{datadesc}
62
63
64\begin{seealso}
65 \seetext{XXX point to ZIP format definition}
66 \seetext{XXX point to Info-ZIP home page; mention WiZ}
67\end{seealso}
68
69
70\subsection{ZipFile Objects \label{zipfile-objects}}
71
72\begin{classdesc}{ZipFile}{filename\optional{, mode\optional{, compression}}}
73 Open a ZIP file named \var{filename}. The \var{mode} parameter
74 should be \code{'r'} to read an existing file, \code{'w'} to
75 truncate and write a new file, or \code{'a'} to append to an
76 existing file. For \var{mode} is \code{'a'} and \var{filename}
77 refers to an existing ZIP file, then additional files are added to
78 it. If \var{filename} does not refer to a ZIP file, then a new ZIP
79 archive is appended to the file. This is meant for adding a ZIP
80 archive to another file, such as \file{python.exe}. Using
81\begin{verbatim}
82cat myzip.zip >> python.exe
83\end{verbatim}
84 also works, and at least \program{WinZip} can read such files.
85 \var{compression} is the ZIP compression method to use when writing
86 the archive, and should be \constant{ZIP_STORED} or
87 \constant{ZIP_DEFLATED}; unrecognized values will cause
88 \exception{ValueError} to be raised. The default is
89 \constant{ZIP_STORED}.
90\end{classdesc}
91
92XXX explain the "extra" string for the ZIP format
93
94\begin{memberdesc}{TOC}
95 A read-only dictionary whose keys are the names in the archive, and
96 whose values are tuples as follows:
97
98\begin{tableii}{c|l}{code}{Index}{Meaning}
99 \lineii{0}{File data seek offset}
100 \lineii{1}{ZIP file "extra" data as a string}
101 \lineii{2}{ZIP file bit flags}
102 \lineii{3}{ZIP file compression type}
103 \lineii{4}{File modification time in DOS format}
104 \lineii{5}{File modification date in DOS format}
105 \lineii{6}{The CRC-32 of the uncompressed data}
106 \lineii{7}{The compressed size of the file}
107 \lineii{8}{The uncompressed size of the file}
108\end{tableii}
109\end{memberdesc}
110
111The class ZipFile has these methods:
112
113\begin{methoddesc}{listdir}{}
114 Return a list of names in the archive. Equivalent to
115 \code{\var{zipfile}.TOC.keys()}.
116\end{methoddesc}
117
118\begin{methoddesc}{printdir}{}
119 Print a table of contents for the archive to stdout.
120\end{methoddesc}
121
122\begin{methoddesc}{read}{name}
123 Return the bytes of the file in the archive. The archive must be
124 open for read or append.
125\end{methoddesc}
126
127\begin{methoddesc}{writestr}{bytes, arcname, year, month, day, hour,
128 minute, second\optional{, extra}}
129 Write the string \var{bytes} and the other data to the archive, and
130 give the archive member the name \var{arcname}. \var{extra} is the
131 ZIP extra data string. The archive must be opened with mode
132 \code{'w'} or \code{'a'}.
133\end{methoddesc}
134
135\begin{methoddesc}{write}{filename, arcname\optional{, extra}}
136 Write the file named \var{filename} to the archive, giving it the
137 archive name \var{arcname}. \var{extra} is the ZIP extra data
138 string. The archive must be open with mode \code{'w'} or
139 \code{'a'}.
140\end{methoddesc}
141
142\begin{methoddesc}{writepy}{pathname\optional{, basename}}
143 Search for files \file{*.py} and add the corresponding file to the
144 archive. The corresponding file is a \file{*.pyo} file if
145 available, else a \file{*.pyc} file, compiling if necessary. If the
146 pathname is a file, the filename must end with \file{.py}, and just
147 the (corresponding \file{*.py[oc]}) file is added at the top level
148 (no path information). If it is a directory, and the directory is
149 not a package directory, then all the files \file{*.py[oc]} are
150 added at the top level. If the directory is a package directory,
151 then all \file{*.py[oc]} are added under the package name as a file
152 path, and if any subdirectories are package directories, all of
153 these are added recursively. \var{basename} is intended for
154 internal use only. The \method{writepy()} method makes archives
155 with file names like this:
156
157\begin{verbatim}
158 string.pyc # Top level name
159 test/__init__.pyc # Package directory
160 test/testall.pyc # Module test.testall
161 test/bogus/__init__.pyc # Subpackage directory
162 test/bogus/myfile.pyc # Submodule test.bogus.myfile
163\end{verbatim}
164\end{methoddesc}
165
166\begin{methoddesc}{close}{}
167 Close the archive file. You must call \method{close()} before
168 exiting your program or essential records will not be written.
169\end{methoddesc}