Files
keep/src/compression_engine/lz4.rs
2025-05-12 20:44:47 -03:00

38 lines
998 B
Rust

use anyhow::Result;
use log::*;
use std::io::Write;
use lz4_flex::frame::{FrameDecoder, FrameEncoder};
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use crate::compression_engine::CompressionEngine;
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct CompressionEngineLZ4 {}
impl CompressionEngineLZ4 {
pub fn new() -> CompressionEngineLZ4 {
CompressionEngineLZ4 {}
}
}
impl CompressionEngine for CompressionEngineLZ4 {
fn open(&self, file_path: PathBuf) -> Result<Box<dyn Read>> {
debug!("COMPRESSION: Opening {:?} using {:?}", file_path, *self);
let file = File::open(file_path)?;
Ok(Box::new(FrameDecoder::new(file)))
}
fn create(&self, file_path: PathBuf) -> Result<Box<dyn Write>> {
debug!("COMPRESSION: Writting to {:?} using {:?}", file_path, *self);
let file = File::create(file_path)?;
let lz4_write = FrameEncoder::new(file).auto_finish();
Ok(Box::new(lz4_write))
}
}