My aggregated monorepo of OCaml code, automaintained
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

readme

+89
+89
ocaml-brotli/README.md
··· 1 + # ocaml-brotli 2 + 3 + A pure OCaml implementation of the [Brotli](https://www.rfc-editor.org/rfc/rfc7932) compression format (RFC 7932). 4 + 5 + This implementation is based on the algorithms and data structures from the reference [Google Brotli C library](https://github.com/google/brotli), rewritten in idiomatic OCaml without any C dependencies. 6 + 7 + ## Features 8 + 9 + - Pure OCaml - no C bindings or external dependencies 10 + - Compression quality levels 0-11 11 + - Simple string-based API 12 + - Low-allocation buffer API 13 + - Streaming compression support 14 + - Optional [bytesrw](https://erratique.ch/software/bytesrw) integration for stream processing 15 + 16 + ## Installation 17 + 18 + ``` 19 + opam install brotli 20 + ``` 21 + 22 + ## Usage 23 + 24 + ### Simple API 25 + 26 + ```ocaml 27 + (* Compress a string *) 28 + let compressed = Brotli.compress "Hello, World!" 29 + 30 + (* Compress with higher quality (0-11, default is 1) *) 31 + let compressed = Brotli.compress ~quality:6 data 32 + 33 + (* Decompress *) 34 + match Brotli.decompress compressed with 35 + | Ok decompressed -> print_endline decompressed 36 + | Error msg -> failwith msg 37 + 38 + (* Or use the exception-raising variant *) 39 + let decompressed = Brotli.decompress_exn compressed 40 + ``` 41 + 42 + ### Low-allocation API 43 + 44 + ```ocaml 45 + let src = Bytes.of_string "data to compress" 46 + let dst = Bytes.create (Brotli.max_compressed_length (Bytes.length src)) 47 + 48 + let compressed_len = 49 + Brotli.compress_into 50 + ~src ~src_pos:0 ~src_len:(Bytes.length src) 51 + ~dst ~dst_pos:0 () 52 + ``` 53 + 54 + ### With bytesrw (optional) 55 + 56 + When the `bytesrw` package is installed, the `brotli.bytesrw` sublibrary provides streaming compression and decompression: 57 + 58 + ```ocaml 59 + open Bytesrw 60 + 61 + (* Compress a byte reader *) 62 + let compressed_reader = Bytesrw_brotli.compress_reads ~quality:4 reader 63 + 64 + (* Decompress a byte reader *) 65 + let decompressed_reader = Bytesrw_brotli.decompress_reads reader 66 + ``` 67 + 68 + ## Quality Levels 69 + 70 + | Quality | Description | 71 + |---------|-------------| 72 + | 0 | Stored (uncompressed) blocks only | 73 + | 1 | Huffman-only compression (default) | 74 + | 2-3 | LZ77 with simple hash table matching | 75 + | 4 | Hash chains with dictionary matching | 76 + | 5-6 | Context mode selection | 77 + | 7-9 | Multiple literal Huffman trees | 78 + | 10-11 | Optimal parsing | 79 + 80 + Higher quality levels produce smaller output but take longer to compress. 81 + 82 + ## References 83 + 84 + - [RFC 7932 - Brotli Compressed Data Format](https://www.rfc-editor.org/rfc/rfc7932) 85 + - [Google Brotli C library](https://github.com/google/brotli) - the reference implementation this library is based on 86 + 87 + ## License 88 + 89 + ISC