Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`gzip` --- Support for :program:`gzip` files |
| 2 | ================================================= |
| 3 | |
| 4 | .. module:: gzip |
| 5 | :synopsis: Interfaces for gzip compression and decompression using file objects. |
| 6 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 7 | **Source code:** :source:`Lib/gzip.py` |
| 8 | |
| 9 | -------------- |
| 10 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 11 | This module provides a simple interface to compress and decompress files just |
| 12 | like the GNU programs :program:`gzip` and :program:`gunzip` would. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 14 | The data compression is provided by the :mod:`zlib` module. |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 15 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 16 | The :mod:`gzip` module provides the :class:`GzipFile` class, as well as the |
Nadeem Vawda | 6872101 | 2012-06-04 23:21:38 +0200 | [diff] [blame] | 17 | :func:`.open`, :func:`compress` and :func:`decompress` convenience functions. |
| 18 | The :class:`GzipFile` class reads and writes :program:`gzip`\ -format files, |
| 19 | automatically compressing or decompressing the data so that it looks like an |
| 20 | ordinary :term:`file object`. |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 21 | |
| 22 | Note that additional file formats which can be decompressed by the |
| 23 | :program:`gzip` and :program:`gunzip` programs, such as those produced by |
| 24 | :program:`compress` and :program:`pack`, are not supported by this module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | The module defines the following items: |
| 27 | |
| 28 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 29 | .. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None) |
| 30 | |
Nadeem Vawda | 6872101 | 2012-06-04 23:21:38 +0200 | [diff] [blame] | 31 | Open a gzip-compressed file in binary or text mode, returning a :term:`file |
| 32 | object`. |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 33 | |
Nadeem Vawda | 6872101 | 2012-06-04 23:21:38 +0200 | [diff] [blame] | 34 | The *filename* argument can be an actual filename (a :class:`str` or |
| 35 | :class:`bytes` object), or an existing file object to read from or write to. |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 36 | |
| 37 | The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``, |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 38 | ``'w'``, ``'wb'``, ``'x'`` or ``'xb'`` for binary mode, or ``'rt'``, |
| 39 | ``'at'``, ``'wt'``, or ``'xt'`` for text mode. The default is ``'rb'``. |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 40 | |
Nadeem Vawda | 6ff262e | 2012-11-11 14:14:47 +0100 | [diff] [blame] | 41 | The *compresslevel* argument is an integer from 0 to 9, as for the |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 42 | :class:`GzipFile` constructor. |
| 43 | |
| 44 | For binary mode, this function is equivalent to the :class:`GzipFile` |
| 45 | constructor: ``GzipFile(filename, mode, compresslevel)``. In this case, the |
| 46 | *encoding*, *errors* and *newline* arguments must not be provided. |
| 47 | |
| 48 | For text mode, a :class:`GzipFile` object is created, and wrapped in an |
| 49 | :class:`io.TextIOWrapper` instance with the specified encoding, error |
| 50 | handling behavior, and line ending(s). |
| 51 | |
| 52 | .. versionchanged:: 3.3 |
Nadeem Vawda | 6872101 | 2012-06-04 23:21:38 +0200 | [diff] [blame] | 53 | Added support for *filename* being a file object, support for text mode, |
| 54 | and the *encoding*, *errors* and *newline* arguments. |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 55 | |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 56 | .. versionchanged:: 3.4 |
| 57 | Added support for the ``'x'``, ``'xb'`` and ``'xt'`` modes. |
| 58 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 59 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 60 | .. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
Antoine Pitrou | c3ed2e7 | 2010-09-29 10:49:46 +0000 | [diff] [blame] | 62 | Constructor for the :class:`GzipFile` class, which simulates most of the |
| 63 | methods of a :term:`file object`, with the exception of the :meth:`truncate` |
| 64 | method. At least one of *fileobj* and *filename* must be given a non-trivial |
| 65 | value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
| 67 | The new class instance is based on *fileobj*, which can be a regular file, a |
Serhiy Storchaka | e79be87 | 2013-08-17 00:09:55 +0300 | [diff] [blame] | 68 | :class:`io.BytesIO` object, or any other object which simulates a file. It |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | defaults to ``None``, in which case *filename* is opened to provide a file |
| 70 | object. |
| 71 | |
| 72 | When *fileobj* is not ``None``, the *filename* argument is only used to be |
Georg Brandl | f27bfd8 | 2013-10-06 12:33:20 +0200 | [diff] [blame] | 73 | included in the :program:`gzip` file header, which may include the original |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | filename of the uncompressed file. It defaults to the filename of *fileobj*, if |
| 75 | discernible; otherwise, it defaults to the empty string, and in this case the |
| 76 | original filename is not included in the header. |
| 77 | |
| 78 | The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``, ``'w'``, |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 79 | ``'wb'``, ``'x'``, or ``'xb'``, depending on whether the file will be read or |
| 80 | written. The default is the mode of *fileobj* if discernible; otherwise, the |
| 81 | default is ``'rb'``. |
Nadeem Vawda | 30d94b7 | 2012-02-11 23:45:10 +0200 | [diff] [blame] | 82 | |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 83 | Note that the file is always opened in binary mode. To open a compressed file |
Nadeem Vawda | 6872101 | 2012-06-04 23:21:38 +0200 | [diff] [blame] | 84 | in text mode, use :func:`.open` (or wrap your :class:`GzipFile` with an |
Nadeem Vawda | 7e12620 | 2012-05-06 15:04:01 +0200 | [diff] [blame] | 85 | :class:`io.TextIOWrapper`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
Nadeem Vawda | 19e568d | 2012-11-11 14:04:14 +0100 | [diff] [blame] | 87 | The *compresslevel* argument is an integer from ``0`` to ``9`` controlling |
| 88 | the level of compression; ``1`` is fastest and produces the least |
| 89 | compression, and ``9`` is slowest and produces the most compression. ``0`` |
| 90 | is no compression. The default is ``9``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 92 | The *mtime* argument is an optional numeric timestamp to be written to |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 93 | the last modification time field in the stream when compressing. It |
| 94 | should only be provided in compression mode. If omitted or ``None``, the |
| 95 | current time is used. See the :attr:`mtime` attribute for more details. |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 96 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | Calling a :class:`GzipFile` object's :meth:`close` method does not close |
| 98 | *fileobj*, since you might wish to append more material after the compressed |
Antoine Pitrou | e5768cf | 2010-09-23 16:45:17 +0000 | [diff] [blame] | 99 | data. This also allows you to pass a :class:`io.BytesIO` object opened for |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | writing as *fileobj*, and retrieve the resulting memory buffer using the |
Antoine Pitrou | e5768cf | 2010-09-23 16:45:17 +0000 | [diff] [blame] | 101 | :class:`io.BytesIO` object's :meth:`~io.BytesIO.getvalue` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
Antoine Pitrou | c3ed2e7 | 2010-09-29 10:49:46 +0000 | [diff] [blame] | 103 | :class:`GzipFile` supports the :class:`io.BufferedIOBase` interface, |
| 104 | including iteration and the :keyword:`with` statement. Only the |
| 105 | :meth:`truncate` method isn't implemented. |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 106 | |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 107 | :class:`GzipFile` also provides the following method and attribute: |
Antoine Pitrou | 7b998e9 | 2010-10-04 21:55:14 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 109 | .. method:: peek(n) |
Antoine Pitrou | 7b998e9 | 2010-10-04 21:55:14 +0000 | [diff] [blame] | 110 | |
| 111 | Read *n* uncompressed bytes without advancing the file position. |
| 112 | At most one single read on the compressed stream is done to satisfy |
| 113 | the call. The number of bytes returned may be more or less than |
| 114 | requested. |
| 115 | |
Nadeem Vawda | 6976104 | 2013-12-08 19:47:22 +0100 | [diff] [blame] | 116 | .. note:: While calling :meth:`peek` does not change the file position of |
| 117 | the :class:`GzipFile`, it may change the position of the underlying |
| 118 | file object (e.g. if the :class:`GzipFile` was constructed with the |
| 119 | *fileobj* parameter). |
| 120 | |
Antoine Pitrou | 7b998e9 | 2010-10-04 21:55:14 +0000 | [diff] [blame] | 121 | .. versionadded:: 3.2 |
| 122 | |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 123 | .. attribute:: mtime |
| 124 | |
| 125 | When decompressing, the value of the last modification time field in |
| 126 | the most recently read header may be read from this attribute, as an |
| 127 | integer. The initial value before reading any headers is ``None``. |
| 128 | |
| 129 | All :program:`gzip` compressed streams are required to contain this |
| 130 | timestamp field. Some programs, such as :program:`gunzip`\ , make use |
| 131 | of the timestamp. The format is the same as the return value of |
| 132 | :func:`time.time` and the :attr:`~os.stat_result.st_mtime` attribute of |
| 133 | the object returned by :func:`os.stat`. |
| 134 | |
Benjamin Peterson | 10745a9 | 2009-03-09 21:08:47 +0000 | [diff] [blame] | 135 | .. versionchanged:: 3.1 |
Georg Brandl | ffb94ae | 2013-10-06 19:02:08 +0200 | [diff] [blame] | 136 | Support for the :keyword:`with` statement was added, along with the |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 137 | *mtime* constructor argument and :attr:`mtime` attribute. |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | 8e33fd7 | 2010-01-13 14:37:26 +0000 | [diff] [blame] | 139 | .. versionchanged:: 3.2 |
Georg Brandl | ffb94ae | 2013-10-06 19:02:08 +0200 | [diff] [blame] | 140 | Support for zero-padded and unseekable files was added. |
Antoine Pitrou | 7b96984 | 2010-09-23 16:22:51 +0000 | [diff] [blame] | 141 | |
Antoine Pitrou | 6b4be36 | 2011-04-04 21:09:05 +0200 | [diff] [blame] | 142 | .. versionchanged:: 3.3 |
| 143 | The :meth:`io.BufferedIOBase.read1` method is now implemented. |
| 144 | |
Nadeem Vawda | ee1be99 | 2013-10-19 00:11:13 +0200 | [diff] [blame] | 145 | .. versionchanged:: 3.4 |
| 146 | Added support for the ``'x'`` and ``'xb'`` modes. |
| 147 | |
Serhiy Storchaka | bca63b3 | 2015-03-23 14:59:48 +0200 | [diff] [blame] | 148 | .. versionchanged:: 3.5 |
| 149 | Added support for writing arbitrary |
| 150 | :term:`bytes-like objects <bytes-like object>`. |
Antoine Pitrou | 2dbc6e6 | 2015-04-11 00:31:01 +0200 | [diff] [blame] | 151 | The :meth:`~io.BufferedIOBase.read` method now accepts an argument of |
| 152 | ``None``. |
Serhiy Storchaka | bca63b3 | 2015-03-23 14:59:48 +0200 | [diff] [blame] | 153 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | 79c5ef1 | 2010-08-17 21:10:05 +0000 | [diff] [blame] | 155 | .. function:: compress(data, compresslevel=9) |
| 156 | |
| 157 | Compress the *data*, returning a :class:`bytes` object containing |
| 158 | the compressed data. *compresslevel* has the same meaning as in |
| 159 | the :class:`GzipFile` constructor above. |
| 160 | |
Antoine Pitrou | cdfe1c5 | 2010-08-17 21:15:00 +0000 | [diff] [blame] | 161 | .. versionadded:: 3.2 |
| 162 | |
Antoine Pitrou | 79c5ef1 | 2010-08-17 21:10:05 +0000 | [diff] [blame] | 163 | .. function:: decompress(data) |
| 164 | |
| 165 | Decompress the *data*, returning a :class:`bytes` object containing the |
| 166 | uncompressed data. |
| 167 | |
Antoine Pitrou | cdfe1c5 | 2010-08-17 21:15:00 +0000 | [diff] [blame] | 168 | .. versionadded:: 3.2 |
| 169 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 171 | .. _gzip-usage-examples: |
| 172 | |
| 173 | Examples of usage |
| 174 | ----------------- |
| 175 | |
| 176 | Example of how to read a compressed file:: |
| 177 | |
| 178 | import gzip |
Antoine Pitrou | bf1a018 | 2010-08-17 21:11:49 +0000 | [diff] [blame] | 179 | with gzip.open('/home/joe/file.txt.gz', 'rb') as f: |
| 180 | file_content = f.read() |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 181 | |
| 182 | Example of how to create a compressed GZIP file:: |
| 183 | |
| 184 | import gzip |
Antoine Pitrou | bf1a018 | 2010-08-17 21:11:49 +0000 | [diff] [blame] | 185 | content = b"Lots of content here" |
| 186 | with gzip.open('/home/joe/file.txt.gz', 'wb') as f: |
| 187 | f.write(content) |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 188 | |
| 189 | Example of how to GZIP compress an existing file:: |
| 190 | |
| 191 | import gzip |
Andrew Kuchling | f887a61 | 2015-04-14 11:44:40 -0400 | [diff] [blame] | 192 | import shutil |
Antoine Pitrou | bf1a018 | 2010-08-17 21:11:49 +0000 | [diff] [blame] | 193 | with open('/home/joe/file.txt', 'rb') as f_in: |
Éric Araujo | f5be090 | 2010-08-17 21:24:05 +0000 | [diff] [blame] | 194 | with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out: |
Andrew Kuchling | f887a61 | 2015-04-14 11:44:40 -0400 | [diff] [blame] | 195 | shutil.copyfileobj(f_in, f_out) |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | 79c5ef1 | 2010-08-17 21:10:05 +0000 | [diff] [blame] | 197 | Example of how to GZIP compress a binary string:: |
| 198 | |
| 199 | import gzip |
| 200 | s_in = b"Lots of content here" |
| 201 | s_out = gzip.compress(s_in) |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 202 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | .. seealso:: |
| 204 | |
| 205 | Module :mod:`zlib` |
| 206 | The basic data compression module needed to support the :program:`gzip` file |
| 207 | format. |
| 208 | |