blob: de5063a7c7cf20ea99de4c1da95f8c2b1d081950 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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 Hettinger469271d2011-01-27 20:38:46 +00007**Source code:** :source:`Lib/gzip.py`
8
9--------------
10
Christian Heimesbbe741d2008-03-28 10:53:29 +000011This module provides a simple interface to compress and decompress files just
12like the GNU programs :program:`gzip` and :program:`gunzip` would.
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl1f01deb2009-01-03 22:47:39 +000014The data compression is provided by the :mod:`zlib` module.
Christian Heimesbbe741d2008-03-28 10:53:29 +000015
Nadeem Vawda7e126202012-05-06 15:04:01 +020016The :mod:`gzip` module provides the :class:`GzipFile` class, as well as the
Nadeem Vawda68721012012-06-04 23:21:38 +020017:func:`.open`, :func:`compress` and :func:`decompress` convenience functions.
18The :class:`GzipFile` class reads and writes :program:`gzip`\ -format files,
19automatically compressing or decompressing the data so that it looks like an
20ordinary :term:`file object`.
Christian Heimesbbe741d2008-03-28 10:53:29 +000021
22Note 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 Brandl116aa622007-08-15 14:28:22 +000025
Georg Brandl116aa622007-08-15 14:28:22 +000026The module defines the following items:
27
28
Nadeem Vawda7e126202012-05-06 15:04:01 +020029.. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
30
Nadeem Vawda68721012012-06-04 23:21:38 +020031 Open a gzip-compressed file in binary or text mode, returning a :term:`file
32 object`.
Nadeem Vawda7e126202012-05-06 15:04:01 +020033
Nadeem Vawda68721012012-06-04 23:21:38 +020034 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 Vawda7e126202012-05-06 15:04:01 +020036
37 The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``,
Nadeem Vawdaee1be992013-10-19 00:11:13 +020038 ``'w'``, ``'wb'``, ``'x'`` or ``'xb'`` for binary mode, or ``'rt'``,
39 ``'at'``, ``'wt'``, or ``'xt'`` for text mode. The default is ``'rb'``.
Nadeem Vawda7e126202012-05-06 15:04:01 +020040
Nadeem Vawda6ff262e2012-11-11 14:14:47 +010041 The *compresslevel* argument is an integer from 0 to 9, as for the
Nadeem Vawda7e126202012-05-06 15:04:01 +020042 :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 Vawda68721012012-06-04 23:21:38 +020053 Added support for *filename* being a file object, support for text mode,
54 and the *encoding*, *errors* and *newline* arguments.
Nadeem Vawda7e126202012-05-06 15:04:01 +020055
Nadeem Vawdaee1be992013-10-19 00:11:13 +020056 .. versionchanged:: 3.4
57 Added support for the ``'x'``, ``'xb'`` and ``'xt'`` modes.
58
Nadeem Vawda7e126202012-05-06 15:04:01 +020059
Georg Brandl036490d2009-05-17 13:00:36 +000060.. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
Georg Brandl116aa622007-08-15 14:28:22 +000061
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +000062 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 Brandl116aa622007-08-15 14:28:22 +000066
67 The new class instance is based on *fileobj*, which can be a regular file, a
Serhiy Storchakae79be872013-08-17 00:09:55 +030068 :class:`io.BytesIO` object, or any other object which simulates a file. It
Georg Brandl116aa622007-08-15 14:28:22 +000069 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 Brandlf27bfd82013-10-06 12:33:20 +020073 included in the :program:`gzip` file header, which may include the original
Georg Brandl116aa622007-08-15 14:28:22 +000074 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 Vawdaee1be992013-10-19 00:11:13 +020079 ``'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 Vawda30d94b72012-02-11 23:45:10 +020082
Nadeem Vawda7e126202012-05-06 15:04:01 +020083 Note that the file is always opened in binary mode. To open a compressed file
Nadeem Vawda68721012012-06-04 23:21:38 +020084 in text mode, use :func:`.open` (or wrap your :class:`GzipFile` with an
Nadeem Vawda7e126202012-05-06 15:04:01 +020085 :class:`io.TextIOWrapper`).
Georg Brandl116aa622007-08-15 14:28:22 +000086
Nadeem Vawda19e568d2012-11-11 14:04:14 +010087 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 Brandl116aa622007-08-15 14:28:22 +000091
Antoine Pitrou42db3ef2009-01-04 21:37:59 +000092 The *mtime* argument is an optional numeric timestamp to be written to
Benjamin Petersone0124bd2009-03-09 21:04:33 +000093 the stream when compressing. All :program:`gzip` compressed streams are
Antoine Pitrou42db3ef2009-01-04 21:37:59 +000094 required to contain a timestamp. If omitted or ``None``, the current
95 time is used. This module ignores the timestamp when decompressing;
96 however, some programs, such as :program:`gunzip`\ , make use of it.
97 The format of the timestamp is the same as that of the return value of
Senthil Kumarana6bac952011-07-04 11:28:30 -070098 ``time.time()`` and of the ``st_mtime`` attribute of the object returned
Antoine Pitrou42db3ef2009-01-04 21:37:59 +000099 by ``os.stat()``.
100
Georg Brandl116aa622007-08-15 14:28:22 +0000101 Calling a :class:`GzipFile` object's :meth:`close` method does not close
102 *fileobj*, since you might wish to append more material after the compressed
Antoine Pitroue5768cf2010-09-23 16:45:17 +0000103 data. This also allows you to pass a :class:`io.BytesIO` object opened for
Georg Brandl116aa622007-08-15 14:28:22 +0000104 writing as *fileobj*, and retrieve the resulting memory buffer using the
Antoine Pitroue5768cf2010-09-23 16:45:17 +0000105 :class:`io.BytesIO` object's :meth:`~io.BytesIO.getvalue` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +0000107 :class:`GzipFile` supports the :class:`io.BufferedIOBase` interface,
108 including iteration and the :keyword:`with` statement. Only the
109 :meth:`truncate` method isn't implemented.
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000110
Antoine Pitrou7b998e92010-10-04 21:55:14 +0000111 :class:`GzipFile` also provides the following method:
112
113 .. method:: peek([n])
114
115 Read *n* uncompressed bytes without advancing the file position.
116 At most one single read on the compressed stream is done to satisfy
117 the call. The number of bytes returned may be more or less than
118 requested.
119
120 .. versionadded:: 3.2
121
Benjamin Peterson10745a92009-03-09 21:08:47 +0000122 .. versionchanged:: 3.1
Georg Brandlffb94ae2013-10-06 19:02:08 +0200123 Support for the :keyword:`with` statement was added, along with the
124 *mtime* argument.
Benjamin Petersone0124bd2009-03-09 21:04:33 +0000125
Antoine Pitrou8e33fd72010-01-13 14:37:26 +0000126 .. versionchanged:: 3.2
Georg Brandlffb94ae2013-10-06 19:02:08 +0200127 Support for zero-padded and unseekable files was added.
Antoine Pitrou7b969842010-09-23 16:22:51 +0000128
Antoine Pitrou6b4be362011-04-04 21:09:05 +0200129 .. versionchanged:: 3.3
130 The :meth:`io.BufferedIOBase.read1` method is now implemented.
131
Nadeem Vawdaee1be992013-10-19 00:11:13 +0200132 .. versionchanged:: 3.4
133 Added support for the ``'x'`` and ``'xb'`` modes.
134
Georg Brandl116aa622007-08-15 14:28:22 +0000135
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000136.. function:: compress(data, compresslevel=9)
137
138 Compress the *data*, returning a :class:`bytes` object containing
139 the compressed data. *compresslevel* has the same meaning as in
140 the :class:`GzipFile` constructor above.
141
Antoine Pitroucdfe1c52010-08-17 21:15:00 +0000142 .. versionadded:: 3.2
143
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000144.. function:: decompress(data)
145
146 Decompress the *data*, returning a :class:`bytes` object containing the
147 uncompressed data.
148
Antoine Pitroucdfe1c52010-08-17 21:15:00 +0000149 .. versionadded:: 3.2
150
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Christian Heimesbbe741d2008-03-28 10:53:29 +0000152.. _gzip-usage-examples:
153
154Examples of usage
155-----------------
156
157Example of how to read a compressed file::
158
159 import gzip
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000160 with gzip.open('/home/joe/file.txt.gz', 'rb') as f:
161 file_content = f.read()
Christian Heimesbbe741d2008-03-28 10:53:29 +0000162
163Example of how to create a compressed GZIP file::
164
165 import gzip
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000166 content = b"Lots of content here"
167 with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
168 f.write(content)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000169
170Example of how to GZIP compress an existing file::
171
172 import gzip
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000173 with open('/home/joe/file.txt', 'rb') as f_in:
Éric Araujof5be0902010-08-17 21:24:05 +0000174 with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000175 f_out.writelines(f_in)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000176
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000177Example of how to GZIP compress a binary string::
178
179 import gzip
180 s_in = b"Lots of content here"
181 s_out = gzip.compress(s_in)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000182
Georg Brandl116aa622007-08-15 14:28:22 +0000183.. seealso::
184
185 Module :mod:`zlib`
186 The basic data compression module needed to support the :program:`gzip` file
187 format.
188