8 use super::decompressor::{DecompressionResult, Decompressor};
9 use crate::{extension::CompressionFormat, file::File, utils};
11 struct DecompressorToMemory;
12 pub struct GzipDecompressor;
13 pub struct LzmaDecompressor;
14 pub struct BzipDecompressor;
17 format: CompressionFormat,
18 buffer: Box<dyn io::Read + Send + 'a>,
19 ) -> Box<dyn io::Read + Send + 'a> {
21 CompressionFormat::Bzip => Box::new(bzip2::read::BzDecoder::new(buffer)),
22 CompressionFormat::Gzip => Box::new(flate2::read::MultiGzDecoder::new(buffer)),
23 CompressionFormat::Lzma => Box::new(xz2::read::XzDecoder::new_multi_decoder(buffer)),
24 _other => unreachable!(),
28 impl DecompressorToMemory {
29 fn unpack_file(path: &Path, format: CompressionFormat) -> crate::Result<Vec<u8>> {
30 let file = std::fs::read(path)?;
32 let mut reader = get_decoder(format, Box::new(&file[..]));
34 let mut buffer = Vec::new();
35 let bytes_read = reader.read_to_end(&mut buffer)?;
38 "{}: {:?} extracted into memory ({}).",
41 utils::Bytes::new(bytes_read as u64)
49 format: CompressionFormat,
51 ) -> crate::Result<DecompressionResult> {
52 let destination_path = utils::get_destination_path(into);
54 utils::create_path_if_non_existent(destination_path)?;
56 let bytes = Self::unpack_file(&from.path, format)?;
58 Ok(DecompressionResult::FileInMemory(bytes))
62 impl Decompressor for GzipDecompressor {
68 ) -> crate::Result<DecompressionResult> {
69 DecompressorToMemory::decompress(from, CompressionFormat::Gzip, into)
73 impl Decompressor for BzipDecompressor {
79 ) -> crate::Result<DecompressionResult> {
80 DecompressorToMemory::decompress(from, CompressionFormat::Bzip, into)
84 impl Decompressor for LzmaDecompressor {
90 ) -> crate::Result<DecompressionResult> {
91 DecompressorToMemory::decompress(from, CompressionFormat::Lzma, into)