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