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