Docs/Building applications

Compression

Shrink data and files with gzip, zlib, or DEFLATE.

On this page

valk.compress implements gzip, zlib and DEFLATE. gzip is the format of .gz files and of HTTP gzip encoding; zlib is what HTTP calls deflate.

Compress data in memory#

Valk
use valk.compress

fn main() {
    let text = "hello hello hello"
    let packed = compress.gzip(text)
    let unpacked = compress.gunzip(packed) ! {
        println("Corrupt data")
        return
    }
    println(unpacked == text) // true
}

gzip, zlib and deflate return compressed bytes; gunzip, unzlib and inflate reverse them and fail on corrupt input. A second argument sets the level from 0 (store only) to 9; the default is 6.

Compress a stream#

For data that should not be held in memory all at once, Compressor writes compressed output to any io.Writer, and Decompressor reads from any io.Reader:

Valk
use valk.compress
use valk.fs
use valk.io

fn main() {
    let file = fs.stream("log.txt.gz", fs.OpenOptions { read: false, write: fs.WriteMode.truncate, create: true }) ! return
    let writer = compress.Compressor.new(file, compress.Format.gzip)
    writer.write("first line\n") ! return
    writer.close() ! return
    file.close() ! return

    let input = fs.stream("log.txt.gz") ! return
    let reader = compress.Decompressor.new(input, compress.Format.gzip)
    let text = io.read_all(reader) ! return
    println(text) // first line
}

Closing the Compressor writes the final block and the trailer, so close it before closing the file underneath it. Decompressing untrusted data can take an optional max_size to bound the output.