rest25/library/zipfile.rst => rest262/library/zipfile.rst
2:mod:`zipfile` --- Work with ZIP archives
3=========================================
4
5.. module:: zipfile
6   :synopsis: Read and write ZIP-format archive files.
7.. moduleauthor:: James C. Ahlstrom <jim@interet.com>
8.. sectionauthor:: James C. Ahlstrom <jim@interet.com>
9
n10- 
11-.. % LaTeX markup by Fred L. Drake, Jr. <fdrake@acm.org>
12- 
13.. versionadded:: 1.6
14
15The ZIP file format is a common archive and compression standard. This module
16provides tools to create, read, write, append, and list a ZIP file.  Any
17advanced use of this module will require an understanding of the format, as
18defined in `PKZIP Application Note
n19-<http://www.pkware.com/business_and_developers/developer/appnote/>`_.
n16+<http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_.
20
n21-This module does not currently handle ZIP files which have appended comments, or
n18+This module does not currently handle multi-disk ZIP files, or ZIP files
19+which have appended comments (although it correctly handles comments
20+added to individual archive members---for which see the :ref:`zipinfo-objects`
22-multi-disk ZIP files. It can handle ZIP files that use the  ZIP64 extensions
21+documentation). It can handle ZIP files that use the ZIP64 extensions
23-(that is ZIP files that are more than 4 GByte in size).
22+(that is ZIP files that are more than 4 GByte in size).  It supports
23+decryption of encrypted files in ZIP archives, but it currently cannot
24+create an encrypted file.  Decryption is extremely slow as it is
25+implemented in native python rather than C.
24
n25-The available attributes of this module are:
n27+For other archive formats, see the :mod:`bz2`, :mod:`gzip`, and
28+:mod:`tarfile` modules.
26
n30+The module defines the following items:
27
n28-.. exception:: error
n32+.. exception:: BadZipfile
29
n30-   The error raised for bad ZIP files.
n34+   The error raised for bad ZIP files (old name: ``zipfile.error``).
31
32
33.. exception:: LargeZipFile
34
35   The error raised when a ZIP file would require ZIP64 functionality but that has
36   not been enabled.
37
38
39.. class:: ZipFile
40
n41-   The class for reading and writing ZIP files.  See "ZipFile Objects" (section
n45+   The class for reading and writing ZIP files.  See section
42-   :ref:`zipfile-objects`) for constructor details.
46+   :ref:`zipfile-objects` for constructor details.
43
44
45.. class:: PyZipFile
46
47   Class for creating ZIP archives containing Python libraries.
48
49
50.. class:: ZipInfo([filename[, date_time]])
51
n52-   Class used to represent information about a member of an archive. Instances of
n56+   Class used to represent information about a member of an archive. Instances
53-   this class are returned by the :meth:`getinfo` and :meth:`infolist` methods of
57+   of this class are returned by the :meth:`getinfo` and :meth:`infolist`
54-   :class:`ZipFile` objects.  Most users of the :mod:`zipfile` module will not need
58+   methods of :class:`ZipFile` objects.  Most users of the :mod:`zipfile` module
55-   to create these, but only use those created by this module. *filename* should be
59+   will not need to create these, but only use those created by this
56-   the full name of the archive member, and *date_time* should be a tuple
60+   module. *filename* should be the full name of the archive member, and
57-   containing six fields which describe the time of the last modification to the
61+   *date_time* should be a tuple containing six fields which describe the time
58-   file; the fields are described in section :ref:`zipinfo-objects`, "ZipInfo
62+   of the last modification to the file; the fields are described in section
59-   Objects."
63+   :ref:`zipinfo-objects`.
60
61
62.. function:: is_zipfile(filename)
63
64   Returns ``True`` if *filename* is a valid ZIP file based on its magic number,
65   otherwise returns ``False``.  This module does not currently handle ZIP files
66   which have appended comments.
67
74.. data:: ZIP_DEFLATED
75
76   The numeric constant for the usual ZIP compression method.  This requires the
77   zlib module.  No other compression methods are currently supported.
78
79
80.. seealso::
81
n82-   `PKZIP Application Note <http://www.pkware.com/business_and_developers/developer/appnote/>`_
n86+   `PKZIP Application Note <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_
83      Documentation on the ZIP file format by Phil Katz, the creator of the format and
84      algorithms used.
85
n86-   `Info-ZIP Home Page <http://www.info-zip.org/pub/infozip/>`_
n90+   `Info-ZIP Home Page <http://www.info-zip.org/>`_
87      Information about the Info-ZIP project's ZIP archive programs and development
88      libraries.
89
90
91.. _zipfile-objects:
92
93ZipFile Objects
94---------------
95
96
97.. class:: ZipFile(file[, mode[, compression[, allowZip64]]])
98
99   Open a ZIP file, where *file* can be either a path to a file (a string) or a
100   file-like object.  The *mode* parameter should be ``'r'`` to read an existing
101   file, ``'w'`` to truncate and write a new file, or ``'a'`` to append to an
n102-   existing file.  For *mode* is ``'a'`` and *file* refers to an existing ZIP file,
n106+   existing file.  If *mode* is ``'a'`` and *file* refers to an existing ZIP file,
103   then additional files are added to it.  If *file* does not refer to a ZIP file,
104   then a new ZIP archive is appended to the file.  This is meant for adding a ZIP
105   archive to another file, such as :file:`python.exe`.  Using ::
106
107      cat myzip.zip >> python.exe
108
n109-   also works, and at least :program:`WinZip` can read such files. *compression* is
n113+   also works, and at least :program:`WinZip` can read such files. If *mode* is
114+   ``a`` and the file does not exist at all, it is created. *compression* is the
110-   the ZIP compression method to use when writing the archive, and should be
115+   ZIP compression method to use when writing the archive, and should be
111   :const:`ZIP_STORED` or :const:`ZIP_DEFLATED`; unrecognized values will cause
112   :exc:`RuntimeError` to be raised.  If :const:`ZIP_DEFLATED` is specified but the
113   :mod:`zlib` module is not available, :exc:`RuntimeError` is also raised.  The
114   default is :const:`ZIP_STORED`.  If *allowZip64* is ``True`` zipfile will create
115   ZIP files that use the ZIP64 extensions when the zipfile is larger than 2 GB. If
116   it is  false (the default) :mod:`zipfile` will raise an exception when the ZIP
117   file would require ZIP64 extensions. ZIP64 extensions are disabled by default
118   because the default :program:`zip` and :program:`unzip` commands on Unix (the
119   InfoZIP utilities) don't support these extensions.
120
n126+   .. versionchanged:: 2.6
127+      If the file does not exist, it is created if the mode is 'a'.
128+ 
121
122.. method:: ZipFile.close()
123
124   Close the archive file.  You must call :meth:`close` before exiting your program
125   or essential records will not be written.
126
127
128.. method:: ZipFile.getinfo(name)
129
130   Return a :class:`ZipInfo` object with information about the archive member
n131-   *name*.
n139+   *name*.  Calling :meth:`getinfo` for a name not currently contained in the
140+   archive will raise a :exc:`KeyError`.
132
133
134.. method:: ZipFile.infolist()
135
136   Return a list containing a :class:`ZipInfo` object for each member of the
137   archive.  The objects are in the same order as their entries in the actual ZIP
138   file on disk if an existing archive was opened.
139
140
141.. method:: ZipFile.namelist()
142
143   Return a list of archive members by name.
144
145
n155+.. method:: ZipFile.open(name[, mode[, pwd]])
156+ 
157+   Extract a member from the archive as a file-like object (ZipExtFile). *name* is
158+   the name of the file in the archive, or a :class:`ZipInfo` object. The *mode*
159+   parameter, if included, must be one of the following: ``'r'`` (the  default),
160+   ``'U'``, or ``'rU'``. Choosing ``'U'`` or  ``'rU'`` will enable universal newline
161+   support in the read-only object. *pwd* is the password used for encrypted files.
162+   Calling  :meth:`open` on a closed ZipFile will raise a  :exc:`RuntimeError`.
163+ 
164+   .. note::
165+ 
166+      The file-like object is read-only and provides the following methods:
167+      :meth:`read`, :meth:`readline`, :meth:`readlines`, :meth:`__iter__`,
168+      :meth:`next`.
169+ 
170+   .. note::
171+ 
172+      If the ZipFile was created by passing in a file-like object as the  first
173+      argument to the constructor, then the object returned by :meth:`.open` shares the
174+      ZipFile's file pointer.  Under these  circumstances, the object returned by
175+      :meth:`.open` should not  be used after any additional operations are performed
176+      on the  ZipFile object.  If the ZipFile was created by passing in a string (the
177+      filename) as the first argument to the constructor, then  :meth:`.open` will
178+      create a new file object that will be held by the ZipExtFile, allowing it to
179+      operate independently of the  ZipFile.
180+ 
181+   .. note::
182+ 
183+      The :meth:`open`, :meth:`read` and :meth:`extract` methods can take a filename
184+      or a :class:`ZipInfo` object.  You will appreciate this when trying to read a
185+      ZIP file that contains members with duplicate names.
186+ 
187+   .. versionadded:: 2.6
188+ 
189+ 
190+.. method:: ZipFile.extract(member[, path[, pwd]])
191+ 
192+   Extract a member from the archive to the current working directory; *member*
193+   must be its full name or a :class:`ZipInfo` object).  Its file information is
194+   extracted as accurately as possible.  *path* specifies a different directory
195+   to extract to.  *member* can be a filename or a :class:`ZipInfo` object.
196+   *pwd* is the password used for encrypted files.
197+ 
198+   .. versionadded:: 2.6
199+ 
200+ 
201+.. method:: ZipFile.extractall([path[, members[, pwd]]])
202+ 
203+   Extract all members from the archive to the current working directory.  *path*
204+   specifies a different directory to extract to.  *members* is optional and must
205+   be a subset of the list returned by :meth:`namelist`.  *pwd* is the password
206+   used for encrypted files.
207+ 
208+   .. versionadded:: 2.6
209+ 
210+ 
146.. method:: ZipFile.printdir()
147
148   Print a table of contents for the archive to ``sys.stdout``.
149
150
n216+.. method:: ZipFile.setpassword(pwd)
217+ 
218+   Set *pwd* as default password to extract encrypted files.
219+ 
220+   .. versionadded:: 2.6
221+ 
222+ 
151-.. method:: ZipFile.read(name)
223+.. method:: ZipFile.read(name[, pwd])
152
n153-   Return the bytes of the file in the archive.  The archive must be open for read
n225+   Return the bytes of the file *name* in the archive.  *name* is the name of the
154-   or append.
226+   file in the archive, or a :class:`ZipInfo` object.  The archive must be open for
227+   read or append. *pwd* is the password used for encrypted  files and, if specified,
228+   it will override the default password set with :meth:`setpassword`.  Calling
229+   :meth:`read` on a closed ZipFile  will raise a :exc:`RuntimeError`.
230+ 
231+   .. versionchanged:: 2.6
232+      *pwd* was added, and *name* can now be a :class:`ZipInfo` object.
155
156
157.. method:: ZipFile.testzip()
158
159   Read all the files in the archive and check their CRC's and file headers.
n160-   Return the name of the first bad file, or else return ``None``.
n238+   Return the name of the first bad file, or else return ``None``. Calling
239+   :meth:`testzip` on a closed ZipFile will raise a :exc:`RuntimeError`.
161
162
163.. method:: ZipFile.write(filename[, arcname[, compress_type]])
164
165   Write the file named *filename* to the archive, giving it the archive name
166   *arcname* (by default, this will be the same as *filename*, but without a drive
167   letter and with leading path separators removed).  If given, *compress_type*
168   overrides the value given for the *compression* parameter to the constructor for
n169-   the new entry.  The archive must be open with mode ``'w'`` or ``'a'``.
n248+   the new entry.  The archive must be open with mode ``'w'`` or ``'a'`` -- calling
249+   :meth:`write` on a ZipFile created with mode ``'r'`` will raise a
250+   :exc:`RuntimeError`.  Calling  :meth:`write` on a closed ZipFile will raise a
251+   :exc:`RuntimeError`.
170
171   .. note::
172
173      There is no official file name encoding for ZIP files. If you have unicode file
n174-      names, please convert them to byte strings in your desired encoding before
n256+      names, you must convert them to byte strings in your desired encoding before
175      passing them to :meth:`write`. WinZip interprets all file names as encoded in
176      CP437, also known as DOS Latin.
177
178   .. note::
179
180      Archive names should be relative to the archive root, that is, they should not
181      start with a path separator.
n264+ 
265+   .. note::
266+ 
267+      If ``arcname`` (or ``filename``, if ``arcname`` is  not given) contains a null
268+      byte, the name of the file in the archive will be truncated at the null byte.
182
183
184.. method:: ZipFile.writestr(zinfo_or_arcname, bytes)
185
186   Write the string *bytes* to the archive; *zinfo_or_arcname* is either the file
187   name it will be given in the archive, or a :class:`ZipInfo` instance.  If it's
188   an instance, at least the filename, date, and time must be given.  If it's a
189   name, the date and time is set to the current date and time. The archive must be
n190-   opened with mode ``'w'`` or ``'a'``.
n277+   opened with mode ``'w'`` or ``'a'`` -- calling  :meth:`writestr` on a ZipFile
278+   created with mode ``'r'``  will raise a :exc:`RuntimeError`.  Calling
279+   :meth:`writestr` on a closed ZipFile will raise a :exc:`RuntimeError`.
191
n281+   .. note::
282+ 
283+      When passing a :class:`ZipInfo` instance as the *zinfo_or_acrname* parameter,
284+      the compression method used will be that specified in the *compress_type*
285+      member of the given :class:`ZipInfo` instance.  By default, the
286+      :class:`ZipInfo` constructor sets this member to :const:`ZIP_STORED`.
287+ 
192-The following data attribute is also available:
288+The following data attributes are also available:
193
194
195.. attribute:: ZipFile.debug
196
197   The level of debug output to use.  This may be set from ``0`` (the default, no
198   output) to ``3`` (the most output).  Debugging information is written to
199   ``sys.stdout``.
200
n297+.. attribute:: ZipFile.comment
298+ 
299+   The comment text associated with the ZIP file.  If assigning a comment to a
300+   :class:`ZipFile` instance created with mode 'a' or 'w', this should be a
301+   string no longer than 65535 bytes.  Comments longer than this will be
302+   truncated in the written archive when :meth:`ZipFile.close` is called.
201
202.. _pyzipfile-objects:
203
204PyZipFile Objects
205-----------------
206
207The :class:`PyZipFile` constructor takes the same parameters as the
208:class:`ZipFile` constructor.  Instances have one method in addition to those of
210
211
212.. method:: PyZipFile.writepy(pathname[, basename])
213
214   Search for files :file:`\*.py` and add the corresponding file to the archive.
215   The corresponding file is a :file:`\*.pyo` file if available, else a
216   :file:`\*.pyc` file, compiling if necessary.  If the pathname is a file, the
217   filename must end with :file:`.py`, and just the (corresponding
n218-   :file:`\*.py[co]`) file is added at the top level (no path information).  If it
n320+   :file:`\*.py[co]`) file is added at the top level (no path information).  If the
219-   is a directory, and the directory is not a package directory, then all the files
321+   pathname is a file that does not end with :file:`.py`, a :exc:`RuntimeError`
220-   :file:`\*.py[co]` are added at the top level.  If the directory is a package
322+   will be raised.  If it is a directory, and the directory is not a package
221-   directory, then all :file:`\*.py[oc]` are added under the package name as a file
323+   directory, then all the files :file:`\*.py[co]` are added at the top level.  If
222-   path, and if any subdirectories are package directories, all of these are added
324+   the directory is a package directory, then all :file:`\*.py[co]` are added under
223-   recursively.  *basename* is intended for internal use only.  The :meth:`writepy`
325+   the package name as a file path, and if any subdirectories are package
224-   method makes archives with file names like this::
326+   directories, all of these are added recursively.  *basename* is intended for
327+   internal use only.  The :meth:`writepy` method makes archives with file names
328+   like this::
225
n226-      string.pyc                                # Top level name 
n330+      string.pyc                                # Top level name
227-      test/__init__.pyc                         # Package directory 
331+      test/__init__.pyc                         # Package directory
228-      test/testall.pyc                          # Module test.testall
332+      test/test_support.pyc                          # Module test.test_support
229-      test/bogus/__init__.pyc                   # Subpackage directory 
333+      test/bogus/__init__.pyc                   # Subpackage directory
230      test/bogus/myfile.pyc                     # Submodule test.bogus.myfile
231
232
233.. _zipinfo-objects:
234
235ZipInfo Objects
236---------------
237
277.. attribute:: ZipInfo.comment
278
279   Comment for the individual archive member.
280
281
282.. attribute:: ZipInfo.extra
283
284   Expansion field data.  The `PKZIP Application Note
t285-   <http://www.pkware.com/business_and_developers/developer/appnote/>`_ contains
t389+   <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_ contains
286   some comments on the internal structure of the data contained in this string.
287
288
289.. attribute:: ZipInfo.create_system
290
291   System which created ZIP archive.
292
293
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op