blob: 73fd4504040a12c50f39353d663593951493be8a [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`bz2` --- Compression compatible with :program:`bzip2`
3===========================================================
4
5.. module:: bz2
6 :synopsis: Interface to compression and decompression routines compatible with bzip2.
7.. moduleauthor:: Gustavo Niemeyer <niemeyer@conectiva.com>
8.. sectionauthor:: Gustavo Niemeyer <niemeyer@conectiva.com>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011This module provides a comprehensive interface for the bz2 compression library.
12It implements a complete file interface, one-shot (de)compression functions, and
13types for sequential (de)compression.
14
15Here is a resume of the features offered by the bz2 module:
16
17* :class:`BZ2File` class implements a complete file interface, including
18 :meth:`readline`, :meth:`readlines`, :meth:`writelines`, :meth:`seek`, etc;
19
20* :class:`BZ2File` class implements emulated :meth:`seek` support;
21
22* :class:`BZ2File` class implements universal newline support;
23
24* :class:`BZ2File` class offers an optimized line iteration using the readahead
25 algorithm borrowed from file objects;
26
27* Sequential (de)compression supported by :class:`BZ2Compressor` and
28 :class:`BZ2Decompressor` classes;
29
30* One-shot (de)compression supported by :func:`compress` and :func:`decompress`
31 functions;
32
33* Thread safety uses individual locking mechanism;
34
35* Complete inline documentation;
36
37
38(De)compression of files
39------------------------
40
41Handling of compressed files is offered by the :class:`BZ2File` class.
42
43
44.. class:: BZ2File(filename[, mode[, buffering[, compresslevel]]])
45
46 Open a bz2 file. Mode can be either ``'r'`` or ``'w'``, for reading (default)
47 or writing. When opened for writing, the file will be created if it doesn't
48 exist, and truncated otherwise. If *buffering* is given, ``0`` means unbuffered,
49 and larger numbers specify the buffer size; the default is ``0``. If
50 *compresslevel* is given, it must be a number between ``1`` and ``9``; the
51 default is ``9``. Add a ``'U'`` to mode to open the file for input with
52 universal newline support. Any line ending in the input file will be seen as a
53 ``'\n'`` in Python. Also, a file so opened gains the attribute
54 :attr:`newlines`; the value for this attribute is one of ``None`` (no newline
55 read yet), ``'\r'``, ``'\n'``, ``'\r\n'`` or a tuple containing all the newline
56 types seen. Universal newlines are available only when reading. Instances
57 support iteration in the same way as normal :class:`file` instances.
58
59
60.. method:: BZ2File.close()
61
62 Close the file. Sets data attribute :attr:`closed` to true. A closed file cannot
63 be used for further I/O operations. :meth:`close` may be called more than once
64 without error.
65
66
67.. method:: BZ2File.read([size])
68
69 Read at most *size* uncompressed bytes, returned as a string. If the *size*
70 argument is negative or omitted, read until EOF is reached.
71
72
73.. method:: BZ2File.readline([size])
74
75 Return the next line from the file, as a string, retaining newline. A
76 non-negative *size* argument limits the maximum number of bytes to return (an
77 incomplete line may be returned then). Return an empty string at EOF.
78
79
80.. method:: BZ2File.readlines([size])
81
82 Return a list of lines read. The optional *size* argument, if given, is an
83 approximate bound on the total number of bytes in the lines returned.
84
85
86.. method:: BZ2File.seek(offset[, whence])
87
88 Move to new file position. Argument *offset* is a byte count. Optional argument
89 *whence* defaults to ``os.SEEK_SET`` or ``0`` (offset from start of file; offset
90 should be ``>= 0``); other values are ``os.SEEK_CUR`` or ``1`` (move relative to
91 current position; offset can be positive or negative), and ``os.SEEK_END`` or
92 ``2`` (move relative to end of file; offset is usually negative, although many
93 platforms allow seeking beyond the end of a file).
94
95 Note that seeking of bz2 files is emulated, and depending on the parameters the
96 operation may be extremely slow.
97
98
99.. method:: BZ2File.tell()
100
101 Return the current file position, an integer (may be a long integer).
102
103
104.. method:: BZ2File.write(data)
105
106 Write string *data* to file. Note that due to buffering, :meth:`close` may be
107 needed before the file on disk reflects the data written.
108
109
110.. method:: BZ2File.writelines(sequence_of_strings)
111
112 Write the sequence of strings to the file. Note that newlines are not added. The
113 sequence can be any iterable object producing strings. This is equivalent to
114 calling write() for each string.
115
116
117Sequential (de)compression
118--------------------------
119
120Sequential compression and decompression is done using the classes
121:class:`BZ2Compressor` and :class:`BZ2Decompressor`.
122
123
124.. class:: BZ2Compressor([compresslevel])
125
126 Create a new compressor object. This object may be used to compress data
127 sequentially. If you want to compress data in one shot, use the :func:`compress`
128 function instead. The *compresslevel* parameter, if given, must be a number
129 between ``1`` and ``9``; the default is ``9``.
130
131
132.. method:: BZ2Compressor.compress(data)
133
134 Provide more data to the compressor object. It will return chunks of compressed
135 data whenever possible. When you've finished providing data to compress, call
136 the :meth:`flush` method to finish the compression process, and return what is
137 left in internal buffers.
138
139
140.. method:: BZ2Compressor.flush()
141
142 Finish the compression process and return what is left in internal buffers. You
143 must not use the compressor object after calling this method.
144
145
146.. class:: BZ2Decompressor()
147
148 Create a new decompressor object. This object may be used to decompress data
149 sequentially. If you want to decompress data in one shot, use the
150 :func:`decompress` function instead.
151
152
153.. method:: BZ2Decompressor.decompress(data)
154
155 Provide more data to the decompressor object. It will return chunks of
156 decompressed data whenever possible. If you try to decompress data after the end
157 of stream is found, :exc:`EOFError` will be raised. If any data was found after
158 the end of stream, it'll be ignored and saved in :attr:`unused_data` attribute.
159
160
161One-shot (de)compression
162------------------------
163
164One-shot compression and decompression is provided through the :func:`compress`
165and :func:`decompress` functions.
166
167
168.. function:: compress(data[, compresslevel])
169
170 Compress *data* in one shot. If you want to compress data sequentially, use an
171 instance of :class:`BZ2Compressor` instead. The *compresslevel* parameter, if
172 given, must be a number between ``1`` and ``9``; the default is ``9``.
173
174
175.. function:: decompress(data)
176
177 Decompress *data* in one shot. If you want to decompress data sequentially, use
178 an instance of :class:`BZ2Decompressor` instead.
179