Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Created March 14, 2024 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjberger/29d13c5d34b13dc3125a0a7331c60a67 to your computer and use it in GitHub Desktop.
Save matthewjberger/29d13c5d34b13dc3125a0a7331c60a67 to your computer and use it in GitHub Desktop.
Snappy compression
[package]
name = "snapper"
version = "0.1.0"
edition = "2021"
[dependencies]
fake = "2.5"
snap = "1.0"
use fake::{faker::lorem::en::Sentence, Fake};
use snap::{read::FrameDecoder, write::FrameEncoder};
use std::{
fs::{self, File},
io::{BufReader, Read, Write},
};
fn main() {
let file_path = "log_lines.snappy";
// Create a Snappy encoder
let file = File::create(file_path).expect("Failed to create file");
let mut encoder = FrameEncoder::new(file);
// Generate and write 20 fake log lines
for _ in 0..20 {
let log_line: String = Sentence(10..20).fake();
encoder
.write_all(log_line.as_bytes())
.expect("Failed to write log line");
encoder.write_all(b"\n").expect("Failed to write newline");
}
// Flush the encoder to ensure all data is written
encoder.flush().expect("Failed to flush encoder");
// Open the Snappy compressed file
let file = File::open(file_path).expect("Failed to open file");
let reader = BufReader::new(file);
let decoder = FrameDecoder::new(reader);
let bytes = decoder.bytes().into_iter().flatten().collect::<Vec<u8>>();
// Write bytes to file
fs::write("uncompressed_output", &dbg!(bytes)).expect("Failed to write to file");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment