summaryrefslogtreecommitdiff
path: root/Doc/library/zipfile.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/zipfile.rst')
-rw-r--r--Doc/library/zipfile.rst129
1 files changed, 93 insertions, 36 deletions
diff --git a/Doc/library/zipfile.rst b/Doc/library/zipfile.rst
index d144ae36f1..5eb6f10338 100644
--- a/Doc/library/zipfile.rst
+++ b/Doc/library/zipfile.rst
@@ -147,10 +147,10 @@ ZipFile Objects
*compression* is the ZIP compression method to use when writing the archive,
and should be :const:`ZIP_STORED`, :const:`ZIP_DEFLATED`,
:const:`ZIP_BZIP2` or :const:`ZIP_LZMA`; unrecognized
- values will cause :exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED`,
+ values will cause :exc:`NotImplementedError` to be raised. If :const:`ZIP_DEFLATED`,
:const:`ZIP_BZIP2` or :const:`ZIP_LZMA` is specified but the corresponding module
(:mod:`zlib`, :mod:`bz2` or :mod:`lzma`) is not available, :exc:`RuntimeError`
- is also raised. The default is :const:`ZIP_STORED`. If *allowZip64* is
+ is raised. The default is :const:`ZIP_STORED`. If *allowZip64* is
``True`` (the default) zipfile will create ZIP files that use the ZIP64
extensions when the zipfile is larger than 4 GiB. If it is false :mod:`zipfile`
will raise an exception when the ZIP file would require ZIP64 extensions.
@@ -179,6 +179,10 @@ ZipFile Objects
Added support for writing to unseekable streams.
Added support for the ``'x'`` mode.
+ .. versionchanged:: 3.6
+ Previously, a plain :exc:`RuntimeError` was raised for unrecognized
+ compression values.
+
.. method:: ZipFile.close()
@@ -205,18 +209,12 @@ ZipFile Objects
Return a list of archive members by name.
-.. index::
- single: universal newlines; zipfile.ZipFile.open method
-
-.. method:: ZipFile.open(name, mode='r', pwd=None)
+.. method:: ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False)
- Extract a member from the archive as a file-like object (ZipExtFile). *name*
- is the name of the file in the archive, or a :class:`ZipInfo` object. The
- *mode* parameter, if included, must be one of the following: ``'r'`` (the
- default), ``'U'``, or ``'rU'``. Choosing ``'U'`` or ``'rU'`` will enable
- :term:`universal newlines` support in the read-only object. *pwd* is the
- password used for encrypted files. Calling :meth:`.open` on a closed
- ZipFile will raise a :exc:`RuntimeError`.
+ Access a member of the archive as a binary file-like object. *name*
+ can be either the name of a file within the archive or a :class:`ZipInfo`
+ object. The *mode* parameter, if included, must be ``'r'`` (the default)
+ or ``'w'``. *pwd* is the password used to decrypt encrypted ZIP files.
:meth:`~ZipFile.open` is also a context manager and therefore supports the
:keyword:`with` statement::
@@ -225,17 +223,23 @@ ZipFile Objects
with myzip.open('eggs.txt') as myfile:
print(myfile.read())
- .. note::
+ With *mode* ``'r'`` the file-like object
+ (``ZipExtFile``) is read-only and provides the following methods:
+ :meth:`~io.BufferedIOBase.read`, :meth:`~io.IOBase.readline`,
+ :meth:`~io.IOBase.readlines`, :meth:`__iter__`,
+ :meth:`~iterator.__next__`. These objects can operate independently of
+ the ZipFile.
- The file-like object is read-only and provides the following methods:
- :meth:`~io.BufferedIOBase.read`, :meth:`~io.IOBase.readline`,
- :meth:`~io.IOBase.readlines`, :meth:`__iter__`,
- :meth:`~iterator.__next__`.
+ With ``mode='w'``, a writable file handle is returned, which supports the
+ :meth:`~io.BufferedIOBase.write` method. While a writable file handle is open,
+ attempting to read or write other files in the ZIP file will raise a
+ :exc:`ValueError`.
- .. note::
-
- Objects returned by :meth:`.open` can operate independently of the
- ZipFile.
+ When writing a file, if the file size is not known in advance but may exceed
+ 2 GiB, pass ``force_zip64=True`` to ensure that the header format is
+ capable of supporting large files. If the file size is known in advance,
+ construct a :class:`ZipInfo` object with :attr:`~ZipInfo.file_size` set, and
+ use that as the *name* parameter.
.. note::
@@ -243,10 +247,19 @@ ZipFile Objects
or a :class:`ZipInfo` object. You will appreciate this when trying to read a
ZIP file that contains members with duplicate names.
- .. deprecated-removed:: 3.4 3.6
- The ``'U'`` or ``'rU'`` mode. Use :class:`io.TextIOWrapper` for reading
+ .. versionchanged:: 3.6
+ Removed support of ``mode='U'``. Use :class:`io.TextIOWrapper` for reading
compressed text files in :term:`universal newlines` mode.
+ .. versionchanged:: 3.6
+ :meth:`open` can now be used to write files into the archive with the
+ ``mode='w'`` option.
+
+ .. versionchanged:: 3.6
+ Calling :meth:`.open` on a closed ZipFile will raise a :exc:`ValueError`.
+ Previously, a :exc:`RuntimeError` was raised.
+
+
.. method:: ZipFile.extract(member, path=None, pwd=None)
Extract a member from the archive to the current working directory; *member*
@@ -267,6 +280,10 @@ ZipFile Objects
characters (``:``, ``<``, ``>``, ``|``, ``"``, ``?``, and ``*``)
replaced by underscore (``_``).
+ .. versionchanged:: 3.6
+ Calling :meth:`extract` on a closed ZipFile will raise a
+ :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised.
+
.. method:: ZipFile.extractall(path=None, members=None, pwd=None)
@@ -283,6 +300,10 @@ ZipFile Objects
dots ``".."``. This module attempts to prevent that.
See :meth:`extract` note.
+ .. versionchanged:: 3.6
+ Calling :meth:`extractall` on a closed ZipFile will raise a
+ :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised.
+
.. method:: ZipFile.printdir()
@@ -300,18 +321,24 @@ ZipFile Objects
file in the archive, or a :class:`ZipInfo` object. The archive must be open for
read or append. *pwd* is the password used for encrypted files and, if specified,
it will override the default password set with :meth:`setpassword`. Calling
- :meth:`read` on a closed ZipFile will raise a :exc:`RuntimeError`. Calling
:meth:`read` on a ZipFile that uses a compression method other than
:const:`ZIP_STORED`, :const:`ZIP_DEFLATED`, :const:`ZIP_BZIP2` or
:const:`ZIP_LZMA` will raise a :exc:`NotImplementedError`. An error will also
be raised if the corresponding compression module is not available.
+ .. versionchanged:: 3.6
+ Calling :meth:`read` on a closed ZipFile will raise a :exc:`ValueError`.
+ Previously, a :exc:`RuntimeError` was raised.
+
.. method:: ZipFile.testzip()
Read all the files in the archive and check their CRC's and file headers.
- Return the name of the first bad file, or else return ``None``. Calling
- :meth:`testzip` on a closed ZipFile will raise a :exc:`RuntimeError`.
+ Return the name of the first bad file, or else return ``None``.
+
+ .. versionchanged:: 3.6
+ Calling :meth:`testfile` on a closed ZipFile will raise a
+ :exc:`ValueError`. Previously, a :exc:`RuntimeError` was raised.
.. method:: ZipFile.write(filename, arcname=None, compress_type=None)
@@ -321,10 +348,7 @@ ZipFile Objects
letter and with leading path separators removed). If given, *compress_type*
overrides the value given for the *compression* parameter to the constructor for
the new entry.
- The archive must be open with mode ``'w'``, ``'x'`` or ``'a'`` -- calling
- :meth:`write` on a ZipFile created with mode ``'r'`` will raise a
- :exc:`RuntimeError`. Calling :meth:`write` on a closed ZipFile will raise a
- :exc:`RuntimeError`.
+ The archive must be open with mode ``'w'``, ``'x'`` or ``'a'``.
.. note::
@@ -343,16 +367,19 @@ ZipFile Objects
If ``arcname`` (or ``filename``, if ``arcname`` is not given) contains a null
byte, the name of the file in the archive will be truncated at the null byte.
+ .. versionchanged:: 3.6
+ Calling :meth:`write` on a ZipFile created with mode ``'r'`` or
+ a closed ZipFile will raise a :exc:`ValueError`. Previously,
+ a :exc:`RuntimeError` was raised.
+
+
.. method:: ZipFile.writestr(zinfo_or_arcname, data[, compress_type])
Write the string *data* to the archive; *zinfo_or_arcname* is either the file
name it will be given in the archive, or a :class:`ZipInfo` instance. If it's
an instance, at least the filename, date, and time must be given. If it's a
name, the date and time is set to the current date and time.
- The archive must be opened with mode ``'w'``, ``'x'`` or ``'a'`` -- calling
- :meth:`writestr` on a ZipFile created with mode ``'r'`` will raise a
- :exc:`RuntimeError`. Calling :meth:`writestr` on a closed ZipFile will
- raise a :exc:`RuntimeError`.
+ The archive must be opened with mode ``'w'``, ``'x'`` or ``'a'``.
If given, *compress_type* overrides the value given for the *compression*
parameter to the constructor for the new entry, or in the *zinfo_or_arcname*
@@ -368,6 +395,12 @@ ZipFile Objects
.. versionchanged:: 3.2
The *compress_type* argument.
+ .. versionchanged:: 3.6
+ Calling :meth:`writestr` on a ZipFile created with mode ``'r'`` or
+ a closed ZipFile will raise a :exc:`ValueError`. Previously,
+ a :exc:`RuntimeError` was raised.
+
+
The following data attributes are also available:
@@ -465,7 +498,31 @@ Instances of the :class:`ZipInfo` class are returned by the :meth:`.getinfo` and
:meth:`.infolist` methods of :class:`ZipFile` objects. Each object stores
information about a single member of the ZIP archive.
-Instances have the following attributes:
+There is one classmethod to make a :class:`ZipInfo` instance for a filesystem
+file:
+
+.. classmethod:: ZipInfo.from_file(filename, arcname=None)
+
+ Construct a :class:`ZipInfo` instance for a file on the filesystem, in
+ preparation for adding it to a zip file.
+
+ *filename* should be the path to a file or directory on the filesystem.
+
+ If *arcname* is specified, it is used as the name within the archive.
+ If *arcname* is not specified, the name will be the same as *filename*, but
+ with any drive letter and leading path separators removed.
+
+ .. versionadded:: 3.6
+
+Instances have the following methods and attributes:
+
+.. method:: ZipInfo.is_dir()
+
+ Return ``True`` if this archive member is a directory.
+
+ This uses the entry's name: directories should always end with ``/``.
+
+ .. versionadded:: 3.6
.. attribute:: ZipInfo.filename