Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 1 | :mod:`bz2` --- Support for :program:`bzip2` compression |
| 2 | ======================================================= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: bz2 |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 5 | :synopsis: Interfaces for bzip2 compression and decompression. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | .. moduleauthor:: Gustavo Niemeyer <niemeyer@conectiva.com> |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 7 | .. moduleauthor:: Nadeem Vawda <nadeem.vawda@gmail.com> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. sectionauthor:: Gustavo Niemeyer <niemeyer@conectiva.com> |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 9 | .. sectionauthor:: Nadeem Vawda <nadeem.vawda@gmail.com> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
| 11 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 12 | This module provides a comprehensive interface for compressing and |
| 13 | decompressing data using the bzip2 compression algorithm. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 15 | For related file formats, see the :mod:`gzip`, :mod:`zipfile`, and |
Guido van Rossum | 7767711 | 2007-11-05 19:43:04 +0000 | [diff] [blame] | 16 | :mod:`tarfile` modules. |
| 17 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 18 | The :mod:`bz2` module contains: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 20 | * 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 26 | All of the classes in this module may safely be accessed from multiple threads. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | (De)compression of files |
| 30 | ------------------------ |
| 31 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 32 | .. class:: BZ2File(filename=None, mode='r', buffering=None, compresslevel=9, fileobj=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 34 | Open a bzip2-compressed file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 36 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 40 | The *mode* argument can be either ``'r'`` for reading (default), or ``'w'`` |
| 41 | for writing. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 43 | 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 Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 62 | |
Benjamin Peterson | 10745a9 | 2009-03-09 21:08:47 +0000 | [diff] [blame] | 63 | .. versionchanged:: 3.1 |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 64 | Support for the :keyword:`with` statement was added. |
| 65 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 66 | .. versionchanged:: 3.3 |
| 67 | The :meth:`fileno`, :meth:`readable`, :meth:`seekable`, :meth:`writable`, |
| 68 | :meth:`read1` and :meth:`readinto` methods were added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 70 | .. versionchanged:: 3.3 |
| 71 | The *fileobj* argument to the constructor was added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | |
| 73 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 74 | Incremental (de)compression |
| 75 | --------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 77 | .. class:: BZ2Compressor(compresslevel=9) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
| 79 | Create a new compressor object. This object may be used to compress data |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 80 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 86 | .. method:: compress(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 88 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
| 94 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 95 | .. method:: flush() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 97 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 | |
| 102 | |
| 103 | .. class:: BZ2Decompressor() |
| 104 | |
| 105 | Create a new decompressor object. This object may be used to decompress data |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 106 | incrementally. For one-shot compression, use the :func:`decompress` function |
| 107 | instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 109 | .. method:: decompress(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 111 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 | |
| 130 | |
| 131 | One-shot (de)compression |
| 132 | ------------------------ |
| 133 | |
Georg Brandl | 0d8f073 | 2009-04-05 22:20:44 +0000 | [diff] [blame] | 134 | .. function:: compress(data, compresslevel=9) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 136 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 | |
| 143 | |
| 144 | .. function:: decompress(data) |
| 145 | |
Antoine Pitrou | 37dc5f8 | 2011-04-03 17:05:46 +0200 | [diff] [blame] | 146 | Decompress *data*. |
| 147 | |
| 148 | For incremental decompression, use a :class:`BZ2Decompressor` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 | |