mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
Simplify Lzma decompression logic
This commit is contained in:
parent
320f27ff8f
commit
433f8b05b0
@ -1,7 +1,7 @@
|
|||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use tar::{Builder, Header};
|
use tar::{Builder, EntryType, Header};
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File};
|
use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File};
|
||||||
@ -23,23 +23,26 @@ impl TarCompressor {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut header = Header::new_gnu();
|
// let ok = EntryType::
|
||||||
|
|
||||||
// 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 mut b = Builder::new(Vec::new());
|
// let mut b = Builder::new(Vec::new());
|
||||||
b.append_data(
|
// let mut header = Header::new_gnu();
|
||||||
&mut header,
|
// let name = b"././@LongLink";
|
||||||
&input.path.file_stem().unwrap(),
|
// header.as_gnu_mut().unwrap().name[..name.len()].clone_from_slice(&name[..]);
|
||||||
&*contents
|
// 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>> {
|
fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
|
||||||
@ -48,7 +51,7 @@ impl TarCompressor {
|
|||||||
let mut b = Builder::new(buf);
|
let mut b = Builder::new(buf);
|
||||||
|
|
||||||
for filename in input_filenames {
|
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) {
|
for entry in WalkDir::new(&filename) {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
|
@ -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)?
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +1,18 @@
|
|||||||
mod decompressor;
|
mod decompressor;
|
||||||
mod unified;
|
mod tomemory;
|
||||||
mod lzma;
|
|
||||||
mod tar;
|
mod tar;
|
||||||
mod zip;
|
mod zip;
|
||||||
|
|
||||||
|
|
||||||
pub use decompressor::Decompressor;
|
pub use decompressor::Decompressor;
|
||||||
pub use decompressor::DecompressionResult;
|
pub use decompressor::DecompressionResult;
|
||||||
|
|
||||||
pub use self::tar::TarDecompressor;
|
pub use self::tar::TarDecompressor;
|
||||||
pub use self::zip::ZipDecompressor;
|
pub use self::zip::ZipDecompressor;
|
||||||
pub use self::unified::GzipDecompressor;
|
|
||||||
pub use self::unified::BzipDecompressor;
|
// These decompressors only decompress to memory,
|
||||||
pub use self::lzma::LzmaDecompressor;
|
// 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;
|
@ -18,12 +18,14 @@ use super::decompressor::Decompressor;
|
|||||||
|
|
||||||
pub struct UnifiedDecompressor {}
|
pub struct UnifiedDecompressor {}
|
||||||
pub struct GzipDecompressor {}
|
pub struct GzipDecompressor {}
|
||||||
|
pub struct LzmaDecompressor {}
|
||||||
pub struct BzipDecompressor {}
|
pub struct BzipDecompressor {}
|
||||||
|
|
||||||
fn get_decoder<'a>(format: CompressionFormat, buffer: Box<dyn io::Read + Send + 'a>) -> Box<dyn io::Read + Send + 'a> {
|
fn get_decoder<'a>(format: CompressionFormat, buffer: Box<dyn io::Read + Send + 'a>) -> Box<dyn io::Read + Send + 'a> {
|
||||||
match format {
|
match format {
|
||||||
CompressionFormat::Bzip => Box::new(bzip2::read::BzDecoder::new(buffer)),
|
CompressionFormat::Bzip => Box::new(bzip2::read::BzDecoder::new(buffer)),
|
||||||
CompressionFormat::Gzip => Box::new(flate2::read::MultiGzDecoder::new(buffer)),
|
CompressionFormat::Gzip => Box::new(flate2::read::MultiGzDecoder::new(buffer)),
|
||||||
|
CompressionFormat::Lzma => Box::new(xz2::read::XzDecoder::new_multi_decoder(buffer)),
|
||||||
_other => unreachable!()
|
_other => unreachable!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,4 +70,10 @@ impl Decompressor for BzipDecompressor {
|
|||||||
fn decompress(&self, from: File, into: &Option<File>) -> OuchResult<DecompressionResult> {
|
fn decompress(&self, from: File, into: &Option<File>) -> OuchResult<DecompressionResult> {
|
||||||
UnifiedDecompressor::decompress(from, CompressionFormat::Bzip, into)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
@ -93,13 +93,12 @@ impl Evaluator {
|
|||||||
|
|
||||||
CompressionFormat::Zip => Box::new(ZipDecompressor {}),
|
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 => {
|
CompressionFormat::Bzip => Box::new(BzipDecompressor {})
|
||||||
Box::new(BzipDecompressor {})
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let first_decompressor: Option<Box<dyn Decompressor>> = match extension.first_ext {
|
let first_decompressor: Option<Box<dyn Decompressor>> = match extension.first_ext {
|
||||||
|
20
src/main.rs
20
src/main.rs
@ -34,16 +34,24 @@ fn main() -> error::OuchResult<()>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
// let mut b = Builder::new(Vec::new());
|
||||||
// .read_to_end(&mut ret)
|
|
||||||
// .unwrap();
|
|
||||||
|
|
||||||
|
// 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(())
|
// Ok(())
|
||||||
// }
|
// }
|
Loading…
x
Reference in New Issue
Block a user