Simplify Lzma decompression logic

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-24 12:56:12 -03:00
parent 320f27ff8f
commit 433f8b05b0
6 changed files with 54 additions and 77 deletions

View File

@ -1,7 +1,7 @@
use std::{fs, path::PathBuf};
use colored::Colorize;
use tar::{Builder, Header};
use tar::{Builder, EntryType, Header};
use walkdir::WalkDir;
use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File};
@ -23,23 +23,26 @@ impl TarCompressor {
}
};
let mut header = Header::new_gnu();
// header.set_path(&input.path.file_stem().unwrap())?;
header.set_path(".")?;
header.set_size(contents.len() as u64);
header.set_cksum();
header.set_mode(644);
// let ok = EntryType::
let mut b = Builder::new(Vec::new());
b.append_data(
&mut header,
&input.path.file_stem().unwrap(),
&*contents
)?;
// let mut b = Builder::new(Vec::new());
// let mut header = Header::new_gnu();
// let name = b"././@LongLink";
// header.as_gnu_mut().unwrap().name[..name.len()].clone_from_slice(&name[..]);
// header.set_mode(0o644);
// header.set_uid(0);
// header.set_gid(0);
// header.set_mtime(0);
// // + 1 to be compliant with GNU tar
// header.set_size(size + 1);
// header.set_entry_type(EntryType::new(entry_type));
// header.set_cksum();
Ok(b.into_inner()?)
// Ok(b.into_inner()?)
Ok(vec![])
}
fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
@ -48,7 +51,7 @@ impl TarCompressor {
let mut b = Builder::new(buf);
for filename in input_filenames {
// TODO: check if filename is a file or a directory
// TODO: check if file exists
for entry in WalkDir::new(&filename) {
let entry = entry?;

View File

@ -1,45 +0,0 @@
use std::{fs, io::Read};
use colored::Colorize;
use crate::{error::OuchResult, utils};
use crate::file::File;
use super::decompressor::{DecompressionResult, Decompressor};
pub struct LzmaDecompressor {}
impl LzmaDecompressor {
fn extract_to_memory(from: File) -> OuchResult<Vec<u8>> {
let mut ret = vec![];
let from_path = from.path;
if !from_path.exists() {
eprintln!("{}: could not find {:?}", "error".red(), from_path);
}
let input_bytes = fs::read(&from_path)?;
xz2::read::XzDecoder::new_multi_decoder(&*input_bytes)
.read_to_end(&mut ret)?;
println!("{}: extracted {:?} into memory. ({} bytes)", "info".yellow(), from_path, ret.len());
Ok(ret)
}
}
impl Decompressor for LzmaDecompressor {
fn decompress(&self, from: File, into: &Option<File>) -> OuchResult<DecompressionResult> {
let destination_path = utils::get_destination_path(into);
utils::create_path_if_non_existent(destination_path)?;
Ok(
DecompressionResult::FileInMemory(
Self::extract_to_memory(from)?
)
)
}
}

View File

@ -1,14 +1,18 @@
mod decompressor;
mod unified;
mod lzma;
mod tomemory;
mod tar;
mod zip;
pub use decompressor::Decompressor;
pub use decompressor::DecompressionResult;
pub use self::tar::TarDecompressor;
pub use self::zip::ZipDecompressor;
pub use self::unified::GzipDecompressor;
pub use self::unified::BzipDecompressor;
pub use self::lzma::LzmaDecompressor;
// These decompressors only decompress to memory,
// unlike {Tar, Zip}Decompressor which are capable of
// decompressing directly to storage
pub use self::tomemory::GzipDecompressor;
pub use self::tomemory::BzipDecompressor;
pub use self::tomemory::LzmaDecompressor;

View File

@ -18,12 +18,14 @@ use super::decompressor::Decompressor;
pub struct UnifiedDecompressor {}
pub struct GzipDecompressor {}
pub struct LzmaDecompressor {}
pub struct BzipDecompressor {}
fn get_decoder<'a>(format: CompressionFormat, buffer: Box<dyn io::Read + Send + 'a>) -> Box<dyn io::Read + Send + 'a> {
match format {
CompressionFormat::Bzip => Box::new(bzip2::read::BzDecoder::new(buffer)),
CompressionFormat::Gzip => Box::new(flate2::read::MultiGzDecoder::new(buffer)),
CompressionFormat::Lzma => Box::new(xz2::read::XzDecoder::new_multi_decoder(buffer)),
_other => unreachable!()
}
}
@ -68,4 +70,10 @@ impl Decompressor for BzipDecompressor {
fn decompress(&self, from: File, into: &Option<File>) -> OuchResult<DecompressionResult> {
UnifiedDecompressor::decompress(from, CompressionFormat::Bzip, into)
}
}
impl Decompressor for LzmaDecompressor {
fn decompress(&self, from: File, into: &Option<File>) -> OuchResult<DecompressionResult> {
UnifiedDecompressor::decompress(from, CompressionFormat::Lzma, into)
}
}

View File

@ -93,13 +93,12 @@ impl Evaluator {
CompressionFormat::Zip => Box::new(ZipDecompressor {}),
CompressionFormat::Gzip => Box::new(GzipDecompressor{}),
CompressionFormat::Gzip => Box::new(GzipDecompressor {}),
CompressionFormat::Lzma => Box::new(LzmaDecompressor{}),
CompressionFormat::Lzma => Box::new(LzmaDecompressor {}),
CompressionFormat::Bzip => {
Box::new(BzipDecompressor {})
}
CompressionFormat::Bzip => Box::new(BzipDecompressor {})
};
let first_decompressor: Option<Box<dyn Decompressor>> = match extension.first_ext {

View File

@ -34,16 +34,24 @@ fn main() -> error::OuchResult<()>{
}
// fn main() -> error::OuchResult<()> {
// let bytes = fs::read("extension.tar.lzma")?;
// let mut ret = vec![];
// use tar::{Builder};
// use walkdir::WalkDir;
// xz2::read::XzDecoder::new_multi_decoder(&*bytes)
// .read_to_end(&mut ret)
// .unwrap();
// let mut b = Builder::new(Vec::new());
// for entry in WalkDir::new("src") {
// let entry = entry?;
// let mut file = std::fs::File::open(entry.path())?;
// b.append_file(entry.path(), &mut file)?;
// }
// fs::write("extension.tar", &*bytes).unwrap();
// // let mut file = std::fs::File::open("Cargo.toml")?;
// // b.append_file("Cargo.toml", &mut file)?;
// let bytes = b.into_inner()?;
// std::fs::write("daaaaamn.tar", bytes)?;
// Ok(())
// }