Changeset 391 for python/trunk/Doc/library/zipfile.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/zipfile.rst
r2 r391 1 2 1 :mod:`zipfile` --- Work with ZIP archives 3 2 ========================================= … … 9 8 10 9 .. versionadded:: 1.6 10 11 **Source code:** :source:`Lib/zipfile.py` 12 13 -------------- 11 14 12 15 The ZIP file format is a common archive and compression standard. This module … … 16 19 <http://www.pkware.com/documents/casestudies/APPNOTE.TXT>`_. 17 20 18 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` 21 documentation). It can handle ZIP files that use the ZIP64 extensions 21 This module does not currently handle multi-disk ZIP files. 22 It can handle ZIP files that use the ZIP64 extensions 22 23 (that is ZIP files that are more than 4 GByte in size). It supports 23 24 decryption of encrypted files in ZIP archives, but it currently cannot 24 25 create an encrypted file. Decryption is extremely slow as it is 25 implemented in native python rather than C. 26 27 For other archive formats, see the :mod:`bz2`, :mod:`gzip`, and 28 :mod:`tarfile` modules. 26 implemented in native Python rather than C. 29 27 30 28 The module defines the following items: … … 42 40 43 41 .. class:: ZipFile 42 :noindex: 44 43 45 44 The class for reading and writing ZIP files. See section … … 55 54 56 55 Class used to represent information about a member of an archive. Instances 57 of this class are returned by the :meth:` getinfo` and :meth:`infolist`56 of this class are returned by the :meth:`.getinfo` and :meth:`.infolist` 58 57 methods of :class:`ZipFile` objects. Most users of the :mod:`zipfile` module 59 58 will not need to create these, but only use those created by this … … 67 66 68 67 Returns ``True`` if *filename* is a valid ZIP file based on its magic number, 69 otherwise returns ``False``. This module does not currently handle ZIP files 70 which have appended comments. 71 68 otherwise returns ``False``. *filename* may be a file or file-like object too. 69 70 .. versionchanged:: 2.7 71 Support for file and file-like objects. 72 72 73 73 .. data:: ZIP_STORED … … 79 79 80 80 The numeric constant for the usual ZIP compression method. This requires the 81 zlibmodule. No other compression methods are currently supported.81 :mod:`zlib` module. No other compression methods are currently supported. 82 82 83 83 … … 104 104 file-like object. The *mode* parameter should be ``'r'`` to read an existing 105 105 file, ``'w'`` to truncate and write a new file, or ``'a'`` to append to an 106 existing file. If *mode* is ``'a'`` and *file* refers to an existing ZIP file, 107 then additional files are added to it. If *file* does not refer to a ZIP file, 108 then a new ZIP archive is appended to the file. This is meant for adding a ZIP 109 archive to another file, such as :file:`python.exe`. Using :: 110 111 cat myzip.zip >> python.exe 112 113 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 115 ZIP compression method to use when writing the archive, and should be 116 :const:`ZIP_STORED` or :const:`ZIP_DEFLATED`; unrecognized values will cause 117 :exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED` is specified but the 118 :mod:`zlib` module is not available, :exc:`RuntimeError` is also raised. The 119 default is :const:`ZIP_STORED`. If *allowZip64* is ``True`` zipfile will create 120 ZIP files that use the ZIP64 extensions when the zipfile is larger than 2 GB. If 121 it is false (the default) :mod:`zipfile` will raise an exception when the ZIP 122 file would require ZIP64 extensions. ZIP64 extensions are disabled by default 123 because the default :program:`zip` and :program:`unzip` commands on Unix (the 124 InfoZIP utilities) don't support these extensions. 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`). 125 110 126 111 .. versionchanged:: 2.6 127 If the file does not exist, it is created if the mode is 'a'. 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 126 .. versionchanged:: 2.7.1 127 If the file is created with mode ``'a'`` or ``'w'`` and then 128 :meth:`closed <close>` without adding any files to the archive, the appropriate 129 ZIP structures for an empty archive will be written to the file. 130 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. 128 140 129 141 … … 152 164 Return a list of archive members by name. 153 165 166 .. index:: 167 single: universal newlines; zipfile.ZipFile.open method 168 154 169 155 170 .. method:: ZipFile.open(name[, mode[, pwd]]) … … 157 172 Extract a member from the archive as a file-like object (ZipExtFile). *name* is 158 173 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 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>` 161 177 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`.178 Calling :meth:`.open` on a closed ZipFile will raise a :exc:`RuntimeError`. 163 179 164 180 .. note:: 165 181 166 182 The file-like object is read-only and provides the following methods: 167 :meth:`read`, :meth:`readline`, :meth:`readlines`, :meth:`__iter__`, 168 :meth:`next`. 183 :meth:`~file.read`, :meth:`~file.readline`, 184 :meth:`~file.readlines`, :meth:`__iter__`, 185 :meth:`~object.next`. 169 186 170 187 .. note:: … … 181 198 .. note:: 182 199 183 The :meth:` open`, :meth:`read` and :meth:`extract` methods can take a filename200 The :meth:`.open`, :meth:`read` and :meth:`extract` methods can take a filename 184 201 or a :class:`ZipInfo` object. You will appreciate this when trying to read a 185 202 ZIP file that contains members with duplicate names. … … 198 215 .. versionadded:: 2.6 199 216 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 200 227 201 228 .. method:: ZipFile.extractall([path[, members[, pwd]]]) … … 212 239 that have absolute filenames starting with ``"/"`` or filenames with two 213 240 dots ``".."``. 241 242 .. versionchanged:: 2.7.4 243 The zipfile module attempts to prevent that. See :meth:`extract` note. 214 244 215 245 .. versionadded:: 2.6 … … 276 306 277 307 278 .. method:: ZipFile.writestr(zinfo_or_arcname, bytes )308 .. method:: ZipFile.writestr(zinfo_or_arcname, bytes[, compress_type]) 279 309 280 310 Write the string *bytes* to the archive; *zinfo_or_arcname* is either the file … … 286 316 :meth:`writestr` on a closed ZipFile will raise a :exc:`RuntimeError`. 287 317 288 .. note:: 289 290 When passing a :class:`ZipInfo` instance as the *zinfo_or_acrname* parameter, 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 322 .. note:: 323 324 When passing a :class:`ZipInfo` instance as the *zinfo_or_arcname* parameter, 291 325 the compression method used will be that specified in the *compress_type* 292 326 member of the given :class:`ZipInfo` instance. By default, the 293 327 :class:`ZipInfo` constructor sets this member to :const:`ZIP_STORED`. 294 328 329 .. versionchanged:: 2.7 330 The *compress_type* argument. 331 295 332 The following data attributes are also available: 296 333 … … 307 344 :class:`ZipFile` instance created with mode 'a' or 'w', this should be a 308 345 string no longer than 65535 bytes. Comments longer than this will be 309 truncated in the written archive when :meth:` ZipFile.close` is called.346 truncated in the written archive when :meth:`.close` is called. 310 347 311 348 .. _pyzipfile-objects: … … 347 384 --------------- 348 385 349 Instances of the :class:`ZipInfo` class are returned by the :meth:` getinfo` and350 :meth:` infolist` methods of :class:`ZipFile` objects. Each object stores386 Instances of the :class:`ZipInfo` class are returned by the :meth:`.getinfo` and 387 :meth:`.infolist` methods of :class:`ZipFile` objects. Each object stores 351 388 information about a single member of the ZIP archive. 352 389 … … 367 404 | Index | Value | 368 405 +=======+==========================+ 369 | ``0`` | Year 406 | ``0`` | Year (>= 1980) | 370 407 +-------+--------------------------+ 371 408 | ``1`` | Month (one-based) | … … 379 416 | ``5`` | Seconds (zero-based) | 380 417 +-------+--------------------------+ 418 419 .. note:: 420 421 The ZIP file format does not support timestamps before 1980. 381 422 382 423
Note:
See TracChangeset
for help on using the changeset viewer.