blob: d3fca64df956c19367579094c31597c0dfb73510 [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
George Yoshida3f1f7be2006-06-17 16:39:13 +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
Martin v. Löwisc6d626e2007-02-13 09:49:38 +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
Ronald Oussoren143cefb2006-06-15 08:14:18 +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}
George Yoshida3f1f7be2006-06-17 16:39:13 +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
Ronald Oussoren143cefb2006-06-15 08:14:18 +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.
104 \var{compression} is the ZIP compression method to use when writing
105 the archive, and should be \constant{ZIP_STORED} or
106 \constant{ZIP_DEFLATED}; unrecognized values will cause
Fred Drakee35360f2000-10-03 15:16:31 +0000107 \exception{RuntimeError} to be raised. If \constant{ZIP_DEFLATED}
Thomas Heller3d62f8c2002-01-14 08:37:39 +0000108 is specified but the \refmodule{zlib} module is not available,
Fred Drakee35360f2000-10-03 15:16:31 +0000109 \exception{RuntimeError} is also raised. The default is
Fred Drake3c9f9362000-03-31 17:51:10 +0000110 \constant{ZIP_STORED}.
Fred Drakee0d4aec2006-07-30 03:03:43 +0000111 If \var{allowZip64} is \code{True} zipfile will create ZIP files that use
112 the ZIP64 extensions when the zipfile is larger than 2 GB. If it is
113 false (the default) \module{zipfile} will raise an exception when the
114 ZIP file would require ZIP64 extensions. ZIP64 extensions are disabled by
115 default because the default \program{zip} and \program{unzip} commands on
116 \UNIX{} (the InfoZIP utilities) don't support these extensions.
Fred Drake3c9f9362000-03-31 17:51:10 +0000117\end{classdesc}
118
Fred Drakee35360f2000-10-03 15:16:31 +0000119\begin{methoddesc}{close}{}
120 Close the archive file. You must call \method{close()} before
121 exiting your program or essential records will not be written.
122\end{methoddesc}
123
124\begin{methoddesc}{getinfo}{name}
125 Return a \class{ZipInfo} object with information about the archive
126 member \var{name}.
127\end{methoddesc}
128
Fred Drake42780242000-10-02 20:56:30 +0000129\begin{methoddesc}{infolist}{}
130 Return a list containing a \class{ZipInfo} object for each member of
131 the archive. The objects are in the same order as their entries in
132 the actual ZIP file on disk if an existing archive was opened.
Fred Drake3c9f9362000-03-31 17:51:10 +0000133\end{methoddesc}
134
Fred Drake6fe9bac2000-10-11 18:56:00 +0000135\begin{methoddesc}{namelist}{}
136 Return a list of archive members by name.
137\end{methoddesc}
138
Fred Drake3c9f9362000-03-31 17:51:10 +0000139\begin{methoddesc}{printdir}{}
Fred Drake42780242000-10-02 20:56:30 +0000140 Print a table of contents for the archive to \code{sys.stdout}.
Fred Drake3c9f9362000-03-31 17:51:10 +0000141\end{methoddesc}
142
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000143\begin{methoddesc}{setpassword}{pwd}
144 Set \var{pwd} as default password to extract encrypted files.
145 \versionadded{2.6}
146\end{methoddesc}
147
148\begin{methoddesc}{read}{name\optional{, pwd}}
Fred Drake3c9f9362000-03-31 17:51:10 +0000149 Return the bytes of the file in the archive. The archive must be
Martin v. Löwisc6d626e2007-02-13 09:49:38 +0000150 open for read or append. \var{pwd} is the password used for encrypted
151 files and, if specified, it will override the default password set with
152 \method{setpassword()}.
153
154 \versionchanged[\var{pwd} was added]{2.6}
Fred Drake3c9f9362000-03-31 17:51:10 +0000155\end{methoddesc}
156
Fred Drake42780242000-10-02 20:56:30 +0000157\begin{methoddesc}{testzip}{}
Ronald Oussoren143cefb2006-06-15 08:14:18 +0000158 Read all the files in the archive and check their CRC's and file
159 headers. Return the name of the first bad file, or else return \code{None}.
Fred Drake42780242000-10-02 20:56:30 +0000160\end{methoddesc}
161
Fred Drakee35360f2000-10-03 15:16:31 +0000162\begin{methoddesc}{write}{filename\optional{, arcname\optional{,
163 compress_type}}}
Fred Drake42780242000-10-02 20:56:30 +0000164 Write the file named \var{filename} to the archive, giving it the
Fred Drakee35360f2000-10-03 15:16:31 +0000165 archive name \var{arcname} (by default, this will be the same as
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000166 \var{filename}, but without a drive letter and with leading path
Georg Brandlcaf95392006-04-06 10:03:32 +0000167 separators removed). If given, \var{compress_type} overrides the
168 value given for the \var{compression} parameter to the constructor
169 for the new entry. The archive must be open with mode \code{'w'}
170 or \code{'a'}.
171
172 \note{There is no official file name encoding for ZIP files.
173 If you have unicode file names, please convert them to byte strings
174 in your desired encoding before passing them to \method{write()}.
175 WinZip interprets all file names as encoded in CP437, also known
176 as DOS Latin.}
177
Georg Brandl8f7c54e2006-02-20 08:40:38 +0000178 \note{Archive names should be relative to the archive root, that is,
179 they should not start with a path separator.}
Fred Drake3c9f9362000-03-31 17:51:10 +0000180\end{methoddesc}
181
Just van Rossumb083cb32002-12-12 12:23:32 +0000182\begin{methoddesc}{writestr}{zinfo_or_arcname, bytes}
183 Write the string \var{bytes} to the archive; \var{zinfo_or_arcname}
184 is either the file name it will be given in the archive, or a
185 \class{ZipInfo} instance. If it's an instance, at least the
186 filename, date, and time must be given. If it's a name, the date
187 and time is set to the current date and time. The archive must be
188 opened with mode \code{'w'} or \code{'a'}.
Fred Drake3c9f9362000-03-31 17:51:10 +0000189\end{methoddesc}
190
Fred Drake42780242000-10-02 20:56:30 +0000191
192The following data attribute is also available:
193
194\begin{memberdesc}{debug}
195 The level of debug output to use. This may be set from \code{0}
196 (the default, no output) to \code{3} (the most output). Debugging
197 information is written to \code{sys.stdout}.
198\end{memberdesc}
199
200
201\subsection{PyZipFile Objects \label{pyzipfile-objects}}
202
203The \class{PyZipFile} constructor takes the same parameters as the
204\class{ZipFile} constructor. Instances have one method in addition to
205those of \class{ZipFile} objects.
206
207\begin{methoddesc}[PyZipFile]{writepy}{pathname\optional{, basename}}
Fred Drake3c9f9362000-03-31 17:51:10 +0000208 Search for files \file{*.py} and add the corresponding file to the
209 archive. The corresponding file is a \file{*.pyo} file if
210 available, else a \file{*.pyc} file, compiling if necessary. If the
211 pathname is a file, the filename must end with \file{.py}, and just
Fred Drake42780242000-10-02 20:56:30 +0000212 the (corresponding \file{*.py[co]}) file is added at the top level
Fred Drake3c9f9362000-03-31 17:51:10 +0000213 (no path information). If it is a directory, and the directory is
Fred Drake42780242000-10-02 20:56:30 +0000214 not a package directory, then all the files \file{*.py[co]} are
Fred Drake3c9f9362000-03-31 17:51:10 +0000215 added at the top level. If the directory is a package directory,
216 then all \file{*.py[oc]} are added under the package name as a file
217 path, and if any subdirectories are package directories, all of
218 these are added recursively. \var{basename} is intended for
219 internal use only. The \method{writepy()} method makes archives
220 with file names like this:
221
222\begin{verbatim}
223 string.pyc # Top level name
224 test/__init__.pyc # Package directory
225 test/testall.pyc # Module test.testall
226 test/bogus/__init__.pyc # Subpackage directory
227 test/bogus/myfile.pyc # Submodule test.bogus.myfile
228\end{verbatim}
229\end{methoddesc}
230
Fred Drake42780242000-10-02 20:56:30 +0000231
232\subsection{ZipInfo Objects \label{zipinfo-objects}}
233
234Instances of the \class{ZipInfo} class are returned by the
Fred Drakee35360f2000-10-03 15:16:31 +0000235\method{getinfo()} and \method{infolist()} methods of
Fred Drake42780242000-10-02 20:56:30 +0000236\class{ZipFile} objects. Each object stores information about a
237single member of the ZIP archive.
238
239Instances have the following attributes:
240
241\begin{memberdesc}[ZipInfo]{filename}
242 Name of the file in the archive.
243\end{memberdesc}
244
245\begin{memberdesc}[ZipInfo]{date_time}
Raymond Hettinger999b57c2003-08-25 04:28:05 +0000246 The time and date of the last modification to the archive
Fred Drake42780242000-10-02 20:56:30 +0000247 member. This is a tuple of six values:
248
249\begin{tableii}{c|l}{code}{Index}{Value}
250 \lineii{0}{Year}
251 \lineii{1}{Month (one-based)}
252 \lineii{2}{Day of month (one-based)}
253 \lineii{3}{Hours (zero-based)}
254 \lineii{4}{Minutes (zero-based)}
255 \lineii{5}{Seconds (zero-based)}
256\end{tableii}
257\end{memberdesc}
258
259\begin{memberdesc}[ZipInfo]{compress_type}
260 Type of compression for the archive member.
261\end{memberdesc}
262
263\begin{memberdesc}[ZipInfo]{comment}
264 Comment for the individual archive member.
265\end{memberdesc}
266
267\begin{memberdesc}[ZipInfo]{extra}
268 Expansion field data. The
George Yoshida3f1f7be2006-06-17 16:39:13 +0000269 \citetitle[http://www.pkware.com/business_and_developers/developer/appnote/]
270 {PKZIP Application Note} contains some comments on the internal
271 structure of the data contained in this string.
Fred Drake42780242000-10-02 20:56:30 +0000272\end{memberdesc}
273
274\begin{memberdesc}[ZipInfo]{create_system}
275 System which created ZIP archive.
276\end{memberdesc}
277
278\begin{memberdesc}[ZipInfo]{create_version}
279 PKZIP version which created ZIP archive.
280\end{memberdesc}
281
282\begin{memberdesc}[ZipInfo]{extract_version}
283 PKZIP version needed to extract archive.
284\end{memberdesc}
285
286\begin{memberdesc}[ZipInfo]{reserved}
287 Must be zero.
288\end{memberdesc}
289
290\begin{memberdesc}[ZipInfo]{flag_bits}
291 ZIP flag bits.
292\end{memberdesc}
293
294\begin{memberdesc}[ZipInfo]{volume}
295 Volume number of file header.
296\end{memberdesc}
297
298\begin{memberdesc}[ZipInfo]{internal_attr}
299 Internal attributes.
300\end{memberdesc}
301
302\begin{memberdesc}[ZipInfo]{external_attr}
303 External file attributes.
304\end{memberdesc}
305
306\begin{memberdesc}[ZipInfo]{header_offset}
307 Byte offset to the file header.
308\end{memberdesc}
309
Fred Drake42780242000-10-02 20:56:30 +0000310\begin{memberdesc}[ZipInfo]{CRC}
311 CRC-32 of the uncompressed file.
312\end{memberdesc}
313
314\begin{memberdesc}[ZipInfo]{compress_size}
315 Size of the compressed data.
316\end{memberdesc}
317
318\begin{memberdesc}[ZipInfo]{file_size}
319 Size of the uncompressed file.
320\end{memberdesc}