1 use std::{fs, io::Write, path::PathBuf};
5 use super::{Compressor, Entry};
7 extension::CompressionFormat,
10 utils::{check_for_multiple_files, ensure_exists},
13 pub struct GzipCompressor {}
16 pub fn compress_files(
18 format: CompressionFormat,
19 ) -> crate::Result<Vec<u8>> {
20 check_for_multiple_files(&files, &format)?;
26 let bytes = fs::read(path)?;
27 Self::compress_bytes(bytes)?
31 "{}: compressed {:?} into memory ({})",
34 Bytes::new(bytes.len() as u64)
40 pub fn compress_file_in_memory(file: File) -> crate::Result<Vec<u8>> {
41 let file_contents = match file.contents_in_memory {
48 Self::compress_bytes(file_contents)
51 pub fn compress_bytes(bytes_to_compress: Vec<u8>) -> crate::Result<Vec<u8>> {
53 let mut encoder = flate2::write::GzEncoder::new(buffer, flate2::Compression::default());
54 encoder.write_all(&*bytes_to_compress)?;
60 impl Compressor for GzipCompressor {
61 fn compress(&self, from: Entry) -> crate::Result<Vec<u8>> {
62 let format = CompressionFormat::Gzip;
64 Entry::Files(files) => Ok(Self::compress_files(files, format)?),
65 Entry::InMemory(file) => Ok(Self::compress_file_in_memory(file)?),