compress
The compress package compresses and decompresses data in memory. Every function accepts a string or bytes and returns bytes; call .to_string() on the result to get text back.
| Format | Compress | Decompress |
|---|---|---|
| DEFLATE | deflate | inflate |
| GZip | gzip | gunzip |
| Brotli | brotli | unbrotli |
| Zstandard | zstd | unzstd |
| ZIP archive | zip_create | zip_extract |
Round trips
Section titled “Round trips”text = "Kiwi is a scripting language. " * 50
packed = compress::gzip(text)println packed.size() < text.size() # trueprintln compress::gunzip(packed).to_string() == text # true
smaller = compress::zstd(text, 10) # Zstandard, compression level 10println compress::unzstd(smaller).to_string() == text # trueTo write compressed data to disk, use fio::writebytes:
fio::writebytes("log.txt.gz", compress::gzip(fio::read("log.txt")))ZIP archives
Section titled “ZIP archives”zip_create takes a list of {name, content} hashmaps and returns the archive as bytes. zip_extract does the reverse.
archive = compress::zip_create([ {name: "hello.txt", content: "Hello from Kiwi!"}, {name: "data.csv", content: "1,2,3\n4,5,6"}])
for entry in compress::zip_extract(archive) do println "${entry["name"]}: ${entry["content"].size()} bytes"endFunctions
Section titled “Functions”compress::deflate
Section titled “compress::deflate”compress::deflate(data)Compresses data using the Deflate algorithm.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | any | The data to compress. |
Returns bytes — The compressed data.
compress::inflate
Section titled “compress::inflate”compress::inflate(data)Decompresses Deflate-compressed data.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | bytes | The compressed data. |
Returns any — The decompressed data.
compress::gzip
Section titled “compress::gzip”compress::gzip(data)Compresses data using the gzip algorithm.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | any | The data to compress. |
Returns bytes — The gzip-compressed data.
compress::gunzip
Section titled “compress::gunzip”compress::gunzip(data)Decompresses gzip-compressed data.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | bytes | The compressed data. |
Returns any — The decompressed data.
compress::brotli
Section titled “compress::brotli”compress::brotli(data)Compresses data using the Brotli algorithm.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | any | The data to compress. |
Returns bytes — The Brotli-compressed data.
compress::unbrotli
Section titled “compress::unbrotli”compress::unbrotli(data)Decompresses Brotli-compressed data.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | bytes | The compressed data. |
Returns any — The decompressed data.
compress::zstd
Section titled “compress::zstd”compress::zstd(data, level = 3)Compresses data using the Zstandard (zstd) algorithm.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | any | The data to compress. | |
level | integer | 3 | Compression level (default 3). |
Returns bytes — The zstd-compressed data.
compress::unzstd
Section titled “compress::unzstd”compress::unzstd(data)Decompresses Zstandard-compressed data.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | bytes | The compressed data. |
Returns any — The decompressed data.
compress::zip_create
Section titled “compress::zip_create”compress::zip_create(entries, level = 1)Creates a ZIP archive from a list of entries.
| Parameter | Type | Default | Description |
|---|---|---|---|
entries | list | A list of file entries to include in the archive. | |
level | integer | 1 | Compression level (default 1). |
Returns bytes — The ZIP archive as bytes.
compress::zip_extract
Section titled “compress::zip_extract”compress::zip_extract(data)Extracts entries from a ZIP archive.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | bytes | The ZIP archive as bytes. |
Returns list — A list of extracted entries.