blob: a5e08c9d8f947511b10d348790e4efc1ebabb652 [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
Antoine Pitrou11cb9612010-09-15 11:11:28 +000016The :mod:`gzip` module provides the :class:`GzipFile` class. The :class:`GzipFile`
17class reads and writes :program:`gzip`\ -format files, automatically compressing
18or decompressing the data so that it looks like an ordinary :term:`file object`.
Christian Heimesbbe741d2008-03-28 10:53:29 +000019
20Note that additional file formats which can be decompressed by the
21:program:`gzip` and :program:`gunzip` programs, such as those produced by
22:program:`compress` and :program:`pack`, are not supported by this module.
Georg Brandl116aa622007-08-15 14:28:22 +000023
Guido van Rossum77677112007-11-05 19:43:04 +000024For other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and
25:mod:`tarfile` modules.
26
Georg Brandl116aa622007-08-15 14:28:22 +000027The module defines the following items:
28
29
Georg Brandl036490d2009-05-17 13:00:36 +000030.. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
Georg Brandl116aa622007-08-15 14:28:22 +000031
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +000032 Constructor for the :class:`GzipFile` class, which simulates most of the
33 methods of a :term:`file object`, with the exception of the :meth:`truncate`
34 method. At least one of *fileobj* and *filename* must be given a non-trivial
35 value.
Georg Brandl116aa622007-08-15 14:28:22 +000036
37 The new class instance is based on *fileobj*, which can be a regular file, a
38 :class:`StringIO` object, or any other object which simulates a file. It
39 defaults to ``None``, in which case *filename* is opened to provide a file
40 object.
41
42 When *fileobj* is not ``None``, the *filename* argument is only used to be
43 included in the :program:`gzip` file header, which may includes the original
44 filename of the uncompressed file. It defaults to the filename of *fileobj*, if
45 discernible; otherwise, it defaults to the empty string, and in this case the
46 original filename is not included in the header.
47
48 The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``, ``'w'``,
49 or ``'wb'``, depending on whether the file will be read or written. The default
50 is the mode of *fileobj* if discernible; otherwise, the default is ``'rb'``. If
51 not given, the 'b' flag will be added to the mode to ensure the file is opened
52 in binary mode for cross-platform portability.
53
54 The *compresslevel* argument is an integer from ``1`` to ``9`` controlling the
55 level of compression; ``1`` is fastest and produces the least compression, and
56 ``9`` is slowest and produces the most compression. The default is ``9``.
57
Antoine Pitrou42db3ef2009-01-04 21:37:59 +000058 The *mtime* argument is an optional numeric timestamp to be written to
Benjamin Petersone0124bd2009-03-09 21:04:33 +000059 the stream when compressing. All :program:`gzip` compressed streams are
Antoine Pitrou42db3ef2009-01-04 21:37:59 +000060 required to contain a timestamp. If omitted or ``None``, the current
61 time is used. This module ignores the timestamp when decompressing;
62 however, some programs, such as :program:`gunzip`\ , make use of it.
63 The format of the timestamp is the same as that of the return value of
Senthil Kumarana6bac952011-07-04 11:28:30 -070064 ``time.time()`` and of the ``st_mtime`` attribute of the object returned
Antoine Pitrou42db3ef2009-01-04 21:37:59 +000065 by ``os.stat()``.
66
Georg Brandl116aa622007-08-15 14:28:22 +000067 Calling a :class:`GzipFile` object's :meth:`close` method does not close
68 *fileobj*, since you might wish to append more material after the compressed
Antoine Pitroue5768cf2010-09-23 16:45:17 +000069 data. This also allows you to pass a :class:`io.BytesIO` object opened for
Georg Brandl116aa622007-08-15 14:28:22 +000070 writing as *fileobj*, and retrieve the resulting memory buffer using the
Antoine Pitroue5768cf2010-09-23 16:45:17 +000071 :class:`io.BytesIO` object's :meth:`~io.BytesIO.getvalue` method.
Georg Brandl116aa622007-08-15 14:28:22 +000072
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +000073 :class:`GzipFile` supports the :class:`io.BufferedIOBase` interface,
74 including iteration and the :keyword:`with` statement. Only the
75 :meth:`truncate` method isn't implemented.
Benjamin Petersone0124bd2009-03-09 21:04:33 +000076
Antoine Pitrou7b998e92010-10-04 21:55:14 +000077 :class:`GzipFile` also provides the following method:
78
79 .. method:: peek([n])
80
81 Read *n* uncompressed bytes without advancing the file position.
82 At most one single read on the compressed stream is done to satisfy
83 the call. The number of bytes returned may be more or less than
84 requested.
85
86 .. versionadded:: 3.2
87
Benjamin Peterson10745a92009-03-09 21:08:47 +000088 .. versionchanged:: 3.1
Benjamin Petersone0124bd2009-03-09 21:04:33 +000089 Support for the :keyword:`with` statement was added.
90
Antoine Pitrou8e33fd72010-01-13 14:37:26 +000091 .. versionchanged:: 3.2
92 Support for zero-padded files was added.
93
Antoine Pitrou7b969842010-09-23 16:22:51 +000094 .. versionchanged:: 3.2
95 Support for unseekable files was added.
96
Antoine Pitrou6b4be362011-04-04 21:09:05 +020097 .. versionchanged:: 3.3
98 The :meth:`io.BufferedIOBase.read1` method is now implemented.
99
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Georg Brandl036490d2009-05-17 13:00:36 +0000101.. function:: open(filename, mode='rb', compresslevel=9)
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103 This is a shorthand for ``GzipFile(filename,`` ``mode,`` ``compresslevel)``.
104 The *filename* argument is required; *mode* defaults to ``'rb'`` and
105 *compresslevel* defaults to ``9``.
106
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000107.. function:: compress(data, compresslevel=9)
108
109 Compress the *data*, returning a :class:`bytes` object containing
110 the compressed data. *compresslevel* has the same meaning as in
111 the :class:`GzipFile` constructor above.
112
Antoine Pitroucdfe1c52010-08-17 21:15:00 +0000113 .. versionadded:: 3.2
114
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000115.. function:: decompress(data)
116
117 Decompress the *data*, returning a :class:`bytes` object containing the
118 uncompressed data.
119
Antoine Pitroucdfe1c52010-08-17 21:15:00 +0000120 .. versionadded:: 3.2
121
Georg Brandl116aa622007-08-15 14:28:22 +0000122
Christian Heimesbbe741d2008-03-28 10:53:29 +0000123.. _gzip-usage-examples:
124
125Examples of usage
126-----------------
127
128Example of how to read a compressed file::
129
130 import gzip
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000131 with gzip.open('/home/joe/file.txt.gz', 'rb') as f:
132 file_content = f.read()
Christian Heimesbbe741d2008-03-28 10:53:29 +0000133
134Example of how to create a compressed GZIP file::
135
136 import gzip
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000137 content = b"Lots of content here"
138 with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
139 f.write(content)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000140
141Example of how to GZIP compress an existing file::
142
143 import gzip
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000144 with open('/home/joe/file.txt', 'rb') as f_in:
Éric Araujof5be0902010-08-17 21:24:05 +0000145 with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000146 f_out.writelines(f_in)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000147
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000148Example of how to GZIP compress a binary string::
149
150 import gzip
151 s_in = b"Lots of content here"
152 s_out = gzip.compress(s_in)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000153
Georg Brandl116aa622007-08-15 14:28:22 +0000154.. seealso::
155
156 Module :mod:`zlib`
157 The basic data compression module needed to support the :program:`gzip` file
158 format.
159