blob: 2ccdb516254a760e2614d895dc72ca9b0785536c [file] [log] [blame]
Antoine Pitrou37dc5f82011-04-03 17:05:46 +02001:mod:`bz2` --- Support for :program:`bzip2` compression
2=======================================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: bz2
Antoine Pitrou37dc5f82011-04-03 17:05:46 +02005 :synopsis: Interfaces for bzip2 compression and decompression.
Georg Brandl116aa622007-08-15 14:28:22 +00006.. moduleauthor:: Gustavo Niemeyer <niemeyer@conectiva.com>
Antoine Pitrou37dc5f82011-04-03 17:05:46 +02007.. moduleauthor:: Nadeem Vawda <nadeem.vawda@gmail.com>
Georg Brandl116aa622007-08-15 14:28:22 +00008.. sectionauthor:: Gustavo Niemeyer <niemeyer@conectiva.com>
Antoine Pitrou37dc5f82011-04-03 17:05:46 +02009.. sectionauthor:: Nadeem Vawda <nadeem.vawda@gmail.com>
Georg Brandl116aa622007-08-15 14:28:22 +000010
11
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020012This module provides a comprehensive interface for compressing and
13decompressing data using the bzip2 compression algorithm.
Georg Brandl116aa622007-08-15 14:28:22 +000014
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020015For related file formats, see the :mod:`gzip`, :mod:`zipfile`, and
Guido van Rossum77677112007-11-05 19:43:04 +000016:mod:`tarfile` modules.
17
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020018The :mod:`bz2` module contains:
Georg Brandl116aa622007-08-15 14:28:22 +000019
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020020* The :class:`BZ2File` class for reading and writing compressed files.
21* The :class:`BZ2Compressor` and :class:`BZ2Decompressor` classes for
22 incremental (de)compression.
23* The :func:`compress` and :func:`decompress` functions for one-shot
24 (de)compression.
Georg Brandl116aa622007-08-15 14:28:22 +000025
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020026All of the classes in this module may safely be accessed from multiple threads.
Georg Brandl116aa622007-08-15 14:28:22 +000027
28
29(De)compression of files
30------------------------
31
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020032.. class:: BZ2File(filename=None, mode='r', buffering=None, compresslevel=9, fileobj=None)
Georg Brandl116aa622007-08-15 14:28:22 +000033
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020034 Open a bzip2-compressed file.
Georg Brandl116aa622007-08-15 14:28:22 +000035
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020036 The :class:`BZ2File` can wrap an existing :term:`file object` (given by
37 *fileobj*), or operate directly on a named file (named by *filename*).
38 Exactly one of these two parameters should be provided.
Georg Brandl116aa622007-08-15 14:28:22 +000039
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020040 The *mode* argument can be either ``'r'`` for reading (default), or ``'w'``
41 for writing.
Georg Brandl116aa622007-08-15 14:28:22 +000042
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020043 The *buffering* argument is ignored. Its use is deprecated.
44
45 If *mode* is ``'w'``, *compresslevel* can be a number between ``1`` and
46 ``9`` specifying the level of compression: ``1`` produces the least
47 compression, and ``9`` (default) produces the most compression.
48
49 :class:`BZ2File` provides all of the members specified by the
50 :class:`io.BufferedIOBase`, except for :meth:`detach` and :meth:`truncate`.
51 Iteration and the :keyword:`with` statement are supported.
52
53 :class:`BZ2File` also provides the following method:
54
55 .. method:: peek([n])
56
57 Return buffered data without advancing the file position. At least one
58 byte of data will be returned (unless at EOF). The exact number of bytes
59 returned is unspecified.
60
61 .. versionadded:: 3.3
Benjamin Petersone0124bd2009-03-09 21:04:33 +000062
Benjamin Peterson10745a92009-03-09 21:08:47 +000063 .. versionchanged:: 3.1
Benjamin Petersone0124bd2009-03-09 21:04:33 +000064 Support for the :keyword:`with` statement was added.
65
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020066 .. versionchanged:: 3.3
67 The :meth:`fileno`, :meth:`readable`, :meth:`seekable`, :meth:`writable`,
68 :meth:`read1` and :meth:`readinto` methods were added.
Georg Brandl116aa622007-08-15 14:28:22 +000069
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020070 .. versionchanged:: 3.3
71 The *fileobj* argument to the constructor was added.
Georg Brandl116aa622007-08-15 14:28:22 +000072
73
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020074Incremental (de)compression
75---------------------------
Georg Brandl116aa622007-08-15 14:28:22 +000076
Georg Brandl0d8f0732009-04-05 22:20:44 +000077.. class:: BZ2Compressor(compresslevel=9)
Georg Brandl116aa622007-08-15 14:28:22 +000078
79 Create a new compressor object. This object may be used to compress data
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020080 incrementally. For one-shot compression, use the :func:`compress` function
81 instead.
82
83 *compresslevel*, if given, must be a number between ``1`` and ``9``. The
84 default is ``9``.
Georg Brandl116aa622007-08-15 14:28:22 +000085
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 .. method:: compress(data)
Georg Brandl116aa622007-08-15 14:28:22 +000087
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020088 Provide data to the compressor object. Returns a chunk of compressed data
89 if possible, or an empty byte string otherwise.
90
91 When you have finished providing data to the compressor, call the
92 :meth:`flush` method to finish the compression process.
Georg Brandl116aa622007-08-15 14:28:22 +000093
94
Benjamin Petersone41251e2008-04-25 01:59:09 +000095 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +000096
Antoine Pitrou37dc5f82011-04-03 17:05:46 +020097 Finish the compression process. Returns the compressed data left in
98 internal buffers.
99
100 The compressor object may not be used after this method has been called.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102
103.. class:: BZ2Decompressor()
104
105 Create a new decompressor object. This object may be used to decompress data
Antoine Pitrou37dc5f82011-04-03 17:05:46 +0200106 incrementally. For one-shot compression, use the :func:`decompress` function
107 instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 .. method:: decompress(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Antoine Pitrou37dc5f82011-04-03 17:05:46 +0200111 Provide data to the decompressor object. Returns a chunk of decompressed
112 data if possible, or an empty byte string otherwise.
113
114 Attempting to decompress data after the end of stream is reached raises
115 an :exc:`EOFError`. If any data is found after the end of the stream, it
116 is ignored and saved in the :attr:`unused_data` attribute.
117
118
119 .. attribute:: eof
120
121 True if the end-of-stream marker has been reached.
122
123 .. versionadded:: 3.3
124
125
126 .. attribute:: unused_data
127
128 Data found after the end of the compressed stream.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130
131One-shot (de)compression
132------------------------
133
Georg Brandl0d8f0732009-04-05 22:20:44 +0000134.. function:: compress(data, compresslevel=9)
Georg Brandl116aa622007-08-15 14:28:22 +0000135
Antoine Pitrou37dc5f82011-04-03 17:05:46 +0200136 Compress *data*.
137
138 *compresslevel*, if given, must be a number between ``1`` and ``9``. The
139 default is ``9``.
140
141 For incremental compression, use a :class:`BZ2Compressor` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143
144.. function:: decompress(data)
145
Antoine Pitrou37dc5f82011-04-03 17:05:46 +0200146 Decompress *data*.
147
148 For incremental decompression, use a :class:`BZ2Decompressor` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000149