blob: 6cdd1d35e6f66fd18b1110f1896563e91ce167fd [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
Fred Drake42780242000-10-02 20:56:30 +000014ZIP file. Any advanced use of this module will require an
15understanding of the format, as defined in
Thomas Wouters0e3f5912006-08-11 14:57:12 +000016\citetitle[http://www.pkware.com/business_and_developers/developer/appnote/]
17{PKZIP Application Note}.
Fred Drake42780242000-10-02 20:56:30 +000018
19This module does not currently handle ZIP files which have appended
Thomas Wouterscf297e42007-02-23 15:07:44 +000020comments, or multi-disk ZIP files. It can handle ZIP files that use
21the ZIP64 extensions (that is ZIP files that are more than 4 GByte in
22size). It supports decryption of encrypted files in ZIP archives, but
23it cannot currently create an encrypted file.
Fred Drake3c9f9362000-03-31 17:51:10 +000024
25The available attributes of this module are:
26
27\begin{excdesc}{error}
28 The error raised for bad ZIP files.
29\end{excdesc}
30
Thomas Wouters0e3f5912006-08-11 14:57:12 +000031\begin{excdesc}{LargeZipFile}
32 The error raised when a ZIP file would require ZIP64 functionality but that
33 has not been enabled.
34\end{excdesc}
35
Fred Drake96d7a702001-05-11 01:08:13 +000036\begin{classdesc*}{ZipFile}
Fred Drake3c9f9362000-03-31 17:51:10 +000037 The class for reading and writing ZIP files. See
38 ``\citetitle{ZipFile Objects}'' (section \ref{zipfile-objects}) for
39 constructor details.
Fred Drake886f1132001-05-11 15:49:19 +000040\end{classdesc*}
Fred Drake3c9f9362000-03-31 17:51:10 +000041
Fred Drake96d7a702001-05-11 01:08:13 +000042\begin{classdesc*}{PyZipFile}
Fred Drake42780242000-10-02 20:56:30 +000043 Class for creating ZIP archives containing Python libraries.
Fred Drake886f1132001-05-11 15:49:19 +000044\end{classdesc*}
Fred Drake42780242000-10-02 20:56:30 +000045
46\begin{classdesc}{ZipInfo}{\optional{filename\optional{, date_time}}}
Raymond Hettinger68804312005-01-01 00:28:46 +000047 Class used to represent information about a member of an archive.
Fred Drake42780242000-10-02 20:56:30 +000048 Instances of this class are returned by the \method{getinfo()} and
Fred Drakee35360f2000-10-03 15:16:31 +000049 \method{infolist()} methods of \class{ZipFile} objects. Most users
Fred Drake42780242000-10-02 20:56:30 +000050 of the \module{zipfile} module will not need to create these, but
51 only use those created by this module.
52 \var{filename} should be the full name of the archive member, and
53 \var{date_time} should be a tuple containing six fields which
54 describe the time of the last modification to the file; the fields
55 are described in section \ref{zipinfo-objects}, ``ZipInfo Objects.''
56\end{classdesc}
57
Fred Drake5d63a392000-10-06 15:29:56 +000058\begin{funcdesc}{is_zipfile}{filename}
Neal Norwitz6b353702002-04-09 18:15:00 +000059 Returns \code{True} if \var{filename} is a valid ZIP file based on its magic
60 number, otherwise returns \code{False}. This module does not currently
Fred Drake3c9f9362000-03-31 17:51:10 +000061 handle ZIP files which have appended comments.
62\end{funcdesc}
63
Fred Drake3c9f9362000-03-31 17:51:10 +000064\begin{datadesc}{ZIP_STORED}
Fred Drake5d63a392000-10-06 15:29:56 +000065 The numeric constant for an uncompressed archive member.
Fred Drake3c9f9362000-03-31 17:51:10 +000066\end{datadesc}
67
68\begin{datadesc}{ZIP_DEFLATED}
69 The numeric constant for the usual ZIP compression method. This
70 requires the zlib module. No other compression methods are
71 currently supported.
72\end{datadesc}
73
74
75\begin{seealso}
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076 \seetitle[http://www.pkware.com/business_and_developers/developer/appnote/]
77 {PKZIP Application Note}{Documentation on the ZIP file format by
78 Phil Katz, the creator of the format and algorithms used.}
Fred Drake58295de2000-09-30 00:11:45 +000079
80 \seetitle[http://www.info-zip.org/pub/infozip/]{Info-ZIP Home Page}{
81 Information about the Info-ZIP project's ZIP archive
82 programs and development libraries.}
Fred Drake3c9f9362000-03-31 17:51:10 +000083\end{seealso}
84
85
86\subsection{ZipFile Objects \label{zipfile-objects}}
87
Thomas Wouters0e3f5912006-08-11 14:57:12 +000088\begin{classdesc}{ZipFile}{file\optional{, mode\optional{, compression\optional{, allowZip64}}}}
Fred Drakebda3a592001-05-09 19:57:37 +000089 Open a ZIP file, where \var{file} can be either a path to a file
Fred Drake907e76b2001-07-06 20:30:11 +000090 (a string) or a file-like object. The \var{mode} parameter
Fred Drake3c9f9362000-03-31 17:51:10 +000091 should be \code{'r'} to read an existing file, \code{'w'} to
92 truncate and write a new file, or \code{'a'} to append to an
Fred Drakebda3a592001-05-09 19:57:37 +000093 existing file. For \var{mode} is \code{'a'} and \var{file}
Fred Drake3c9f9362000-03-31 17:51:10 +000094 refers to an existing ZIP file, then additional files are added to
Fred Drakebda3a592001-05-09 19:57:37 +000095 it. If \var{file} does not refer to a ZIP file, then a new ZIP
Fred Drake3c9f9362000-03-31 17:51:10 +000096 archive is appended to the file. This is meant for adding a ZIP
97 archive to another file, such as \file{python.exe}. Using
Fred Drake42780242000-10-02 20:56:30 +000098
Fred Drake3c9f9362000-03-31 17:51:10 +000099\begin{verbatim}
100cat myzip.zip >> python.exe
101\end{verbatim}
Fred Drake42780242000-10-02 20:56:30 +0000102
Fred Drake3c9f9362000-03-31 17:51:10 +0000103 also works, and at least \program{WinZip} can read such files.
Thomas Wouterscf297e42007-02-23 15:07:44 +0000104 If \var{mode} is \code{a} and the file does not exist at all,
105 it is created.
Fred Drake3c9f9362000-03-31 17:51:10 +0000106 \var{compression} is the ZIP compression method to use when writing
107 the archive, and should be \constant{ZIP_STORED} or
108 \constant{ZIP_DEFLATED}; unrecognized values will cause
Fred Drakee35360f2000-10-03 15:16:31 +0000109 \exception{RuntimeError} to be raised. If \constant{ZIP_DEFLATED}
Thomas Heller3d62f8c2002-01-14 08:37:39 +0000110 is specified but the \refmodule{zlib} module is not available,
Fred Drakee35360f2000-10-03 15:16:31 +0000111 \exception{RuntimeError} is also raised. The default is
Fred Drake3c9f9362000-03-31 17:51:10 +0000112 \constant{ZIP_STORED}.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000113 If \var{allowZip64} is \code{True} zipfile will create ZIP files that use
114 the ZIP64 extensions when the zipfile is larger than 2 GB. If it is
115 false (the default) \module{zipfile} will raise an exception when the
116 ZIP file would require ZIP64 extensions. ZIP64 extensions are disabled by
117 default because the default \program{zip} and \program{unzip} commands on
118 \UNIX{} (the InfoZIP utilities) don't support these extensions.
Thomas Wouterscf297e42007-02-23 15:07:44 +0000119
120 \versionchanged[If the file does not exist, it is created if the
121 mode is 'a']{2.6}
Fred Drake3c9f9362000-03-31 17:51:10 +0000122\end{classdesc}
123
Fred Drakee35360f2000-10-03 15:16:31 +0000124\begin{methoddesc}{close}{}
125 Close the archive file. You must call \method{close()} before
126 exiting your program or essential records will not be written.
127\end{methoddesc}
128
129\begin{methoddesc}{getinfo}{name}
130 Return a \class{ZipInfo} object with information about the archive
131 member \var{name}.
132\end{methoddesc}
133
Fred Drake42780242000-10-02 20:56:30 +0000134\begin{methoddesc}{infolist}{}
135 Return a list containing a \class{ZipInfo} object for each member of
136 the archive. The objects are in the same order as their entries in
137 the actual ZIP file on disk if an existing archive was opened.
Fred Drake3c9f9362000-03-31 17:51:10 +0000138\end{methoddesc}
139
Fred Drake6fe9bac2000-10-11 18:56:00 +0000140\begin{methoddesc}{namelist}{}
141 Return a list of archive members by name.
142\end{methoddesc}
143
Guido van Rossumd8faa362007-04-27 19:54:29 +0000144\begin{methoddesc}{open}{name\optional{, mode\optional{, pwd}}}
145 Extract a member from the archive as a file-like object (ZipExtFile).
146 \var{name} is the name of the file in the archive. The \var{mode}
147 parameter, if included, must be one of the following: \code{'r'} (the
148 default), \code{'U'}, or \code{'rU'}. Choosing \code{'U'} or
149 \code{'rU'} will enable universal newline support in the read-only
150 object. \var{pwd} is the password used for encrypted files.
151 \begin{notice}
152 The file-like object is read-only and provides the following methods:
153 \method{read()}, \method{readline()}, \method{readlines()},
154 \method{__iter__()}, \method{next()}.
155 \end{notice}
156 \begin{notice}
157 If the ZipFile was created by passing in a file-like object as the
158 first argument to the constructor, then the object returned by
159 \method{open()} shares the ZipFile's file pointer. Under these
160 circumstances, the object returned by \method{open()} should not
161 be used after any additional operations are performed on the
162 ZipFile object. If the ZipFile was created by passing in a string
163 (the filename) as the first argument to the constructor, then
164 \method{open()} will create a new file object that will be held
165 by the ZipExtFile, allowing it to operate independently of the
166 ZipFile.
167 \end{notice}
168
169 \versionadded{2.6}
170\end{methoddesc}
171
Fred Drake3c9f9362000-03-31 17:51:10 +0000172\begin{methoddesc}{printdir}{}
Fred Drake42780242000-10-02 20:56:30 +0000173 Print a table of contents for the archive to \code{sys.stdout}.
Fred Drake3c9f9362000-03-31 17:51:10 +0000174\end{methoddesc}
175
Thomas Wouterscf297e42007-02-23 15:07:44 +0000176\begin{methoddesc}{setpassword}{pwd}
177 Set \var{pwd} as default password to extract encrypted files.
178 \versionadded{2.6}
179\end{methoddesc}
180
181\begin{methoddesc}{read}{name\optional{, pwd}}
Fred Drake3c9f9362000-03-31 17:51:10 +0000182 Return the bytes of the file in the archive. The archive must be
Thomas Wouterscf297e42007-02-23 15:07:44 +0000183 open for read or append. \var{pwd} is the password used for encrypted
184 files and, if specified, it will override the default password set with
185 \method{setpassword()}.
186
187 \versionchanged[\var{pwd} was added]{2.6}
Fred Drake3c9f9362000-03-31 17:51:10 +0000188\end{methoddesc}
189
Fred Drake42780242000-10-02 20:56:30 +0000190\begin{methoddesc}{testzip}{}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000191 Read all the files in the archive and check their CRC's and file
192 headers. Return the name of the first bad file, or else return \code{None}.
Fred Drake42780242000-10-02 20:56:30 +0000193\end{methoddesc}
194
Fred Drakee35360f2000-10-03 15:16:31 +0000195\begin{methoddesc}{write}{filename\optional{, arcname\optional{,
196 compress_type}}}
Fred Drake42780242000-10-02 20:56:30 +0000197 Write the file named \var{filename} to the archive, giving it the
Fred Drakee35360f2000-10-03 15:16:31 +0000198 archive name \var{arcname} (by default, this will be the same as
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000199 \var{filename}, but without a drive letter and with leading path
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200 separators removed). If given, \var{compress_type} overrides the
201 value given for the \var{compression} parameter to the constructor
202 for the new entry. The archive must be open with mode \code{'w'}
203 or \code{'a'}.
204
205 \note{There is no official file name encoding for ZIP files.
206 If you have unicode file names, please convert them to byte strings
207 in your desired encoding before passing them to \method{write()}.
208 WinZip interprets all file names as encoded in CP437, also known
209 as DOS Latin.}
210
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000211 \note{Archive names should be relative to the archive root, that is,
212 they should not start with a path separator.}
Fred Drake3c9f9362000-03-31 17:51:10 +0000213\end{methoddesc}
214
Just van Rossumb083cb32002-12-12 12:23:32 +0000215\begin{methoddesc}{writestr}{zinfo_or_arcname, bytes}
216 Write the string \var{bytes} to the archive; \var{zinfo_or_arcname}
217 is either the file name it will be given in the archive, or a
218 \class{ZipInfo} instance. If it's an instance, at least the
219 filename, date, and time must be given. If it's a name, the date
220 and time is set to the current date and time. The archive must be
221 opened with mode \code{'w'} or \code{'a'}.
Fred Drake3c9f9362000-03-31 17:51:10 +0000222\end{methoddesc}
223
Fred Drake42780242000-10-02 20:56:30 +0000224
225The following data attribute is also available:
226
227\begin{memberdesc}{debug}
228 The level of debug output to use. This may be set from \code{0}
229 (the default, no output) to \code{3} (the most output). Debugging
230 information is written to \code{sys.stdout}.
231\end{memberdesc}
232
233
234\subsection{PyZipFile Objects \label{pyzipfile-objects}}
235
236The \class{PyZipFile} constructor takes the same parameters as the
237\class{ZipFile} constructor. Instances have one method in addition to
238those of \class{ZipFile} objects.
239
240\begin{methoddesc}[PyZipFile]{writepy}{pathname\optional{, basename}}
Fred Drake3c9f9362000-03-31 17:51:10 +0000241 Search for files \file{*.py} and add the corresponding file to the
242 archive. The corresponding file is a \file{*.pyo} file if
243 available, else a \file{*.pyc} file, compiling if necessary. If the
244 pathname is a file, the filename must end with \file{.py}, and just
Fred Drake42780242000-10-02 20:56:30 +0000245 the (corresponding \file{*.py[co]}) file is added at the top level
Fred Drake3c9f9362000-03-31 17:51:10 +0000246 (no path information). If it is a directory, and the directory is
Fred Drake42780242000-10-02 20:56:30 +0000247 not a package directory, then all the files \file{*.py[co]} are
Fred Drake3c9f9362000-03-31 17:51:10 +0000248 added at the top level. If the directory is a package directory,
249 then all \file{*.py[oc]} are added under the package name as a file
250 path, and if any subdirectories are package directories, all of
251 these are added recursively. \var{basename} is intended for
252 internal use only. The \method{writepy()} method makes archives
253 with file names like this:
254
255\begin{verbatim}
256 string.pyc # Top level name
257 test/__init__.pyc # Package directory
258 test/testall.pyc # Module test.testall
259 test/bogus/__init__.pyc # Subpackage directory
260 test/bogus/myfile.pyc # Submodule test.bogus.myfile
261\end{verbatim}
262\end{methoddesc}
263
Fred Drake42780242000-10-02 20:56:30 +0000264
265\subsection{ZipInfo Objects \label{zipinfo-objects}}
266
267Instances of the \class{ZipInfo} class are returned by the
Fred Drakee35360f2000-10-03 15:16:31 +0000268\method{getinfo()} and \method{infolist()} methods of
Fred Drake42780242000-10-02 20:56:30 +0000269\class{ZipFile} objects. Each object stores information about a
270single member of the ZIP archive.
271
272Instances have the following attributes:
273
274\begin{memberdesc}[ZipInfo]{filename}
275 Name of the file in the archive.
276\end{memberdesc}
277
278\begin{memberdesc}[ZipInfo]{date_time}
Raymond Hettinger999b57c2003-08-25 04:28:05 +0000279 The time and date of the last modification to the archive
Fred Drake42780242000-10-02 20:56:30 +0000280 member. This is a tuple of six values:
281
282\begin{tableii}{c|l}{code}{Index}{Value}
283 \lineii{0}{Year}
284 \lineii{1}{Month (one-based)}
285 \lineii{2}{Day of month (one-based)}
286 \lineii{3}{Hours (zero-based)}
287 \lineii{4}{Minutes (zero-based)}
288 \lineii{5}{Seconds (zero-based)}
289\end{tableii}
290\end{memberdesc}
291
292\begin{memberdesc}[ZipInfo]{compress_type}
293 Type of compression for the archive member.
294\end{memberdesc}
295
296\begin{memberdesc}[ZipInfo]{comment}
297 Comment for the individual archive member.
298\end{memberdesc}
299
300\begin{memberdesc}[ZipInfo]{extra}
301 Expansion field data. The
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302 \citetitle[http://www.pkware.com/business_and_developers/developer/appnote/]
303 {PKZIP Application Note} contains some comments on the internal
304 structure of the data contained in this string.
Fred Drake42780242000-10-02 20:56:30 +0000305\end{memberdesc}
306
307\begin{memberdesc}[ZipInfo]{create_system}
308 System which created ZIP archive.
309\end{memberdesc}
310
311\begin{memberdesc}[ZipInfo]{create_version}
312 PKZIP version which created ZIP archive.
313\end{memberdesc}
314
315\begin{memberdesc}[ZipInfo]{extract_version}
316 PKZIP version needed to extract archive.
317\end{memberdesc}
318
319\begin{memberdesc}[ZipInfo]{reserved}
320 Must be zero.
321\end{memberdesc}
322
323\begin{memberdesc}[ZipInfo]{flag_bits}
324 ZIP flag bits.
325\end{memberdesc}
326
327\begin{memberdesc}[ZipInfo]{volume}
328 Volume number of file header.
329\end{memberdesc}
330
331\begin{memberdesc}[ZipInfo]{internal_attr}
332 Internal attributes.
333\end{memberdesc}
334
335\begin{memberdesc}[ZipInfo]{external_attr}
336 External file attributes.
337\end{memberdesc}
338
339\begin{memberdesc}[ZipInfo]{header_offset}
340 Byte offset to the file header.
341\end{memberdesc}
342
Fred Drake42780242000-10-02 20:56:30 +0000343\begin{memberdesc}[ZipInfo]{CRC}
344 CRC-32 of the uncompressed file.
345\end{memberdesc}
346
347\begin{memberdesc}[ZipInfo]{compress_size}
348 Size of the compressed data.
349\end{memberdesc}
350
351\begin{memberdesc}[ZipInfo]{file_size}
352 Size of the uncompressed file.
353\end{memberdesc}