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 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 7 | This module provides a simple interface to compress and decompress files just |
| 8 | like the GNU programs :program:`gzip` and :program:`gunzip` would. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 10 | The data compression is provided by the :mod:`zlib` module. |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 11 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 12 | The :mod:`gzip` module provides the :class:`GzipFile` class. The :class:`GzipFile` |
| 13 | class reads and writes :program:`gzip`\ -format files, automatically compressing |
| 14 | or decompressing the data so that it looks like an ordinary :term:`file object`. |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 15 | |
| 16 | Note that additional file formats which can be decompressed by the |
| 17 | :program:`gzip` and :program:`gunzip` programs, such as those produced by |
| 18 | :program:`compress` and :program:`pack`, are not supported by this module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 7767711 | 2007-11-05 19:43:04 +0000 | [diff] [blame] | 20 | For other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and |
| 21 | :mod:`tarfile` modules. |
| 22 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | The module defines the following items: |
| 24 | |
| 25 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 26 | .. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
| 28 | Constructor for the :class:`GzipFile` class, which simulates most of the methods |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 29 | of a :term:`file object`, with the exception of the :meth:`readinto` and |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | :meth:`truncate` methods. At least one of *fileobj* and *filename* must be |
| 31 | given a non-trivial value. |
| 32 | |
| 33 | The new class instance is based on *fileobj*, which can be a regular file, a |
| 34 | :class:`StringIO` object, or any other object which simulates a file. It |
| 35 | defaults to ``None``, in which case *filename* is opened to provide a file |
| 36 | object. |
| 37 | |
| 38 | When *fileobj* is not ``None``, the *filename* argument is only used to be |
| 39 | included in the :program:`gzip` file header, which may includes the original |
| 40 | filename of the uncompressed file. It defaults to the filename of *fileobj*, if |
| 41 | discernible; otherwise, it defaults to the empty string, and in this case the |
| 42 | original filename is not included in the header. |
| 43 | |
| 44 | The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``, ``'w'``, |
| 45 | or ``'wb'``, depending on whether the file will be read or written. The default |
| 46 | is the mode of *fileobj* if discernible; otherwise, the default is ``'rb'``. If |
| 47 | not given, the 'b' flag will be added to the mode to ensure the file is opened |
| 48 | in binary mode for cross-platform portability. |
| 49 | |
| 50 | The *compresslevel* argument is an integer from ``1`` to ``9`` controlling the |
| 51 | level of compression; ``1`` is fastest and produces the least compression, and |
| 52 | ``9`` is slowest and produces the most compression. The default is ``9``. |
| 53 | |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 54 | The *mtime* argument is an optional numeric timestamp to be written to |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 55 | the stream when compressing. All :program:`gzip` compressed streams are |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 56 | required to contain a timestamp. If omitted or ``None``, the current |
| 57 | time is used. This module ignores the timestamp when decompressing; |
| 58 | however, some programs, such as :program:`gunzip`\ , make use of it. |
| 59 | The format of the timestamp is the same as that of the return value of |
| 60 | ``time.time()`` and of the ``st_mtime`` member of the object returned |
| 61 | by ``os.stat()``. |
| 62 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | Calling a :class:`GzipFile` object's :meth:`close` method does not close |
| 64 | *fileobj*, since you might wish to append more material after the compressed |
Antoine Pitrou | e5768cf | 2010-09-23 16:45:17 +0000 | [diff] [blame^] | 65 | 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] | 66 | writing as *fileobj*, and retrieve the resulting memory buffer using the |
Antoine Pitrou | e5768cf | 2010-09-23 16:45:17 +0000 | [diff] [blame^] | 67 | :class:`io.BytesIO` object's :meth:`~io.BytesIO.getvalue` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
Antoine Pitrou | e5768cf | 2010-09-23 16:45:17 +0000 | [diff] [blame^] | 69 | :class:`GzipFile` supports the whole :class:`io.BufferedIOBase` interface, |
| 70 | including iteration and the :keyword:`with` statement. |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 71 | |
Benjamin Peterson | 10745a9 | 2009-03-09 21:08:47 +0000 | [diff] [blame] | 72 | .. versionchanged:: 3.1 |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 73 | Support for the :keyword:`with` statement was added. |
| 74 | |
Antoine Pitrou | 8e33fd7 | 2010-01-13 14:37:26 +0000 | [diff] [blame] | 75 | .. versionchanged:: 3.2 |
| 76 | Support for zero-padded files was added. |
| 77 | |
Antoine Pitrou | 7b96984 | 2010-09-23 16:22:51 +0000 | [diff] [blame] | 78 | .. versionchanged:: 3.2 |
| 79 | Support for unseekable files was added. |
| 80 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 82 | .. function:: open(filename, mode='rb', compresslevel=9) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
| 84 | This is a shorthand for ``GzipFile(filename,`` ``mode,`` ``compresslevel)``. |
| 85 | The *filename* argument is required; *mode* defaults to ``'rb'`` and |
| 86 | *compresslevel* defaults to ``9``. |
| 87 | |
Antoine Pitrou | 79c5ef1 | 2010-08-17 21:10:05 +0000 | [diff] [blame] | 88 | .. function:: compress(data, compresslevel=9) |
| 89 | |
| 90 | Compress the *data*, returning a :class:`bytes` object containing |
| 91 | the compressed data. *compresslevel* has the same meaning as in |
| 92 | the :class:`GzipFile` constructor above. |
| 93 | |
Antoine Pitrou | cdfe1c5 | 2010-08-17 21:15:00 +0000 | [diff] [blame] | 94 | .. versionadded:: 3.2 |
| 95 | |
Antoine Pitrou | 79c5ef1 | 2010-08-17 21:10:05 +0000 | [diff] [blame] | 96 | .. function:: decompress(data) |
| 97 | |
| 98 | Decompress the *data*, returning a :class:`bytes` object containing the |
| 99 | uncompressed data. |
| 100 | |
Antoine Pitrou | cdfe1c5 | 2010-08-17 21:15:00 +0000 | [diff] [blame] | 101 | .. versionadded:: 3.2 |
| 102 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 103 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 104 | .. _gzip-usage-examples: |
| 105 | |
| 106 | Examples of usage |
| 107 | ----------------- |
| 108 | |
| 109 | Example of how to read a compressed file:: |
| 110 | |
| 111 | import gzip |
Antoine Pitrou | bf1a018 | 2010-08-17 21:11:49 +0000 | [diff] [blame] | 112 | with gzip.open('/home/joe/file.txt.gz', 'rb') as f: |
| 113 | file_content = f.read() |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 114 | |
| 115 | Example of how to create a compressed GZIP file:: |
| 116 | |
| 117 | import gzip |
Antoine Pitrou | bf1a018 | 2010-08-17 21:11:49 +0000 | [diff] [blame] | 118 | content = b"Lots of content here" |
| 119 | with gzip.open('/home/joe/file.txt.gz', 'wb') as f: |
| 120 | f.write(content) |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 121 | |
| 122 | Example of how to GZIP compress an existing file:: |
| 123 | |
| 124 | import gzip |
Antoine Pitrou | bf1a018 | 2010-08-17 21:11:49 +0000 | [diff] [blame] | 125 | with open('/home/joe/file.txt', 'rb') as f_in: |
Éric Araujo | f5be090 | 2010-08-17 21:24:05 +0000 | [diff] [blame] | 126 | with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out: |
Antoine Pitrou | bf1a018 | 2010-08-17 21:11:49 +0000 | [diff] [blame] | 127 | f_out.writelines(f_in) |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 128 | |
Antoine Pitrou | 79c5ef1 | 2010-08-17 21:10:05 +0000 | [diff] [blame] | 129 | Example of how to GZIP compress a binary string:: |
| 130 | |
| 131 | import gzip |
| 132 | s_in = b"Lots of content here" |
| 133 | s_out = gzip.compress(s_in) |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 134 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | .. seealso:: |
| 136 | |
| 137 | Module :mod:`zlib` |
| 138 | The basic data compression module needed to support the :program:`gzip` file |
| 139 | format. |
| 140 | |