Version 1.0.0 · 4 functions · 2 crates
Functions
What use gzip.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use gzip_file, gunzip_file from gzip (Quillo 0.6).
gzip_file(from: str, to: str, level: int) → intor an errorGzips the file at from into to, at level 1 (fastest) to 9 (smallest); the size written, in bytes.
gunzip_file(from: str, to: str) → intor an errorUnpacks the gzip file at from into to; the size written, in bytes.
gzip_text(text: str) → stror an errortext gzipped, as base64 (a value cannot hold bytes).
gunzip_text(b64: str) → stror an errorBase64 of gzipped text, back to the text.
Get it
$ quillo install gzip # gzip.qwer into this folder$ quillo install gzip --example # and its example, gzip.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/gzip.qwer$ curl -O https://quillolang.org/plugins/files/gzip.qweThen, in your program, all of it or only what you call:
main.qwe
use gzip.qweruse gzip_file, gunzip_file from gzip // Quillo 0.6: only theseIt uses these crates, which cargo fetches the first time the plugin is built:
[dependencies]
flate2 = "1"base64 = "0.22"Example
gzip.qwe
// Gzip a file, and text both ways.//// quillo run gzip.qwe then open http://localhost:8080/packuse "gzip.qwer"route GET /pack { write_file("report.txt", repeat("Quillo packs this line well. ", 2000)) let size = gzip_file("report.txt", "report.txt.gz", 9)? let packed = gzip_text("hello, hello, hello")? { file_bytes: size, text: packed, back: gunzip_text(packed)? }}$ quillo run gzip.qwe$ curl 'localhost:8080/pack'Source
gzip.qwer
//! Gzip files and text, and read them back: smaller backups, exports and API payloads.//! version: 1.0.0//!//! [dependencies]//! flate2 = "1"//! base64 = "0.22"use base64::engine::general_purpose::STANDARD;use base64::Engine;use flate2::read::GzDecoder;use flate2::write::GzEncoder;use flate2::Compression;use std::io::{Read, Write};fn compression(level: i64) -> Compression { Compression::new(level.clamp(1, 9) as u32)}/// Gzips the file at `from` into `to`, at `level` 1 (fastest) to 9 (smallest); the size written, in bytes.pub fn gzip_file(from: &str, to: &str, level: i64) -> Result<i64, String> { let mut input = std::fs::File::open(from).map_err(|e| format!("{}: {}", from, e))?; let out = std::fs::File::create(to).map_err(|e| format!("{}: {}", to, e))?; let mut enc = GzEncoder::new(out, compression(level)); std::io::copy(&mut input, &mut enc).map_err(|e| e.to_string())?; enc.finish().map_err(|e| e.to_string())?; Ok(std::fs::metadata(to).map_err(|e| e.to_string())?.len() as i64)}/// Unpacks the gzip file at `from` into `to`; the size written, in bytes.pub fn gunzip_file(from: &str, to: &str) -> Result<i64, String> { let input = std::fs::File::open(from).map_err(|e| format!("{}: {}", from, e))?; let mut out = std::fs::File::create(to).map_err(|e| format!("{}: {}", to, e))?; let n = std::io::copy(&mut GzDecoder::new(input), &mut out).map_err(|e| e.to_string())?; Ok(n as i64)}/// `text` gzipped, as base64 (a value cannot hold bytes).pub fn gzip_text(text: &str) -> Result<String, String> { let mut enc = GzEncoder::new(Vec::new(), Compression::best()); enc.write_all(text.as_bytes()).map_err(|e| e.to_string())?; Ok(STANDARD.encode(enc.finish().map_err(|e| e.to_string())?))}/// Base64 of gzipped text, back to the text.pub fn gunzip_text(b64: &str) -> Result<String, String> { let bytes = STANDARD.decode(b64.trim()).map_err(|e| e.to_string())?; let mut out = String::new(); GzDecoder::new(&bytes[..]).read_to_string(&mut out).map_err(|e| e.to_string())?; Ok(out)}Related
bzip2
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 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 downloadsZstandard zstd.qwer · 1.0.0Zstandard compression of files and text: faster than gzip at the same size, for backups, logs and caches.8 views · 6 downloadsBencode and torrents bencode.qwer · 1.0.0Bencode, BitTorrent's format, encoded and decoded; .torrent files read (files, sizes, trackers, info hash, magnet link) and made from a file.7 views · 10 downloadsEncoding encoding.qwer · 1.0.0Base64, base64url, hex and percent-encoding read back, files as base64 and back, and query strings as maps.7 views · 7 downloadsThread
Thread 0 posts
On Gzip: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.