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
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 errorCompresses 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 errorUnpacks 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 errortext compressed, as base64 (a value cannot hold bytes), at level 1 to 19.
unzstd_text(b64: str) → stror an errorBase64 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.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/zstd.qwer$ curl -O https://quillolang.org/plugins/files/zstd.qweThen, in your program, all of it or only what you call:
use zstd.qweruse zstd_file, unzstd_file from zstd // Quillo 0.6: only theseIt uses these crates, which cargo fetches the first time the plugin is built:
zstd = { version = "0.13", default-features = false }base64 = "0.22"Example
// 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
//! 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())}Related
bzip2.qwer · 1.0.0bzip2 compression of files and text in pure Rust, .bz2 dumps unpacked, and their first lines read without unpacking them all.6 views · 7 downloadsLZ4 lz4.qwer · 1.0.0LZ4 compression of files and text in the standard .lz4 frame format: the fastest to pack and unpack, for caches, logs and data passed between services.6 views · 7 downloadsTar tar.qwer · 1.0.0Tar archives, gzipped or not: made from files and folders, listed, one file read, and opened safely into a folder.7 views · 8 downloadsxz xz.qwer · 1.0.0xz (LZMA2) compression of files and text, and old .lzma files read: the smallest archives, for backups and downloads.6 views · 8 downloadsGzip gzip.qwer · 1.0.0Gzip files and text, and unpack them: smaller backups, exports and payloads.7 views · 6 downloads7-Zip sevenz.qwer · 1.0.07-Zip archives listed, one file read, opened safely into a folder, and made from a folder.7 views · 7 downloadsThread 0 posts
On Zstandard: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.