Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Compress String Feed to Base64

See more Compression Examples

This example receives incoming text data in chunks, compresses as a stream, and accumulates the compressed data in base64.

Chilkat Rust Downloads

Rust

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let sb_compressed_base64 = chilkat::StringBuilder::new();

let compress = chilkat::Compression::new();
compress.set_algorithm("deflate");
compress.set_charset("utf-8");
compress.set_encoding_mode("base64");

compress.set_first_chunk(true);
compress.set_last_chunk(false);

let bd_compressed = chilkat::BinData::new();
let sb_uncompressed_chunk = chilkat::StringBuilder::new();

for i in 0..=24 {
    if i == 24 {
        compress.set_last_chunk(true);
    }

    sb_uncompressed_chunk.clear();
    let _ = sb_uncompressed_chunk.append_int(i);
    let _ = sb_uncompressed_chunk.append(": This is a line of data to be compressed...\r\n");

    let _ = compress.compress_sb(&sb_uncompressed_chunk, &bd_compressed);

    compress.set_first_chunk(false);
}

println!("The base64 encoded compressed text:");
println!("{}", bd_compressed.get_encoded("base64").unwrap_or_default());

// Decompress in one call:
let original_text = compress.decompress_string_enc(&bd_compressed.get_encoded("base64").unwrap_or_default()).unwrap_or_default();
println!("{}", original_text);