Files

Zstandard

Zstandard compression of files and text: faster than gzip at the same size, for backups, logs and caches.

Version 1.0.0 · 4 functions · 2 crates

#compression#files#backups#logs

Functions

What use zstd.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use zstd_file, unzstd_file from zstd (Quillo 0.6).

zstd_file(from: str, to: str, level: int) → intor an error

Compresses the file at from into to (often from + ".zst"), at level 1 (fastest) to 19 (smallest; 3 is zstd's usual); the size written, in bytes.

unzstd_file(from: str, to: str) → intor an error

Unpacks the .zst file at from into to: the size written, in bytes. Stops with an error past 16 GB.

zstd_text(text: str, level: int) → stror an error

text compressed, as base64 (a value cannot hold bytes), at level 1 to 19.

unzstd_text(b64: str) → stror an error

Base64 of zstd-compressed text, back to the text; up to 64 MB of it.

Get it

$ quillo install zstd               # zstd.qwer into this folder$ quillo install zstd --example     # and its example, zstd.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/zstd.qwer$ curl -O https://quillolang.org/plugins/files/zstd.qwe

Then, in your program, all of it or only what you call:

main.qwe
use zstd.qweruse zstd_file, unzstd_file from zstd      // Quillo 0.6: only these

It uses these crates, which cargo fetches the first time the plugin is built:

[dependencies]
zstd = { version = "0.13", default-features = false }base64 = "0.22"

Example

zstd.qwe
// A log file compressed with zstd, and text both ways.////   quillo run zstd.qwe      then open http://localhost:8080/packuse "zstd.qwer"route GET /pack {    let lines = for i in range(5000) => `2026-10-29T10:00:${i % 60} GET /menu 200 ${i % 7}ms`    write_file("data/access.log", join(lines, "\n"))    let size = zstd_file("data/access.log", "data/access.log.zst", 19)?    let back = unzstd_file("data/access.log.zst", "data/access.copy.log")?    let packed = zstd_text("hello, hello, hello", 3)?    { log_bytes: len(read_file("data/access.log")), zst_bytes: size, unpacked_bytes: back, text: packed, back: unzstd_text(packed)? }}
$ quillo run zstd.qwe$ curl 'localhost:8080/pack'

Source

zstd.qwer
//! Zstandard compression of files and text: faster than gzip at the same size, for backups, logs and caches.//! version: 1.0.0//!//! [dependencies]//! zstd = { version = "0.13", default-features = false }//! base64 = "0.22"use base64::engine::general_purpose::STANDARD;use base64::Engine;use std::io::Read;const MAX_FILE: u64 = 16 * 1024 * 1024 * 1024;const MAX_TEXT: u64 = 64 * 1024 * 1024;fn clamp_level(l: i64) -> i32 {    l.clamp(1, 19) as i32}fn create(to: &str) -> Result<std::fs::File, String> {    if let Some(dir) = std::path::Path::new(to).parent().filter(|d| !d.as_os_str().is_empty()) {        std::fs::create_dir_all(dir).map_err(|e| format!("{}: {}", dir.display(), e))?;    }    std::fs::File::create(to).map_err(|e| format!("{}: {}", to, e))}/// Compresses the file at `from` into `to` (often `from` + ".zst"), at `level` 1 (fastest) to 19 (smallest; 3 is zstd's usual); the size written, in bytes.pub fn zstd_file(from: &str, to: &str, level: i64) -> Result<i64, String> {    let input = std::fs::File::open(from).map_err(|e| format!("{}: {}", from, e))?;    let out = create(to)?;    zstd::stream::copy_encode(input, out, clamp_level(level)).map_err(|e| e.to_string())?;    Ok(std::fs::metadata(to).map_err(|e| e.to_string())?.len() as i64)}/// Unpacks the .zst file at `from` into `to`: the size written, in bytes. Stops with an error past 16 GB.pub fn unzstd_file(from: &str, to: &str) -> Result<i64, String> {    let input = std::fs::File::open(from).map_err(|e| format!("{}: {}", from, e))?;    let dec = zstd::stream::Decoder::new(input).map_err(|e| format!("{}: {}", from, e))?;    let mut out = create(to)?;    let n = std::io::copy(&mut dec.take(MAX_FILE + 1), &mut out).map_err(|e| format!("{}: {}", from, e))?;    if n > MAX_FILE {        return Err(format!("{}: more than 16 GB unpacked", from));    }    Ok(n as i64)}/// `text` compressed, as base64 (a value cannot hold bytes), at `level` 1 to 19.pub fn zstd_text(text: &str, level: i64) -> Result<String, String> {    Ok(STANDARD.encode(zstd::stream::encode_all(text.as_bytes(), clamp_level(level)).map_err(|e| e.to_string())?))}/// Base64 of zstd-compressed text, back to the text; up to 64 MB of it.pub fn unzstd_text(b64: &str) -> Result<String, String> {    let bytes = STANDARD.decode(b64.trim()).map_err(|e| e.to_string())?;    let dec = zstd::stream::Decoder::new(&bytes[..]).map_err(|e| e.to_string())?;    let mut out = Vec::new();    dec.take(MAX_TEXT + 1).read_to_end(&mut out).map_err(|e| e.to_string())?;    if out.len() as u64 > MAX_TEXT {        return Err("more than 64 MB of text".into());    }    String::from_utf8(out).map_err(|_| "the data is not UTF-8 text".to_string())}
Thread
86Useful? Like it, or add to its thread below.

Thread 0 posts

On Zstandard: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.