compress.bzip2
import compress
compressexposes this ascompress.bzip2, soimport compressis enough and the names are called ascompress.bzip2.*.import compress.bzip2reaches the same definitions directly.
The Bzip2 submodule for the compress module.
Functions
compress()
compress.bzip2.compress(data, level) -> bytes
Compress data using the default options for Bzip2.
Parameters
data(bytes|string)level(?number) — Compression level (1..=9): Default 9.
Returns bytes
decompress()
compress.bzip2.decompress(data) -> bytes
Decompress Bzip2-compressed data.
Parameters
data(bytes|string)
Returns bytes
Note: This only decodes the first bzip2 stream in
data. Some tools (pbzip2, Wikipedia data dumps, …) concatenate several independent bzip2 streams back to back (“multistreams”); useBzip2Decoderin a loop withreset()to decode all of them.
Classes
Bzip2Encoder
class compress.bzip2.Bzip2Encoder
A streaming Bzip2 encoder.
Example:
%> import compress.bzip2
%> var encoder = bzip2.Bzip2Encoder()
%> encoder.write('hello world')
11
%> encoder.finish()
(42 5a 68 39 31 41 59 26 53 59 ...)
Constructor
compress.bzip2.Bzip2Encoder(level, work_factor)
Creates a new streaming Bzip2 encoder.
Parameters
level(?number) — Compression level (1..=9): Default 9.work_factor(?number) — Controls when the compressor falls back from its standard sorting algorithm to a slower but more robust one on highly repetitive input. 0..=250: 0 (the default) means “use bzip2’s own default of 30”.
Bzip2Encoder.finish()
compress.bzip2.Bzip2Encoder.finish() -> bytes
Flushes remaining data, writes the stream footer, and returns the inner byte stream.
Returns bytes
Note: Once finish is called, the encoder can no longer be reused.
Bzip2Encoder.close()
compress.bzip2.Bzip2Encoder.close() -> bytes
Same as finish() for file API compartibility.
Returns bytes
Note: Once finish is called, the encoder can no longer be reused.
Bzip2Encoder.reset()
compress.bzip2.Bzip2Encoder.reset() -> bytes
Finishes the current stream and installs a fresh one for the next frame.
Returns the previous byte stream containing the completed frame.
Returns bytes
Bzip2Encoder.write()
compress.bzip2.Bzip2Encoder.write(data) -> number
Writes data into this encoder’s byte stream, returning how many bytes were written.
Parameters
data(bytes|string)
Returns number
Bzip2Encoder.flush()
compress.bzip2.Bzip2Encoder.flush() -> bytes
Flushes this output stream and returns the compressed data produced so far, without terminating the bzip2 stream.
Returns bytes
Bzip2Encoder.available()
compress.bzip2.Bzip2Encoder.available() -> number
Returns the number of compressed bytes currently waiting in the encoder’s output buffer.
Returns number
Bzip2Encoder.finished()
compress.bzip2.Bzip2Encoder.finished() -> bool
Returns whether finish() has completed the stream.
Returns bool
Bzip2Encoder.total_in()
compress.bzip2.Bzip2Encoder.total_in() -> number
Number of uncompressed bytes consumed by the encoder.
Returns number
Bzip2Encoder.total_out()
compress.bzip2.Bzip2Encoder.total_out() -> number
Number of compressed bytes generated by the encoder.
Returns number
Bzip2Decoder
class compress.bzip2.Bzip2Decoder
Streaming bzip2 decompressor implementation.
Wraps a byte stream of compressed data and yields decompressed bytes.
Example:
%> import compress.bzip2
%> var data = bzip2.compress('hello world')
%> var decoder = bzip2.Bzip2Decoder(data)
%> decoder.read_as_string()
hello world
Note: only decodes a single bzip2 stream: see
decompress()’s note about “multistreams”.
Constructor
compress.bzip2.Bzip2Decoder(source)
Creates a Bzip2 decoder
Parameters
source(bytes)
Bzip2Decoder.reset()
compress.bzip2.Bzip2Decoder.reset(new_source)
Installs a new data source, discarding whatever remained of the previous one.
Parameters
new_source(bytes)
Bzip2Decoder.close()
compress.bzip2.Bzip2Decoder.close()
Closes the decoder by resetting it into an empty stream.
Bzip2Decoder.read()
compress.bzip2.Bzip2Decoder.read(length) -> bytes
Reads some bytes up to the amount of bytes specified by length from the current source. Returns an empty byte stream when there is no more data to read.
Parameters
length(number)
Returns bytes
Bzip2Decoder.read_exact()
compress.bzip2.Bzip2Decoder.read_exact(length) -> bytes
Reads the exact number of bytes from the buffer. This method will raise
an error if it encounters an unexpected EOF (end of file) or there are
insufficient data to read to complete the required number of bytes.
Parameters
length(number)
Returns bytes
Bzip2Decoder.read_all()
compress.bzip2.Bzip2Decoder.read_all() -> bytes
Reads all remaining bytes until EOF is encountered in the source.
Returns bytes
Bzip2Decoder.read_as_string()
compress.bzip2.Bzip2Decoder.read_as_string() -> string
Reads all remaining bytes until EOF is encountered in the source and returns the data read as a string instead of a byte stream.
Returns string
Bzip2Decoder.available()
compress.bzip2.Bzip2Decoder.available() -> number
Returns the number of compressed bytes still unread in the decoder’s source.
Returns number
Bzip2Decoder.finished()
compress.bzip2.Bzip2Decoder.finished() -> bool
Returns whether the decoder has reached the end of the stream.
Returns bool
Bzip2Decoder.total_in()
compress.bzip2.Bzip2Decoder.total_in() -> number
Number of compressed bytes consumed by the decoder.
Returns number
Bzip2Decoder.total_out()
compress.bzip2.Bzip2Decoder.total_out() -> number
Number of uncompressed bytes generated by the decoder.
Returns number
BzFile
class compress.bzip2.BzFile
The BzFile class implements a Bzip2 based I/O system that allows you to treat Bzip2 streams (bytes) as if they were a file.
The class implements the essentials of a file except those that ties it to the operating system filesystem such as symbolic links, chmod and set time.
See the chapter on files in The Zuri Programming Language for more information.
Constructor
compress.bzip2.BzFile(path: string, mode: ?string, _inner)
Returns a new BzFile object bounded to a physical file at the given path and opened in the given mode. See [[file]] for a description of the supported file modes.
Parameters
path(string)mode(?string)
Returns BzFile
BzFile.exists()
compress.bzip2.BzFile.exists() -> bool
Returns true if the underlying file bounded to BzFile actually exists
or false otherwise.
Returns bool
BzFile.close()
compress.bzip2.BzFile.close()
Closes the stream to an opened BzFile. You’ll rarely ever need to call this method yourself in most use cases.
BzFile.flush()
compress.bzip2.BzFile.flush() -> bytes
Flushes the remaning data into the BzFile underlying file and returns the byte stream returned.
Returns bytes
BzFile.open()
compress.bzip2.BzFile.open() -> bool
Opens the stream to a BzFile for the operation originally specified on the BzFile object during creation.
You may need to call this method after a call to read() if the length
isn’t specified or write() if you wish to read or write again as the
BzFile will already be closed.
Returns bool
BzFile.read()
compress.bzip2.BzFile.read(length: ?number) -> bytes
Reads the content of an opened BzFile up to the specified length and returns it as string or bytes if the BzFile was opened in the binary mode. If the length is not specified, the BzFile will be read to the end.
This method requires that the BzFile be opened in the read mode (default mode) or a mode that supports reading. If you aren’t reading the full length of the BzFile, you’ll need to call the close() method to free the BzFile for further reading, otherwise, the close() method will be automatically called for you.
Parameters
length(number) — Default = -1
Returns bytes
Raises Error
BzFile.gets()
compress.bzip2.BzFile.gets(length: ?number) -> bytes
Same as read(), but doesn’t close the BzFile automatically.
Parameters
length(?number) — Default = -1, meaning read to the end.
Returns bytes
Raises Error
BzFile.write()
compress.bzip2.BzFile.write(data: bytes|string) -> number
Writes a string or bytes to an opened BzFile at the current insertion point. When the BzFile is opened with the a mode enabled, write will always start from the end of the BzFile.
If the seek() method has been previously called, write will begin from the seeked position, otherwise it will start at the beginning of the BzFile.
Parameters
- `` (
bytes|string)
Returns number
BzFile.puts()
compress.bzip2.BzFile.puts(data: bytes|string) -> number
Same as write(), but doesn’t open or close the BzFile automatically.
Parameters
- `` (
bytes|string)
Returns number
BzFile.number()
compress.bzip2.BzFile.number() -> number
Returns the integer file descriptor number that is used by the underlying implementation to request I/O operations from the operating system. This can be very useful for low-level interfaces that uses or act as BzFile descriptors.
Returns number
BzFile.is_tty()
compress.bzip2.BzFile.is_tty() -> bool
Returns true if the underlying file of BzFile is a TTY device or
false otherwise.
Returns bool
BzFile.is_open()
compress.bzip2.BzFile.is_open() -> bool
Returns true if the BzFile is open for reading or writing and false
otherwise.
Returns bool
BzFile.is_closed()
compress.bzip2.BzFile.is_closed()
Returns true if the BzFile is closed for reading or writing and
false otherwise.
BzFile.symlink()
compress.bzip2.BzFile.symlink(path: string) -> bool
See [[file.symlink]]
Returns bool
BzFile.stats()
compress.bzip2.BzFile.stats() -> dict
Returns the statistics or details of the BzFile.
See the working with files documentation for more information about the
stats() method.
Returns dict
BzFile.delete()
compress.bzip2.BzFile.delete() -> bool
Deletes the underlying file pointed to by BzFile
Any further attempt to perform most operations on the BzFile after
calling delete() will raise an error.
Returns bool
BzFile.rename()
compress.bzip2.BzFile.rename(new_name) -> bool
Renames the underlying file pointed to by BzFile to the new name and
returns true if it succeeds or false otherwise.
Returns bool
BzFile.copy()
compress.bzip2.BzFile.copy() -> [[compress.bzip2.BzFile]]
Returns a new BzFile reading (or writing) the same underlying path independently of this one, with its own decoder/encoder state and its own position.
Returns [[compress.bzip2.BzFile]]
BzFile.path()
compress.bzip2.BzFile.path() -> string
Returns the physical path of the file pointed to by BzFile.
Returns string
BzFile.abs_path()
compress.bzip2.BzFile.abs_path() -> string
Returns the absolute path of the physical file pointed to by BzFile.
Returns string
BzFile.truncate()
compress.bzip2.BzFile.truncate(length: ?int) -> bool
Truncates the entire BzFile if length is not given or truncates the BzFile such that only length number of bytes is left in it.
Returns bool
BzFile.chmod()
compress.bzip2.BzFile.chmod(number: int) -> bool
Updates the permission for the file bounded by BzFile.
Returns bool
BzFile.set_times()
compress.bzip2.BzFile.set_times(atime: int, mtime: int) -> bool
Sets the last access time and last modified time of the BzFile.
Returns bool
BzFile.seek()
compress.bzip2.BzFile.seek(position: int, seek_type: int) -> bool
Sets the position of a BzFile reader in the decompressed byte stream
(not the raw, still-compressed bytes on disk, which have no useful
correspondence to a decompressed offset). The seek_type argument must
be one of [[io.SEEK_SET]], [[io.SEEK_CUR]] or [[io.SEEK_END]].
Seeking forward simply discards decompressed bytes until the target position, since a streaming decoder has no way to skip ahead without producing them. Seeking backward (or SEEK_END, which has to fully decode the stream once to learn its length) re-opens the underlying file and restarts decompression from the beginning, discarding up to the target position: an inherently expensive operation for a compressed stream, same as with any other streaming (de)compressor.
Not supported on a BzFile opened for writing.
Returns bool
BzFile.tell()
compress.bzip2.BzFile.tell() -> number
Returns the current position of the reader in the decompressed byte
stream (bytes already consumed via read()/gets()/seek()), or of
the writer in the uncompressed input already fed to write()/puts()
for a BzFile opened for writing.
Returns number
BzFile.mode()
compress.bzip2.BzFile.mode() -> string
Returns the mode in which the current BzFile was opened.
Returns string
BzFile.name()
compress.bzip2.BzFile.name() -> string
Returns the name of the file pointed to by BzFile.
Returns string
2026, Richard Ore and Zuri contributors