Utopia Compression
Compression algorithms (Brotli, Deflate, GZIP, LZ4, Snappy, XZ, Zstd) and Accept-Encoding negotiation, byte for byte as PHP's extensions.
This library is maintained by the Appwrite team.
Getting started
[]
= { = "https://github.com/appwrite/appwrite" }
This library compresses and decompresses data with seven algorithms behind one Algorithm trait: Brotli, Deflate, Gzip, LZ4, Snappy, XZ and Zstd. It also picks the algorithm to answer an HTTP request with, from the client's Accept-Encoding header. The output matches what PHP's extensions write, so either runtime reads what the other wrote. This guide covers the common tasks.
Compress a response for the client
Compression::from_accept_encoding reads the header and returns the accepted algorithm with the highest q value, or None when the response should go out as is. Send the result with its content_encoding() as the Content-Encoding header.
use ;
let body = b"{\"users\": [], \"total\": 0}".repeat;
match from_accept_encoding
Offer only some algorithms
Pass the names you offer. Clients' preferences decide among them, and the first one listed in the header wins a tie. none and identity mean no compression.
use ;
let chosen = from_accept_encoding;
assert_eq!;
assert!;
Choose an algorithm and its level
Each algorithm is a type you can build directly. Brotli, LZ4 and Zstd have levels: set_level checks the range and fails with Error::InvalidArgument outside it.
use ;
let mut zstd = new;
zstd.set_level?;
let packed = zstd.compress?;
assert_eq!;
let mut brotli = new;
assert_eq!;
Write into your own buffer
Each algorithm's compress_into and decompress_into append to a Sink: a Vec<u8>, or a buffer type of your own that implements it, so the output is written once, where it ends up. The PHP extension writes straight into the PHP string it returns this way.
use ;
let mut out = b"header:".to_vec;
new.compress_into?;
assert_eq!;
Handle data that is not compressed
Decompressing data that is not a valid stream fails with Error::Codec, whose message is the warning PHP's extension emits. PHP returns an empty string in that case.
use ;
assert_eq!;
Add your own algorithm
Implement Algorithm for a type. content_encoding() defaults to the name in lower case.
use ;
/// Reverses the bytes: not compression, but an inverse pair.
;
assert_eq!;
assert_eq!;
Contributing
Development happens in appwrite/appwrite, where this crate lives at crates/utopia/compression. Run these commands from a checkout of it. The contributing guide covers each step.
Test
Compare with PHP
Replay the results PHP recorded for every case against Rust, without PHP:
COMPAT_LIB=compression
Run every case on both runtimes, fuzz both with generated inputs, then run the whole conversion check. These need the dev stack (docker compose up -d):
Benchmark
This times every benchmark on PHP and on a release build of Rust, both in containers on the dev stack. It fails if Rust is slower. Add --write to record the run in tests/compat/compression/bench.json. Run it on a quiet machine, one library at a time.
License
BSD-3-Clause. See LICENSE.