Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 1 | \section{\module{zipfile} --- |
| 2 | Work with ZIP archives} |
| 3 | |
Fred Drake | 1675375 | 2000-09-18 16:21:11 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{zipfile} |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 5 | \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 Drake | 6300bd4 | 2000-09-07 14:01:40 +0000 | [diff] [blame] | 10 | \versionadded{1.6} |
| 11 | |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 12 | The ZIP file format is a common archive and compression standard. |
| 13 | This module provides tools to create, read, write, append, and list a |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 14 | ZIP file. Any advanced use of this module will require an |
| 15 | understanding of the format, as defined in |
| 16 | \citetitle[http://www.pkware.com/appnote.html]{PKZIP Application |
| 17 | Note}. |
| 18 | |
| 19 | This module does not currently handle ZIP files which have appended |
| 20 | comments, or multi-disk ZIP files. |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 21 | |
| 22 | The available attributes of this module are: |
| 23 | |
| 24 | \begin{excdesc}{error} |
| 25 | The error raised for bad ZIP files. |
| 26 | \end{excdesc} |
| 27 | |
Fred Drake | 96d7a70 | 2001-05-11 01:08:13 +0000 | [diff] [blame] | 28 | \begin{classdesc*}{ZipFile} |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 29 | The class for reading and writing ZIP files. See |
| 30 | ``\citetitle{ZipFile Objects}'' (section \ref{zipfile-objects}) for |
| 31 | constructor details. |
Fred Drake | 886f113 | 2001-05-11 15:49:19 +0000 | [diff] [blame] | 32 | \end{classdesc*} |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 33 | |
Fred Drake | 96d7a70 | 2001-05-11 01:08:13 +0000 | [diff] [blame] | 34 | \begin{classdesc*}{PyZipFile} |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 35 | Class for creating ZIP archives containing Python libraries. |
Fred Drake | 886f113 | 2001-05-11 15:49:19 +0000 | [diff] [blame] | 36 | \end{classdesc*} |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 37 | |
| 38 | \begin{classdesc}{ZipInfo}{\optional{filename\optional{, date_time}}} |
Raymond Hettinger | 6880431 | 2005-01-01 00:28:46 +0000 | [diff] [blame] | 39 | Class used to represent information about a member of an archive. |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 40 | Instances of this class are returned by the \method{getinfo()} and |
Fred Drake | e35360f | 2000-10-03 15:16:31 +0000 | [diff] [blame] | 41 | \method{infolist()} methods of \class{ZipFile} objects. Most users |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 42 | of the \module{zipfile} module will not need to create these, but |
| 43 | only use those created by this module. |
| 44 | \var{filename} should be the full name of the archive member, and |
| 45 | \var{date_time} should be a tuple containing six fields which |
| 46 | describe the time of the last modification to the file; the fields |
| 47 | are described in section \ref{zipinfo-objects}, ``ZipInfo Objects.'' |
| 48 | \end{classdesc} |
| 49 | |
Fred Drake | 5d63a39 | 2000-10-06 15:29:56 +0000 | [diff] [blame] | 50 | \begin{funcdesc}{is_zipfile}{filename} |
Neal Norwitz | 6b35370 | 2002-04-09 18:15:00 +0000 | [diff] [blame] | 51 | Returns \code{True} if \var{filename} is a valid ZIP file based on its magic |
| 52 | number, otherwise returns \code{False}. This module does not currently |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 53 | handle ZIP files which have appended comments. |
| 54 | \end{funcdesc} |
| 55 | |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 56 | \begin{datadesc}{ZIP_STORED} |
Fred Drake | 5d63a39 | 2000-10-06 15:29:56 +0000 | [diff] [blame] | 57 | The numeric constant for an uncompressed archive member. |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 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} |
Fred Drake | 58295de | 2000-09-30 00:11:45 +0000 | [diff] [blame] | 68 | \seetitle[http://www.pkware.com/appnote.html]{PKZIP Application |
| 69 | Note}{Documentation on the ZIP file format by Phil |
| 70 | Katz, the creator of the format and algorithms used.} |
| 71 | |
| 72 | \seetitle[http://www.info-zip.org/pub/infozip/]{Info-ZIP Home Page}{ |
| 73 | Information about the Info-ZIP project's ZIP archive |
| 74 | programs and development libraries.} |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 75 | \end{seealso} |
| 76 | |
| 77 | |
| 78 | \subsection{ZipFile Objects \label{zipfile-objects}} |
| 79 | |
Fred Drake | bda3a59 | 2001-05-09 19:57:37 +0000 | [diff] [blame] | 80 | \begin{classdesc}{ZipFile}{file\optional{, mode\optional{, compression}}} |
| 81 | Open a ZIP file, where \var{file} can be either a path to a file |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 82 | (a string) or a file-like object. The \var{mode} parameter |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 83 | should be \code{'r'} to read an existing file, \code{'w'} to |
| 84 | truncate and write a new file, or \code{'a'} to append to an |
Fred Drake | bda3a59 | 2001-05-09 19:57:37 +0000 | [diff] [blame] | 85 | existing file. For \var{mode} is \code{'a'} and \var{file} |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 86 | refers to an existing ZIP file, then additional files are added to |
Fred Drake | bda3a59 | 2001-05-09 19:57:37 +0000 | [diff] [blame] | 87 | it. If \var{file} does not refer to a ZIP file, then a new ZIP |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 88 | archive is appended to the file. This is meant for adding a ZIP |
| 89 | archive to another file, such as \file{python.exe}. Using |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 90 | |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 91 | \begin{verbatim} |
| 92 | cat myzip.zip >> python.exe |
| 93 | \end{verbatim} |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 94 | |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 95 | also works, and at least \program{WinZip} can read such files. |
| 96 | \var{compression} is the ZIP compression method to use when writing |
| 97 | the archive, and should be \constant{ZIP_STORED} or |
| 98 | \constant{ZIP_DEFLATED}; unrecognized values will cause |
Fred Drake | e35360f | 2000-10-03 15:16:31 +0000 | [diff] [blame] | 99 | \exception{RuntimeError} to be raised. If \constant{ZIP_DEFLATED} |
Thomas Heller | 3d62f8c | 2002-01-14 08:37:39 +0000 | [diff] [blame] | 100 | is specified but the \refmodule{zlib} module is not available, |
Fred Drake | e35360f | 2000-10-03 15:16:31 +0000 | [diff] [blame] | 101 | \exception{RuntimeError} is also raised. The default is |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 102 | \constant{ZIP_STORED}. |
| 103 | \end{classdesc} |
| 104 | |
Fred Drake | e35360f | 2000-10-03 15:16:31 +0000 | [diff] [blame] | 105 | \begin{methoddesc}{close}{} |
| 106 | Close the archive file. You must call \method{close()} before |
| 107 | exiting your program or essential records will not be written. |
| 108 | \end{methoddesc} |
| 109 | |
| 110 | \begin{methoddesc}{getinfo}{name} |
| 111 | Return a \class{ZipInfo} object with information about the archive |
| 112 | member \var{name}. |
| 113 | \end{methoddesc} |
| 114 | |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 115 | \begin{methoddesc}{infolist}{} |
| 116 | Return a list containing a \class{ZipInfo} object for each member of |
| 117 | the archive. The objects are in the same order as their entries in |
| 118 | the actual ZIP file on disk if an existing archive was opened. |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 119 | \end{methoddesc} |
| 120 | |
Fred Drake | 6fe9bac | 2000-10-11 18:56:00 +0000 | [diff] [blame] | 121 | \begin{methoddesc}{namelist}{} |
| 122 | Return a list of archive members by name. |
| 123 | \end{methoddesc} |
| 124 | |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 125 | \begin{methoddesc}{printdir}{} |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 126 | Print a table of contents for the archive to \code{sys.stdout}. |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 127 | \end{methoddesc} |
| 128 | |
| 129 | \begin{methoddesc}{read}{name} |
| 130 | Return the bytes of the file in the archive. The archive must be |
| 131 | open for read or append. |
| 132 | \end{methoddesc} |
| 133 | |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 134 | \begin{methoddesc}{testzip}{} |
| 135 | Read all the files in the archive and check their CRC's. Return the |
| 136 | name of the first bad file, or else return \code{None}. |
| 137 | \end{methoddesc} |
| 138 | |
Fred Drake | e35360f | 2000-10-03 15:16:31 +0000 | [diff] [blame] | 139 | \begin{methoddesc}{write}{filename\optional{, arcname\optional{, |
| 140 | compress_type}}} |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 141 | Write the file named \var{filename} to the archive, giving it the |
Fred Drake | e35360f | 2000-10-03 15:16:31 +0000 | [diff] [blame] | 142 | archive name \var{arcname} (by default, this will be the same as |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame^] | 143 | \var{filename}, but without a drive letter and with leading path |
| 144 | separators removed). If given, \var{compress_type} overrides the value |
Fred Drake | e35360f | 2000-10-03 15:16:31 +0000 | [diff] [blame] | 145 | given for the \var{compression} parameter to the constructor for |
| 146 | the new entry. The archive must be open with mode \code{'w'} or |
Georg Brandl | 8f7c54e | 2006-02-20 08:40:38 +0000 | [diff] [blame^] | 147 | \code{'a'}. |
| 148 | \note{Archive names should be relative to the archive root, that is, |
| 149 | they should not start with a path separator.} |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 150 | \end{methoddesc} |
| 151 | |
Just van Rossum | b083cb3 | 2002-12-12 12:23:32 +0000 | [diff] [blame] | 152 | \begin{methoddesc}{writestr}{zinfo_or_arcname, bytes} |
| 153 | Write the string \var{bytes} to the archive; \var{zinfo_or_arcname} |
| 154 | is either the file name it will be given in the archive, or a |
| 155 | \class{ZipInfo} instance. If it's an instance, at least the |
| 156 | filename, date, and time must be given. If it's a name, the date |
| 157 | and time is set to the current date and time. The archive must be |
| 158 | opened with mode \code{'w'} or \code{'a'}. |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 159 | \end{methoddesc} |
| 160 | |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 161 | |
| 162 | The following data attribute is also available: |
| 163 | |
| 164 | \begin{memberdesc}{debug} |
| 165 | The level of debug output to use. This may be set from \code{0} |
| 166 | (the default, no output) to \code{3} (the most output). Debugging |
| 167 | information is written to \code{sys.stdout}. |
| 168 | \end{memberdesc} |
| 169 | |
| 170 | |
| 171 | \subsection{PyZipFile Objects \label{pyzipfile-objects}} |
| 172 | |
| 173 | The \class{PyZipFile} constructor takes the same parameters as the |
| 174 | \class{ZipFile} constructor. Instances have one method in addition to |
| 175 | those of \class{ZipFile} objects. |
| 176 | |
| 177 | \begin{methoddesc}[PyZipFile]{writepy}{pathname\optional{, basename}} |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 178 | Search for files \file{*.py} and add the corresponding file to the |
| 179 | archive. The corresponding file is a \file{*.pyo} file if |
| 180 | available, else a \file{*.pyc} file, compiling if necessary. If the |
| 181 | pathname is a file, the filename must end with \file{.py}, and just |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 182 | the (corresponding \file{*.py[co]}) file is added at the top level |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 183 | (no path information). If it is a directory, and the directory is |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 184 | not a package directory, then all the files \file{*.py[co]} are |
Fred Drake | 3c9f936 | 2000-03-31 17:51:10 +0000 | [diff] [blame] | 185 | added at the top level. If the directory is a package directory, |
| 186 | then all \file{*.py[oc]} are added under the package name as a file |
| 187 | path, and if any subdirectories are package directories, all of |
| 188 | these are added recursively. \var{basename} is intended for |
| 189 | internal use only. The \method{writepy()} method makes archives |
| 190 | with file names like this: |
| 191 | |
| 192 | \begin{verbatim} |
| 193 | string.pyc # Top level name |
| 194 | test/__init__.pyc # Package directory |
| 195 | test/testall.pyc # Module test.testall |
| 196 | test/bogus/__init__.pyc # Subpackage directory |
| 197 | test/bogus/myfile.pyc # Submodule test.bogus.myfile |
| 198 | \end{verbatim} |
| 199 | \end{methoddesc} |
| 200 | |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 201 | |
| 202 | \subsection{ZipInfo Objects \label{zipinfo-objects}} |
| 203 | |
| 204 | Instances of the \class{ZipInfo} class are returned by the |
Fred Drake | e35360f | 2000-10-03 15:16:31 +0000 | [diff] [blame] | 205 | \method{getinfo()} and \method{infolist()} methods of |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 206 | \class{ZipFile} objects. Each object stores information about a |
| 207 | single member of the ZIP archive. |
| 208 | |
| 209 | Instances have the following attributes: |
| 210 | |
| 211 | \begin{memberdesc}[ZipInfo]{filename} |
| 212 | Name of the file in the archive. |
| 213 | \end{memberdesc} |
| 214 | |
| 215 | \begin{memberdesc}[ZipInfo]{date_time} |
Raymond Hettinger | 999b57c | 2003-08-25 04:28:05 +0000 | [diff] [blame] | 216 | The time and date of the last modification to the archive |
Fred Drake | 4278024 | 2000-10-02 20:56:30 +0000 | [diff] [blame] | 217 | member. This is a tuple of six values: |
| 218 | |
| 219 | \begin{tableii}{c|l}{code}{Index}{Value} |
| 220 | \lineii{0}{Year} |
| 221 | \lineii{1}{Month (one-based)} |
| 222 | \lineii{2}{Day of month (one-based)} |
| 223 | \lineii{3}{Hours (zero-based)} |
| 224 | \lineii{4}{Minutes (zero-based)} |
| 225 | \lineii{5}{Seconds (zero-based)} |
| 226 | \end{tableii} |
| 227 | \end{memberdesc} |
| 228 | |
| 229 | \begin{memberdesc}[ZipInfo]{compress_type} |
| 230 | Type of compression for the archive member. |
| 231 | \end{memberdesc} |
| 232 | |
| 233 | \begin{memberdesc}[ZipInfo]{comment} |
| 234 | Comment for the individual archive member. |
| 235 | \end{memberdesc} |
| 236 | |
| 237 | \begin{memberdesc}[ZipInfo]{extra} |
| 238 | Expansion field data. The |
| 239 | \citetitle[http://www.pkware.com/appnote.html]{PKZIP Application |
| 240 | Note} contains some comments on the internal structure of the data |
| 241 | contained in this string. |
| 242 | \end{memberdesc} |
| 243 | |
| 244 | \begin{memberdesc}[ZipInfo]{create_system} |
| 245 | System which created ZIP archive. |
| 246 | \end{memberdesc} |
| 247 | |
| 248 | \begin{memberdesc}[ZipInfo]{create_version} |
| 249 | PKZIP version which created ZIP archive. |
| 250 | \end{memberdesc} |
| 251 | |
| 252 | \begin{memberdesc}[ZipInfo]{extract_version} |
| 253 | PKZIP version needed to extract archive. |
| 254 | \end{memberdesc} |
| 255 | |
| 256 | \begin{memberdesc}[ZipInfo]{reserved} |
| 257 | Must be zero. |
| 258 | \end{memberdesc} |
| 259 | |
| 260 | \begin{memberdesc}[ZipInfo]{flag_bits} |
| 261 | ZIP flag bits. |
| 262 | \end{memberdesc} |
| 263 | |
| 264 | \begin{memberdesc}[ZipInfo]{volume} |
| 265 | Volume number of file header. |
| 266 | \end{memberdesc} |
| 267 | |
| 268 | \begin{memberdesc}[ZipInfo]{internal_attr} |
| 269 | Internal attributes. |
| 270 | \end{memberdesc} |
| 271 | |
| 272 | \begin{memberdesc}[ZipInfo]{external_attr} |
| 273 | External file attributes. |
| 274 | \end{memberdesc} |
| 275 | |
| 276 | \begin{memberdesc}[ZipInfo]{header_offset} |
| 277 | Byte offset to the file header. |
| 278 | \end{memberdesc} |
| 279 | |
| 280 | \begin{memberdesc}[ZipInfo]{file_offset} |
| 281 | Byte offset to the start of the file data. |
| 282 | \end{memberdesc} |
| 283 | |
| 284 | \begin{memberdesc}[ZipInfo]{CRC} |
| 285 | CRC-32 of the uncompressed file. |
| 286 | \end{memberdesc} |
| 287 | |
| 288 | \begin{memberdesc}[ZipInfo]{compress_size} |
| 289 | Size of the compressed data. |
| 290 | \end{memberdesc} |
| 291 | |
| 292 | \begin{memberdesc}[ZipInfo]{file_size} |
| 293 | Size of the uncompressed file. |
| 294 | \end{memberdesc} |