Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 7 | **Source code:** :source:`Lib/gzip.py` |
| 8 | |
| 9 | -------------- |
| 10 | |
Georg Brandl | 621cd26 | 2008-03-28 08:06:56 +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 | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 13 | |
Georg Brandl | fc29f27 | 2009-01-02 20:25:14 +0000 | [diff] [blame] | 14 | The data compression is provided by the :mod:`zlib` module. |
Georg Brandl | 621cd26 | 2008-03-28 08:06:56 +0000 | [diff] [blame] | 15 | |
| 16 | The :mod:`gzip` module provides the :class:`GzipFile` class which is modeled |
| 17 | after Python's File Object. The :class:`GzipFile` class reads and writes |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 18 | :program:`gzip`\ -format files, automatically compressing or decompressing the |
Georg Brandl | 621cd26 | 2008-03-28 08:06:56 +0000 | [diff] [blame] | 19 | data so that it looks like an ordinary file object. |
| 20 | |
| 21 | Note that additional file formats which can be decompressed by the |
| 22 | :program:`gzip` and :program:`gunzip` programs, such as those produced by |
| 23 | :program:`compress` and :program:`pack`, are not supported by this module. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 24 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 25 | The module defines the following items: |
| 26 | |
| 27 | |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 28 | .. class:: GzipFile([filename[, mode[, compresslevel[, fileobj[, mtime]]]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 29 | |
| 30 | Constructor for the :class:`GzipFile` class, which simulates most of the methods |
| 31 | of a file object, with the exception of the :meth:`readinto` and |
| 32 | :meth:`truncate` methods. At least one of *fileobj* and *filename* must be |
| 33 | given a non-trivial value. |
| 34 | |
| 35 | The new class instance is based on *fileobj*, which can be a regular file, a |
Serhiy Storchaka | 6d5bd52 | 2013-08-29 11:44:44 +0300 | [diff] [blame] | 36 | :class:`~StringIO.StringIO` object, or any other object which simulates a file. It |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 37 | defaults to ``None``, in which case *filename* is opened to provide a file |
| 38 | object. |
| 39 | |
| 40 | When *fileobj* is not ``None``, the *filename* argument is only used to be |
Georg Brandl | d796cdb | 2013-10-06 12:33:20 +0200 | [diff] [blame] | 41 | included in the :program:`gzip` file header, which may include the original |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 42 | filename of the uncompressed file. It defaults to the filename of *fileobj*, if |
| 43 | discernible; otherwise, it defaults to the empty string, and in this case the |
| 44 | original filename is not included in the header. |
| 45 | |
| 46 | The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``, ``'w'``, |
| 47 | or ``'wb'``, depending on whether the file will be read or written. The default |
| 48 | is the mode of *fileobj* if discernible; otherwise, the default is ``'rb'``. If |
| 49 | not given, the 'b' flag will be added to the mode to ensure the file is opened |
| 50 | in binary mode for cross-platform portability. |
| 51 | |
Nadeem Vawda | 04050b8 | 2012-11-11 13:52:10 +0100 | [diff] [blame] | 52 | The *compresslevel* argument is an integer from ``0`` to ``9`` controlling |
| 53 | the level of compression; ``1`` is fastest and produces the least |
| 54 | compression, and ``9`` is slowest and produces the most compression. ``0`` |
| 55 | is no compression. The default is ``9``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 56 | |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 57 | The *mtime* argument is an optional numeric timestamp to be written to |
Georg Brandl | 38f1bf6 | 2009-03-09 16:35:48 +0000 | [diff] [blame] | 58 | the stream when compressing. All :program:`gzip` compressed streams are |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 59 | required to contain a timestamp. If omitted or ``None``, the current |
| 60 | time is used. This module ignores the timestamp when decompressing; |
| 61 | however, some programs, such as :program:`gunzip`\ , make use of it. |
| 62 | The format of the timestamp is the same as that of the return value of |
Senthil Kumaran | 6f18b98 | 2011-07-04 12:50:02 -0700 | [diff] [blame] | 63 | ``time.time()`` and of the ``st_mtime`` attribute of the object returned |
Antoine Pitrou | f0d2c3f | 2009-01-04 21:29:23 +0000 | [diff] [blame] | 64 | by ``os.stat()``. |
| 65 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 66 | Calling a :class:`GzipFile` object's :meth:`close` method does not close |
| 67 | *fileobj*, since you might wish to append more material after the compressed |
Serhiy Storchaka | 6d5bd52 | 2013-08-29 11:44:44 +0300 | [diff] [blame] | 68 | data. This also allows you to pass a :class:`~StringIO.StringIO` object opened for |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 69 | writing as *fileobj*, and retrieve the resulting memory buffer using the |
Serhiy Storchaka | 6d5bd52 | 2013-08-29 11:44:44 +0300 | [diff] [blame] | 70 | :class:`StringIO` object's :meth:`~StringIO.StringIO.getvalue` method. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 71 | |
Georg Brandl | d1068be | 2010-03-21 09:09:38 +0000 | [diff] [blame] | 72 | :class:`GzipFile` supports iteration and the :keyword:`with` statement. |
Benjamin Peterson | 6d83429 | 2009-03-09 20:38:56 +0000 | [diff] [blame] | 73 | |
| 74 | .. versionchanged:: 2.7 |
| 75 | Support for the :keyword:`with` statement was added. |
| 76 | |
Antoine Pitrou | 5a9112c | 2010-01-13 14:32:10 +0000 | [diff] [blame] | 77 | .. versionchanged:: 2.7 |
| 78 | Support for zero-padded files was added. |
| 79 | |
Georg Brandl | 188ddd3 | 2013-10-06 19:01:21 +0200 | [diff] [blame] | 80 | .. versionadded:: 2.7 |
| 81 | The *mtime* argument. |
| 82 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 83 | |
| 84 | .. function:: open(filename[, mode[, compresslevel]]) |
| 85 | |
| 86 | This is a shorthand for ``GzipFile(filename,`` ``mode,`` ``compresslevel)``. |
| 87 | The *filename* argument is required; *mode* defaults to ``'rb'`` and |
| 88 | *compresslevel* defaults to ``9``. |
| 89 | |
| 90 | |
Georg Brandl | 621cd26 | 2008-03-28 08:06:56 +0000 | [diff] [blame] | 91 | .. _gzip-usage-examples: |
| 92 | |
| 93 | Examples of usage |
| 94 | ----------------- |
| 95 | |
| 96 | Example of how to read a compressed file:: |
| 97 | |
| 98 | import gzip |
Petri Lehtinen | 0b78503 | 2013-02-23 19:24:08 +0100 | [diff] [blame] | 99 | f = gzip.open('file.txt.gz', 'rb') |
Georg Brandl | 621cd26 | 2008-03-28 08:06:56 +0000 | [diff] [blame] | 100 | file_content = f.read() |
| 101 | f.close() |
| 102 | |
| 103 | Example of how to create a compressed GZIP file:: |
| 104 | |
| 105 | import gzip |
| 106 | content = "Lots of content here" |
Petri Lehtinen | 0b78503 | 2013-02-23 19:24:08 +0100 | [diff] [blame] | 107 | f = gzip.open('file.txt.gz', 'wb') |
Georg Brandl | 621cd26 | 2008-03-28 08:06:56 +0000 | [diff] [blame] | 108 | f.write(content) |
| 109 | f.close() |
| 110 | |
| 111 | Example of how to GZIP compress an existing file:: |
| 112 | |
| 113 | import gzip |
Petri Lehtinen | 0b78503 | 2013-02-23 19:24:08 +0100 | [diff] [blame] | 114 | f_in = open('file.txt', 'rb') |
| 115 | f_out = gzip.open('file.txt.gz', 'wb') |
Georg Brandl | 621cd26 | 2008-03-28 08:06:56 +0000 | [diff] [blame] | 116 | f_out.writelines(f_in) |
| 117 | f_out.close() |
| 118 | f_in.close() |
| 119 | |
| 120 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 121 | .. seealso:: |
| 122 | |
| 123 | Module :mod:`zlib` |
| 124 | The basic data compression module needed to support the :program:`gzip` file |
| 125 | format. |
| 126 | |