Bzip2 compression extension for Rebol3 (version 3.20.5 and higher)
Use Bzip2 as a codec for the standard compress and decompress functions:
import bzip2
bin: compress "some data" 'bzip2
txt: to string! decompress bin 'bzip2Build the extension using Siskin and copy the produced .rebx into your Rebol modules directory.
# Build (Linux x64 example)
./siskin Rebol-Bzip2.nest static-lib-x64
./siskin Rebol-Bzip2.nest bzip2-linux-x64
mv ./build/bzip2-linux-x64.so ./bzip2.rebx
# Install
mkdir -p ~/.rebol/modules
cp -f ./bzip2.rebx ~/.rebol/modules/bzip2.rebxIf you see invalid ELF header, you likely installed the wrong file (e.g. the static archive libbzip2-*.a) instead of the extension .rebx.
Incremental compression uses libbzip2 bz_stream handles (make-encoder, make-decoder, write, read). Output is accumulated on the handle until you call write with /flush or /finish (encoder), which returns a copy of the pending compressed binary and clears the buffer—similar to Rebol-Zstd.
bzip2: import 'bzip2
enc: bzip2/make-encoder
bzip2/write :enc "Hello "
bzip2/write :enc "World"
bin: bzip2/write/finish :enc "!"
text: to string! decompress bin 'bzip2
;== "Hello World!"
dec: bzip2/make-decoder
bzip2/write :dec bin
plain: to string! bzip2/read :decFor large inputs you can feed the decoder in multiple write calls (splitting the compressed binary at byte boundaries is fine once the stream is valid).
Libbzip2 version string (BZ2_bzlibVersion)
Compress data using bzip2
data[binary! any-string!]Input data to compress./partLimit the input data to a given length.length[integer!]Length of input data./levelquality[integer!]Block size 100k: 1 (fast) to 9 (best).
Decompress bzip2 data
data[binary! any-string!]Input data to decompress./partLimit the input data to a given length.length[integer!]Length of input data./sizeLimit the output size.bytes[integer!]Maximum number of uncompressed bytes./maxCap allocated output (ZIP bomb guard).ceiling[integer!]Maximum bytes to allocate while decompressing.
Create a new bzip2 encoder handle.
/levelquality[integer!]Block size 100k: 1 (fast) to 9 (best).
Create a new bzip2 decoder handle.
Feed data into a bzip2 streaming codec.
codec[handle!]Encoder or decoder handle.data[binary! any-string! none!]Data to compress or decompress, or NONE to finish encoder output./flushFlush encoder output (BZ_FLUSH)./finishFinish encoder stream (BZ_FINISH).
Retrieve pending data from the codec buffer.
codec[handle!]
