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