blob: 06a236cef60bf70ea9c3d8b2366dce330e4792b7 [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
Fred Drake3c9f9362000-03-31 17:51:10 +0000144\begin{methoddesc}{printdir}{}
Fred Drake42780242000-10-02 20:56:30 +0000145 Print a table of contents for the archive to \code{sys.stdout}.
Fred Drake3c9f9362000-03-31 17:51:10 +0000146\end{methoddesc}
147
Thomas Wouterscf297e42007-02-23 15:07:44 +0000148\begin{methoddesc}{setpassword}{pwd}
149 Set \var{pwd} as default password to extract encrypted files.
150 \versionadded{2.6}
151\end{methoddesc}
152
153\begin{methoddesc}{read}{name\optional{, pwd}}
Fred Drake3c9f9362000-03-31 17:51:10 +0000154 Return the bytes of the file in the archive. The archive must be
Thomas Wouterscf297e42007-02-23 15:07:44 +0000155 open for read or append. \var{pwd} is the password used for encrypted
156 files and, if specified, it will override the default password set with
157 \method{setpassword()}.
158
159 \versionchanged[\var{pwd} was added]{2.6}
Fred Drake3c9f9362000-03-31 17:51:10 +0000160\end{methoddesc}
161
Fred Drake42780242000-10-02 20:56:30 +0000162\begin{methoddesc}{testzip}{}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000163 Read all the files in the archive and check their CRC's and file
164 headers. Return the name of the first bad file, or else return \code{None}.
Fred Drake42780242000-10-02 20:56:30 +0000165\end{methoddesc}
166
Fred Drakee35360f2000-10-03 15:16:31 +0000167\begin{methoddesc}{write}{filename\optional{, arcname\optional{,
168 compress_type}}}
Fred Drake42780242000-10-02 20:56:30 +0000169 Write the file named \var{filename} to the archive, giving it the
Fred Drakee35360f2000-10-03 15:16:31 +0000170 archive name \var{arcname} (by default, this will be the same as
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000171 \var{filename}, but without a drive letter and with leading path
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000172 separators removed). If given, \var{compress_type} overrides the
173 value given for the \var{compression} parameter to the constructor
174 for the new entry. The archive must be open with mode \code{'w'}
175 or \code{'a'}.
176
177 \note{There is no official file name encoding for ZIP files.
178 If you have unicode file names, please convert them to byte strings
179 in your desired encoding before passing them to \method{write()}.
180 WinZip interprets all file names as encoded in CP437, also known
181 as DOS Latin.}
182
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000183 \note{Archive names should be relative to the archive root, that is,
184 they should not start with a path separator.}
Fred Drake3c9f9362000-03-31 17:51:10 +0000185\end{methoddesc}
186
Just van Rossumb083cb32002-12-12 12:23:32 +0000187\begin{methoddesc}{writestr}{zinfo_or_arcname, bytes}
188 Write the string \var{bytes} to the archive; \var{zinfo_or_arcname}
189 is either the file name it will be given in the archive, or a
190 \class{ZipInfo} instance. If it's an instance, at least the
191 filename, date, and time must be given. If it's a name, the date
192 and time is set to the current date and time. The archive must be
193 opened with mode \code{'w'} or \code{'a'}.
Fred Drake3c9f9362000-03-31 17:51:10 +0000194\end{methoddesc}
195
Fred Drake42780242000-10-02 20:56:30 +0000196
197The following data attribute is also available:
198
199\begin{memberdesc}{debug}
200 The level of debug output to use. This may be set from \code{0}
201 (the default, no output) to \code{3} (the most output). Debugging
202 information is written to \code{sys.stdout}.
203\end{memberdesc}
204
205
206\subsection{PyZipFile Objects \label{pyzipfile-objects}}
207
208The \class{PyZipFile} constructor takes the same parameters as the
209\class{ZipFile} constructor. Instances have one method in addition to
210those of \class{ZipFile} objects.
211
212\begin{methoddesc}[PyZipFile]{writepy}{pathname\optional{, basename}}
Fred Drake3c9f9362000-03-31 17:51:10 +0000213 Search for files \file{*.py} and add the corresponding file to the
214 archive. The corresponding file is a \file{*.pyo} file if
215 available, else a \file{*.pyc} file, compiling if necessary. If the
216 pathname is a file, the filename must end with \file{.py}, and just
Fred Drake42780242000-10-02 20:56:30 +0000217 the (corresponding \file{*.py[co]}) file is added at the top level
Fred Drake3c9f9362000-03-31 17:51:10 +0000218 (no path information). If it is a directory, and the directory is
Fred Drake42780242000-10-02 20:56:30 +0000219 not a package directory, then all the files \file{*.py[co]} are
Fred Drake3c9f9362000-03-31 17:51:10 +0000220 added at the top level. If the directory is a package directory,
221 then all \file{*.py[oc]} are added under the package name as a file
222 path, and if any subdirectories are package directories, all of
223 these are added recursively. \var{basename} is intended for
224 internal use only. The \method{writepy()} method makes archives
225 with file names like this:
226
227\begin{verbatim}
228 string.pyc # Top level name
229 test/__init__.pyc # Package directory
230 test/testall.pyc # Module test.testall
231 test/bogus/__init__.pyc # Subpackage directory
232 test/bogus/myfile.pyc # Submodule test.bogus.myfile
233\end{verbatim}
234\end{methoddesc}
235
Fred Drake42780242000-10-02 20:56:30 +0000236
237\subsection{ZipInfo Objects \label{zipinfo-objects}}
238
239Instances of the \class{ZipInfo} class are returned by the
Fred Drakee35360f2000-10-03 15:16:31 +0000240\method{getinfo()} and \method{infolist()} methods of
Fred Drake42780242000-10-02 20:56:30 +0000241\class{ZipFile} objects. Each object stores information about a
242single member of the ZIP archive.
243
244Instances have the following attributes:
245
246\begin{memberdesc}[ZipInfo]{filename}
247 Name of the file in the archive.
248\end{memberdesc}
249
250\begin{memberdesc}[ZipInfo]{date_time}
Raymond Hettinger999b57c2003-08-25 04:28:05 +0000251 The time and date of the last modification to the archive
Fred Drake42780242000-10-02 20:56:30 +0000252 member. This is a tuple of six values:
253
254\begin{tableii}{c|l}{code}{Index}{Value}
255 \lineii{0}{Year}
256 \lineii{1}{Month (one-based)}
257 \lineii{2}{Day of month (one-based)}
258 \lineii{3}{Hours (zero-based)}
259 \lineii{4}{Minutes (zero-based)}
260 \lineii{5}{Seconds (zero-based)}
261\end{tableii}
262\end{memberdesc}
263
264\begin{memberdesc}[ZipInfo]{compress_type}
265 Type of compression for the archive member.
266\end{memberdesc}
267
268\begin{memberdesc}[ZipInfo]{comment}
269 Comment for the individual archive member.
270\end{memberdesc}
271
272\begin{memberdesc}[ZipInfo]{extra}
273 Expansion field data. The
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000274 \citetitle[http://www.pkware.com/business_and_developers/developer/appnote/]
275 {PKZIP Application Note} contains some comments on the internal
276 structure of the data contained in this string.
Fred Drake42780242000-10-02 20:56:30 +0000277\end{memberdesc}
278
279\begin{memberdesc}[ZipInfo]{create_system}
280 System which created ZIP archive.
281\end{memberdesc}
282
283\begin{memberdesc}[ZipInfo]{create_version}
284 PKZIP version which created ZIP archive.
285\end{memberdesc}
286
287\begin{memberdesc}[ZipInfo]{extract_version}
288 PKZIP version needed to extract archive.
289\end{memberdesc}
290
291\begin{memberdesc}[ZipInfo]{reserved}
292 Must be zero.
293\end{memberdesc}
294
295\begin{memberdesc}[ZipInfo]{flag_bits}
296 ZIP flag bits.
297\end{memberdesc}
298
299\begin{memberdesc}[ZipInfo]{volume}
300 Volume number of file header.
301\end{memberdesc}
302
303\begin{memberdesc}[ZipInfo]{internal_attr}
304 Internal attributes.
305\end{memberdesc}
306
307\begin{memberdesc}[ZipInfo]{external_attr}
308 External file attributes.
309\end{memberdesc}
310
311\begin{memberdesc}[ZipInfo]{header_offset}
312 Byte offset to the file header.
313\end{memberdesc}
314
Fred Drake42780242000-10-02 20:56:30 +0000315\begin{memberdesc}[ZipInfo]{CRC}
316 CRC-32 of the uncompressed file.
317\end{memberdesc}
318
319\begin{memberdesc}[ZipInfo]{compress_size}
320 Size of the compressed data.
321\end{memberdesc}
322
323\begin{memberdesc}[ZipInfo]{file_size}
324 Size of the uncompressed file.
325\end{memberdesc}