Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :mod:`zipfile` --- Work with ZIP archives |
| 2 | ========================================= |
| 3 | |
| 4 | .. module:: zipfile |
| 5 | :synopsis: Read and write ZIP-format archive files. |
| 6 | .. moduleauthor:: James C. Ahlstrom <jim@interet.com> |
| 7 | .. sectionauthor:: James C. Ahlstrom <jim@interet.com> |
| 8 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 9 | .. versionadded:: 1.6 |
| 10 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 11 | **Source code:** :source:`Lib/zipfile.py` |
| 12 | |
| 13 | -------------- |
| 14 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 15 | The ZIP file format is a common archive and compression standard. This module |
| 16 | provides tools to create, read, write, append, and list a ZIP file. Any |
| 17 | advanced use of this module will require an understanding of the format, as |
| 18 | defined in `PKZIP Application Note |
Georg Brandl | 0267781 | 2008-03-15 00:20:19 +0000 | [diff] [blame] | 19 | <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 20 | |
Georg Brandl | 7d4bfb3 | 2010-08-02 21:44:25 +0000 | [diff] [blame] | 21 | This module does not currently handle multi-disk ZIP files. |
| 22 | It can handle ZIP files that use the ZIP64 extensions |
Mark Summerfield | 91f9429 | 2007-11-05 14:38:50 +0000 | [diff] [blame] | 23 | (that is ZIP files that are more than 4 GByte in size). It supports |
| 24 | decryption of encrypted files in ZIP archives, but it currently cannot |
Gregory P. Smith | da40723 | 2008-01-20 01:32:00 +0000 | [diff] [blame] | 25 | create an encrypted file. Decryption is extremely slow as it is |
Georg Brandl | 8c18a47 | 2009-11-18 19:39:14 +0000 | [diff] [blame] | 26 | implemented in native Python rather than C. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 27 | |
Mark Summerfield | aea6e59 | 2007-11-05 09:22:48 +0000 | [diff] [blame] | 28 | The module defines the following items: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 29 | |
| 30 | .. exception:: BadZipfile |
| 31 | |
| 32 | The error raised for bad ZIP files (old name: ``zipfile.error``). |
| 33 | |
| 34 | |
| 35 | .. exception:: LargeZipFile |
| 36 | |
| 37 | The error raised when a ZIP file would require ZIP64 functionality but that has |
| 38 | not been enabled. |
| 39 | |
| 40 | |
| 41 | .. class:: ZipFile |
Georg Brandl | 3b85b9b | 2010-11-26 08:20:18 +0000 | [diff] [blame] | 42 | :noindex: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 43 | |
| 44 | The class for reading and writing ZIP files. See section |
| 45 | :ref:`zipfile-objects` for constructor details. |
| 46 | |
| 47 | |
| 48 | .. class:: PyZipFile |
| 49 | |
| 50 | Class for creating ZIP archives containing Python libraries. |
| 51 | |
| 52 | |
| 53 | .. class:: ZipInfo([filename[, date_time]]) |
| 54 | |
| 55 | Class used to represent information about a member of an archive. Instances |
Andrew Svetlov | 8c44193 | 2012-10-06 18:01:05 +0300 | [diff] [blame] | 56 | of this class are returned by the :meth:`.getinfo` and :meth:`.infolist` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 57 | methods of :class:`ZipFile` objects. Most users of the :mod:`zipfile` module |
| 58 | will not need to create these, but only use those created by this |
| 59 | module. *filename* should be the full name of the archive member, and |
| 60 | *date_time* should be a tuple containing six fields which describe the time |
| 61 | of the last modification to the file; the fields are described in section |
| 62 | :ref:`zipinfo-objects`. |
| 63 | |
| 64 | |
| 65 | .. function:: is_zipfile(filename) |
| 66 | |
| 67 | Returns ``True`` if *filename* is a valid ZIP file based on its magic number, |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 68 | otherwise returns ``False``. *filename* may be a file or file-like object too. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | 6f193e0 | 2008-12-27 15:43:12 +0000 | [diff] [blame] | 70 | .. versionchanged:: 2.7 |
Benjamin Peterson | fbaeca7 | 2008-12-27 22:18:58 +0000 | [diff] [blame] | 71 | Support for file and file-like objects. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 72 | |
| 73 | .. data:: ZIP_STORED |
| 74 | |
| 75 | The numeric constant for an uncompressed archive member. |
| 76 | |
| 77 | |
| 78 | .. data:: ZIP_DEFLATED |
| 79 | |
| 80 | The numeric constant for the usual ZIP compression method. This requires the |
Andrew Svetlov | 8c44193 | 2012-10-06 18:01:05 +0300 | [diff] [blame] | 81 | :mod:`zlib` module. No other compression methods are currently supported. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | .. seealso:: |
| 85 | |
Georg Brandl | 0267781 | 2008-03-15 00:20:19 +0000 | [diff] [blame] | 86 | `PKZIP Application Note <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 87 | Documentation on the ZIP file format by Phil Katz, the creator of the format and |
| 88 | algorithms used. |
| 89 | |
| 90 | `Info-ZIP Home Page <http://www.info-zip.org/>`_ |
| 91 | Information about the Info-ZIP project's ZIP archive programs and development |
| 92 | libraries. |
| 93 | |
| 94 | |
| 95 | .. _zipfile-objects: |
| 96 | |
| 97 | ZipFile Objects |
| 98 | --------------- |
| 99 | |
| 100 | |
| 101 | .. class:: ZipFile(file[, mode[, compression[, allowZip64]]]) |
| 102 | |
| 103 | Open a ZIP file, where *file* can be either a path to a file (a string) or a |
| 104 | file-like object. The *mode* parameter should be ``'r'`` to read an existing |
| 105 | file, ``'w'`` to truncate and write a new file, or ``'a'`` to append to an |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 106 | existing file. If *mode* is ``'a'`` and *file* refers to an existing ZIP |
| 107 | file, then additional files are added to it. If *file* does not refer to a |
| 108 | ZIP file, then a new ZIP archive is appended to the file. This is meant for |
| 109 | adding a ZIP archive to another file (such as :file:`python.exe`). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 110 | |
| 111 | .. versionchanged:: 2.6 |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 112 | If *mode* is ``a`` and the file does not exist at all, it is created. |
| 113 | |
| 114 | *compression* is the ZIP compression method to use when writing the archive, |
| 115 | and should be :const:`ZIP_STORED` or :const:`ZIP_DEFLATED`; unrecognized |
| 116 | values will cause :exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED` |
| 117 | is specified but the :mod:`zlib` module is not available, :exc:`RuntimeError` |
| 118 | is also raised. The default is :const:`ZIP_STORED`. If *allowZip64* is |
| 119 | ``True`` zipfile will create ZIP files that use the ZIP64 extensions when |
| 120 | the zipfile is larger than 2 GB. If it is false (the default) :mod:`zipfile` |
| 121 | will raise an exception when the ZIP file would require ZIP64 extensions. |
| 122 | ZIP64 extensions are disabled by default because the default :program:`zip` |
| 123 | and :program:`unzip` commands on Unix (the InfoZIP utilities) don't support |
| 124 | these extensions. |
| 125 | |
Georg Brandl | da29acd | 2010-12-21 17:58:06 +0000 | [diff] [blame] | 126 | .. versionchanged:: 2.7.1 |
| 127 | If the file is created with mode ``'a'`` or ``'w'`` and then |
Andrew Svetlov | 8c44193 | 2012-10-06 18:01:05 +0300 | [diff] [blame] | 128 | :meth:`closed <close>` without adding any files to the archive, the appropriate |
Georg Brandl | da29acd | 2010-12-21 17:58:06 +0000 | [diff] [blame] | 129 | ZIP structures for an empty archive will be written to the file. |
Georg Brandl | 86e0c89 | 2010-11-26 07:22:28 +0000 | [diff] [blame] | 130 | |
Ezio Melotti | 569e61f | 2009-12-30 06:14:51 +0000 | [diff] [blame] | 131 | ZipFile is also a context manager and therefore supports the |
| 132 | :keyword:`with` statement. In the example, *myzip* is closed after the |
| 133 | :keyword:`with` statement's suite is finished---even if an exception occurs:: |
| 134 | |
| 135 | with ZipFile('spam.zip', 'w') as myzip: |
| 136 | myzip.write('eggs.txt') |
| 137 | |
| 138 | .. versionadded:: 2.7 |
| 139 | Added the ability to use :class:`ZipFile` as a context manager. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | .. method:: ZipFile.close() |
| 143 | |
| 144 | Close the archive file. You must call :meth:`close` before exiting your program |
| 145 | or essential records will not be written. |
| 146 | |
| 147 | |
| 148 | .. method:: ZipFile.getinfo(name) |
| 149 | |
| 150 | Return a :class:`ZipInfo` object with information about the archive member |
| 151 | *name*. Calling :meth:`getinfo` for a name not currently contained in the |
| 152 | archive will raise a :exc:`KeyError`. |
| 153 | |
| 154 | |
| 155 | .. method:: ZipFile.infolist() |
| 156 | |
| 157 | Return a list containing a :class:`ZipInfo` object for each member of the |
| 158 | archive. The objects are in the same order as their entries in the actual ZIP |
| 159 | file on disk if an existing archive was opened. |
| 160 | |
| 161 | |
| 162 | .. method:: ZipFile.namelist() |
| 163 | |
| 164 | Return a list of archive members by name. |
| 165 | |
R David Murray | 5618aaa | 2012-08-15 11:15:39 -0400 | [diff] [blame] | 166 | .. index:: |
| 167 | single: universal newlines; zipfile.ZipFile.open method |
| 168 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 169 | |
| 170 | .. method:: ZipFile.open(name[, mode[, pwd]]) |
| 171 | |
| 172 | Extract a member from the archive as a file-like object (ZipExtFile). *name* is |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 173 | the name of the file in the archive, or a :class:`ZipInfo` object. The *mode* |
R David Murray | 5618aaa | 2012-08-15 11:15:39 -0400 | [diff] [blame] | 174 | parameter, if included, must be one of the following: ``'r'`` (the default), |
| 175 | ``'U'``, or ``'rU'``. Choosing ``'U'`` or ``'rU'`` will enable |
| 176 | :term:`universal newline <universal newlines>` |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 177 | support in the read-only object. *pwd* is the password used for encrypted files. |
Andrew Svetlov | 8c44193 | 2012-10-06 18:01:05 +0300 | [diff] [blame] | 178 | Calling :meth:`.open` on a closed ZipFile will raise a :exc:`RuntimeError`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 179 | |
| 180 | .. note:: |
| 181 | |
| 182 | The file-like object is read-only and provides the following methods: |
Serhiy Storchaka | b33336f | 2013-10-13 23:09:00 +0300 | [diff] [blame] | 183 | :meth:`~file.read`, :meth:`~file.readline`, |
| 184 | :meth:`~file.readlines`, :meth:`__iter__`, |
| 185 | :meth:`~object.next`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 186 | |
| 187 | .. note:: |
| 188 | |
Serhiy Storchaka | c328d11 | 2015-01-26 13:45:04 +0200 | [diff] [blame^] | 189 | If the ZipFile was created by passing in a file-like object as the first |
| 190 | argument to the constructor, then the object returned by :meth:`.open` shares the |
| 191 | ZipFile's file pointer. Under these circumstances, the object returned by |
| 192 | :meth:`.open` should not be used after any additional operations are performed |
| 193 | on the ZipFile object. If the ZipFile was created by passing in a string (the |
| 194 | filename) as the first argument to the constructor, then :meth:`.open` will |
| 195 | create a new file object that will be held by the ZipExtFile, allowing it to |
| 196 | operate independently of the ZipFile. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 197 | |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 198 | .. note:: |
| 199 | |
Andrew Svetlov | 8c44193 | 2012-10-06 18:01:05 +0300 | [diff] [blame] | 200 | The :meth:`.open`, :meth:`read` and :meth:`extract` methods can take a filename |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 201 | or a :class:`ZipInfo` object. You will appreciate this when trying to read a |
| 202 | ZIP file that contains members with duplicate names. |
| 203 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 204 | .. versionadded:: 2.6 |
| 205 | |
| 206 | |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 207 | .. method:: ZipFile.extract(member[, path[, pwd]]) |
| 208 | |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 209 | Extract a member from the archive to the current working directory; *member* |
| 210 | must be its full name or a :class:`ZipInfo` object). Its file information is |
| 211 | extracted as accurately as possible. *path* specifies a different directory |
| 212 | to extract to. *member* can be a filename or a :class:`ZipInfo` object. |
| 213 | *pwd* is the password used for encrypted files. |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 214 | |
| 215 | .. versionadded:: 2.6 |
| 216 | |
Gregory P. Smith | a26ec65 | 2013-02-07 22:11:03 -0800 | [diff] [blame] | 217 | .. note:: |
| 218 | |
| 219 | If a member filename is an absolute path, a drive/UNC sharepoint and |
| 220 | leading (back)slashes will be stripped, e.g.: ``///foo/bar`` becomes |
| 221 | ``foo/bar`` on Unix, and ``C:\foo\bar`` becomes ``foo\bar`` on Windows. |
| 222 | And all ``".."`` components in a member filename will be removed, e.g.: |
| 223 | ``../../foo../../ba..r`` becomes ``foo../ba..r``. On Windows illegal |
| 224 | characters (``:``, ``<``, ``>``, ``|``, ``"``, ``?``, and ``*``) |
| 225 | replaced by underscore (``_``). |
| 226 | |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 227 | |
| 228 | .. method:: ZipFile.extractall([path[, members[, pwd]]]) |
| 229 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 230 | Extract all members from the archive to the current working directory. *path* |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 231 | specifies a different directory to extract to. *members* is optional and must |
| 232 | be a subset of the list returned by :meth:`namelist`. *pwd* is the password |
| 233 | used for encrypted files. |
| 234 | |
Gregory P. Smith | 657024c | 2009-09-29 21:56:31 +0000 | [diff] [blame] | 235 | .. warning:: |
| 236 | |
| 237 | Never extract archives from untrusted sources without prior inspection. |
| 238 | It is possible that files are created outside of *path*, e.g. members |
| 239 | that have absolute filenames starting with ``"/"`` or filenames with two |
| 240 | dots ``".."``. |
Gregory P. Smith | ec02217 | 2013-02-07 22:18:21 -0800 | [diff] [blame] | 241 | |
Gregory P. Smith | a26ec65 | 2013-02-07 22:11:03 -0800 | [diff] [blame] | 242 | .. versionchanged:: 2.7.4 |
| 243 | The zipfile module attempts to prevent that. See :meth:`extract` note. |
Gregory P. Smith | 657024c | 2009-09-29 21:56:31 +0000 | [diff] [blame] | 244 | |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 245 | .. versionadded:: 2.6 |
| 246 | |
| 247 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 248 | .. method:: ZipFile.printdir() |
| 249 | |
| 250 | Print a table of contents for the archive to ``sys.stdout``. |
| 251 | |
| 252 | |
| 253 | .. method:: ZipFile.setpassword(pwd) |
| 254 | |
| 255 | Set *pwd* as default password to extract encrypted files. |
| 256 | |
| 257 | .. versionadded:: 2.6 |
| 258 | |
| 259 | |
| 260 | .. method:: ZipFile.read(name[, pwd]) |
| 261 | |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 262 | Return the bytes of the file *name* in the archive. *name* is the name of the |
| 263 | file in the archive, or a :class:`ZipInfo` object. The archive must be open for |
| 264 | read or append. *pwd* is the password used for encrypted files and, if specified, |
| 265 | it will override the default password set with :meth:`setpassword`. Calling |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 266 | :meth:`read` on a closed ZipFile will raise a :exc:`RuntimeError`. |
| 267 | |
| 268 | .. versionchanged:: 2.6 |
Georg Brandl | 112aa50 | 2008-05-20 08:25:48 +0000 | [diff] [blame] | 269 | *pwd* was added, and *name* can now be a :class:`ZipInfo` object. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 270 | |
| 271 | |
| 272 | .. method:: ZipFile.testzip() |
| 273 | |
| 274 | Read all the files in the archive and check their CRC's and file headers. |
| 275 | Return the name of the first bad file, or else return ``None``. Calling |
| 276 | :meth:`testzip` on a closed ZipFile will raise a :exc:`RuntimeError`. |
| 277 | |
| 278 | |
| 279 | .. method:: ZipFile.write(filename[, arcname[, compress_type]]) |
| 280 | |
| 281 | Write the file named *filename* to the archive, giving it the archive name |
| 282 | *arcname* (by default, this will be the same as *filename*, but without a drive |
| 283 | letter and with leading path separators removed). If given, *compress_type* |
| 284 | overrides the value given for the *compression* parameter to the constructor for |
| 285 | the new entry. The archive must be open with mode ``'w'`` or ``'a'`` -- calling |
| 286 | :meth:`write` on a ZipFile created with mode ``'r'`` will raise a |
| 287 | :exc:`RuntimeError`. Calling :meth:`write` on a closed ZipFile will raise a |
| 288 | :exc:`RuntimeError`. |
| 289 | |
| 290 | .. note:: |
| 291 | |
| 292 | There is no official file name encoding for ZIP files. If you have unicode file |
Georg Brandl | f11ed15 | 2007-08-30 10:09:42 +0000 | [diff] [blame] | 293 | names, you must convert them to byte strings in your desired encoding before |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 294 | passing them to :meth:`write`. WinZip interprets all file names as encoded in |
| 295 | CP437, also known as DOS Latin. |
| 296 | |
| 297 | .. note:: |
| 298 | |
| 299 | Archive names should be relative to the archive root, that is, they should not |
| 300 | start with a path separator. |
| 301 | |
| 302 | .. note:: |
| 303 | |
| 304 | If ``arcname`` (or ``filename``, if ``arcname`` is not given) contains a null |
| 305 | byte, the name of the file in the archive will be truncated at the null byte. |
| 306 | |
| 307 | |
Ronald Oussoren | dd25e86 | 2010-02-07 20:18:02 +0000 | [diff] [blame] | 308 | .. method:: ZipFile.writestr(zinfo_or_arcname, bytes[, compress_type]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 309 | |
| 310 | Write the string *bytes* to the archive; *zinfo_or_arcname* is either the file |
| 311 | name it will be given in the archive, or a :class:`ZipInfo` instance. If it's |
| 312 | an instance, at least the filename, date, and time must be given. If it's a |
| 313 | name, the date and time is set to the current date and time. The archive must be |
| 314 | opened with mode ``'w'`` or ``'a'`` -- calling :meth:`writestr` on a ZipFile |
| 315 | created with mode ``'r'`` will raise a :exc:`RuntimeError`. Calling |
| 316 | :meth:`writestr` on a closed ZipFile will raise a :exc:`RuntimeError`. |
| 317 | |
Ronald Oussoren | dd25e86 | 2010-02-07 20:18:02 +0000 | [diff] [blame] | 318 | If given, *compress_type* overrides the value given for the *compression* |
| 319 | parameter to the constructor for the new entry, or in the *zinfo_or_arcname* |
| 320 | (if that is a :class:`ZipInfo` instance). |
| 321 | |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 322 | .. note:: |
| 323 | |
Éric Araujo | 8e726b4 | 2010-12-26 17:55:02 +0000 | [diff] [blame] | 324 | When passing a :class:`ZipInfo` instance as the *zinfo_or_arcname* parameter, |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 325 | the compression method used will be that specified in the *compress_type* |
| 326 | member of the given :class:`ZipInfo` instance. By default, the |
Georg Brandl | 62416bc | 2008-01-07 18:47:44 +0000 | [diff] [blame] | 327 | :class:`ZipInfo` constructor sets this member to :const:`ZIP_STORED`. |
| 328 | |
Ronald Oussoren | dd25e86 | 2010-02-07 20:18:02 +0000 | [diff] [blame] | 329 | .. versionchanged:: 2.7 |
Andrew Svetlov | 8c44193 | 2012-10-06 18:01:05 +0300 | [diff] [blame] | 330 | The *compress_type* argument. |
Ronald Oussoren | dd25e86 | 2010-02-07 20:18:02 +0000 | [diff] [blame] | 331 | |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 332 | The following data attributes are also available: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 333 | |
| 334 | |
| 335 | .. attribute:: ZipFile.debug |
| 336 | |
| 337 | The level of debug output to use. This may be set from ``0`` (the default, no |
| 338 | output) to ``3`` (the most output). Debugging information is written to |
| 339 | ``sys.stdout``. |
| 340 | |
Martin v. Löwis | 8c43641 | 2008-07-03 12:51:14 +0000 | [diff] [blame] | 341 | .. attribute:: ZipFile.comment |
| 342 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 343 | The comment text associated with the ZIP file. If assigning a comment to a |
| 344 | :class:`ZipFile` instance created with mode 'a' or 'w', this should be a |
| 345 | string no longer than 65535 bytes. Comments longer than this will be |
Andrew Svetlov | 8c44193 | 2012-10-06 18:01:05 +0300 | [diff] [blame] | 346 | truncated in the written archive when :meth:`.close` is called. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 347 | |
| 348 | .. _pyzipfile-objects: |
| 349 | |
| 350 | PyZipFile Objects |
| 351 | ----------------- |
| 352 | |
| 353 | The :class:`PyZipFile` constructor takes the same parameters as the |
| 354 | :class:`ZipFile` constructor. Instances have one method in addition to those of |
| 355 | :class:`ZipFile` objects. |
| 356 | |
| 357 | |
| 358 | .. method:: PyZipFile.writepy(pathname[, basename]) |
| 359 | |
| 360 | Search for files :file:`\*.py` and add the corresponding file to the archive. |
| 361 | The corresponding file is a :file:`\*.pyo` file if available, else a |
| 362 | :file:`\*.pyc` file, compiling if necessary. If the pathname is a file, the |
| 363 | filename must end with :file:`.py`, and just the (corresponding |
| 364 | :file:`\*.py[co]`) file is added at the top level (no path information). If the |
| 365 | pathname is a file that does not end with :file:`.py`, a :exc:`RuntimeError` |
| 366 | will be raised. If it is a directory, and the directory is not a package |
| 367 | directory, then all the files :file:`\*.py[co]` are added at the top level. If |
| 368 | the directory is a package directory, then all :file:`\*.py[co]` are added under |
| 369 | the package name as a file path, and if any subdirectories are package |
| 370 | directories, all of these are added recursively. *basename* is intended for |
| 371 | internal use only. The :meth:`writepy` method makes archives with file names |
| 372 | like this:: |
| 373 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 374 | string.pyc # Top level name |
| 375 | test/__init__.pyc # Package directory |
Brett Cannon | 3c75914 | 2008-05-09 05:25:37 +0000 | [diff] [blame] | 376 | test/test_support.pyc # Module test.test_support |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 377 | test/bogus/__init__.pyc # Subpackage directory |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 378 | test/bogus/myfile.pyc # Submodule test.bogus.myfile |
| 379 | |
| 380 | |
| 381 | .. _zipinfo-objects: |
| 382 | |
| 383 | ZipInfo Objects |
| 384 | --------------- |
| 385 | |
Andrew Svetlov | 8c44193 | 2012-10-06 18:01:05 +0300 | [diff] [blame] | 386 | Instances of the :class:`ZipInfo` class are returned by the :meth:`.getinfo` and |
| 387 | :meth:`.infolist` methods of :class:`ZipFile` objects. Each object stores |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 388 | information about a single member of the ZIP archive. |
| 389 | |
| 390 | Instances have the following attributes: |
| 391 | |
| 392 | |
| 393 | .. attribute:: ZipInfo.filename |
| 394 | |
| 395 | Name of the file in the archive. |
| 396 | |
| 397 | |
| 398 | .. attribute:: ZipInfo.date_time |
| 399 | |
| 400 | The time and date of the last modification to the archive member. This is a |
| 401 | tuple of six values: |
| 402 | |
| 403 | +-------+--------------------------+ |
| 404 | | Index | Value | |
| 405 | +=======+==========================+ |
Senthil Kumaran | ddd4031 | 2011-10-20 01:38:35 +0800 | [diff] [blame] | 406 | | ``0`` | Year (>= 1980) | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 407 | +-------+--------------------------+ |
| 408 | | ``1`` | Month (one-based) | |
| 409 | +-------+--------------------------+ |
| 410 | | ``2`` | Day of month (one-based) | |
| 411 | +-------+--------------------------+ |
| 412 | | ``3`` | Hours (zero-based) | |
| 413 | +-------+--------------------------+ |
| 414 | | ``4`` | Minutes (zero-based) | |
| 415 | +-------+--------------------------+ |
| 416 | | ``5`` | Seconds (zero-based) | |
| 417 | +-------+--------------------------+ |
| 418 | |
Senthil Kumaran | ddd4031 | 2011-10-20 01:38:35 +0800 | [diff] [blame] | 419 | .. note:: |
| 420 | |
| 421 | The ZIP file format does not support timestamps before 1980. |
| 422 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 423 | |
| 424 | .. attribute:: ZipInfo.compress_type |
| 425 | |
| 426 | Type of compression for the archive member. |
| 427 | |
| 428 | |
| 429 | .. attribute:: ZipInfo.comment |
| 430 | |
| 431 | Comment for the individual archive member. |
| 432 | |
| 433 | |
| 434 | .. attribute:: ZipInfo.extra |
| 435 | |
| 436 | Expansion field data. The `PKZIP Application Note |
Georg Brandl | 0267781 | 2008-03-15 00:20:19 +0000 | [diff] [blame] | 437 | <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_ contains |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 438 | some comments on the internal structure of the data contained in this string. |
| 439 | |
| 440 | |
| 441 | .. attribute:: ZipInfo.create_system |
| 442 | |
| 443 | System which created ZIP archive. |
| 444 | |
| 445 | |
| 446 | .. attribute:: ZipInfo.create_version |
| 447 | |
| 448 | PKZIP version which created ZIP archive. |
| 449 | |
| 450 | |
| 451 | .. attribute:: ZipInfo.extract_version |
| 452 | |
| 453 | PKZIP version needed to extract archive. |
| 454 | |
| 455 | |
| 456 | .. attribute:: ZipInfo.reserved |
| 457 | |
| 458 | Must be zero. |
| 459 | |
| 460 | |
| 461 | .. attribute:: ZipInfo.flag_bits |
| 462 | |
| 463 | ZIP flag bits. |
| 464 | |
| 465 | |
| 466 | .. attribute:: ZipInfo.volume |
| 467 | |
| 468 | Volume number of file header. |
| 469 | |
| 470 | |
| 471 | .. attribute:: ZipInfo.internal_attr |
| 472 | |
| 473 | Internal attributes. |
| 474 | |
| 475 | |
| 476 | .. attribute:: ZipInfo.external_attr |
| 477 | |
| 478 | External file attributes. |
| 479 | |
| 480 | |
| 481 | .. attribute:: ZipInfo.header_offset |
| 482 | |
| 483 | Byte offset to the file header. |
| 484 | |
| 485 | |
| 486 | .. attribute:: ZipInfo.CRC |
| 487 | |
| 488 | CRC-32 of the uncompressed file. |
| 489 | |
| 490 | |
| 491 | .. attribute:: ZipInfo.compress_size |
| 492 | |
| 493 | Size of the compressed data. |
| 494 | |
| 495 | |
| 496 | .. attribute:: ZipInfo.file_size |
| 497 | |
| 498 | Size of the uncompressed file. |
| 499 | |