Data formats

Gzip

Gzip files and text, and unpack them: smaller backups, exports and payloads.

Version 1.0.0 · 4 functions · 2 crates

#compression#files#backups

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 error

Gzips 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 error

Unpacks the gzip file at from into to; the size written, in bytes.

gzip_text(text: str) → stror an error

text gzipped, as base64 (a value cannot hold bytes).

gunzip_text(b64: str) → stror an error

Base64 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.qwe

Or with curl:

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

Then, 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 these

It 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)}
Thread
76Useful? Like it, or add to its thread below.

Thread 0 posts

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

No posts yet. The first one opens the thread.