Brotli extension for Rebol3 (version 3.20.5 and higher)
This extension provides compression and decompression functionality using the Brotli algorithm, which is a modern, efficient compression method widely used for web content.
Basic usage is just using import brotli and then use br method with default Rebol compress and decompress functions. Like:
import brotli
bin: compress "some data" 'br
txt: to string! decompress bin 'brAdditionally, this extension provides a streaming API, allowing data to be (de)compressed in chunks without requiring it to be fully loaded into memory.
brotli: import brotli ;; Import the module and assign it to a variable
enc: brotli/make-encoder ;; Initialize the Brotli encoder state handle
brotli/write :enc "Hello" ;; Process some input data
brotli/write :enc " "
brotli/write :enc "Brotli"
;; When there is enough data to compress,
;; use `read` to finish the current data block and get the encoded chunk
bin1: brotli/read :enc
;; Continue with other data and use `/finish` to encode all remaining input
;; and mark the stream as complete.
bin2: brotli/write/finish :enc " from Rebol!"
;; Decompress both compressed blocks again (using extension's command this time):
text: to string! brotli/decompress join bin1 bin2
;== "Hello Brotli from Rebol!"Using this streaming API, you can write functions like these:
compress-file: function[file][
src: open/read file ;; input file
out: open/new/write join file %.br ;; output file
enc: brotli/make-encoder/level 6 ;; initialize Brotli encoder
enc/size-hint: size? src
enc/mode: 1 ;= text input
chunk-size: 65536
while [not finish][
chunk: copy/part src chunk-size
;; If length of the chunk is less than chunk-size,
;; it must be the last chunk and we can finish the stream.
finish: chunk-size > length? chunk
;; Flush output after each chunk.
write out brotli/write/flush/:finish :enc :chunk
]
close src
close out
]
decompress-file: function[file][
src: open/read file ;; input file
dec: brotli/make-decoder ;; initialize Brotli decoder
chunk-size: 65536
while [not empty? chunk: copy/part src chunk-size][
brotli/write :dec :chunk
]
close src
brotli/read :dec
]Native Brotli version
Compress data using Brotli.
data[binary! any-string!]Input data to compress./partLimit the input data to a given length.length[integer!]Length of input data./levelquality[integer!]Compression level from 0 to 11.
Decompress data using Brotli.
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.
Create a new Brotli encoder handle.
/levelquality[integer!]Compression level from 0 to 11.
Create a new Brotli decoder handle.
Feed data into a Brotli streaming codec.
codec[handle!]Brotli encoder or decoder handle.data[binary! any-string! none!]Data to compress or decompress, or NONE to finish the stream./flushFinish the current data block and return the encoded chunk./finishEncode all remaining input and mark the stream as complete.
Retrieve pending encoded or decoded data from the stream.
codec[handle!]Brotli encoder or decoder handle.
;Refinement Gets Sets Description
/mode none integer! "Tune encoder for specific input. (0-2)"
/size-hint none integer! "Estimated total input size."
/finished integer! none "Return TRUE if encoder reached the final state.";Refinement Gets Sets Description
/finished integer! none "Return TRUE if decoder reached the final state."