Created
March 14, 2024 04:30
-
-
Save matthewjberger/29d13c5d34b13dc3125a0a7331c60a67 to your computer and use it in GitHub Desktop.
Snappy compression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "snapper" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
fake = "2.5" | |
snap = "1.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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