Tar compression seemingly working

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-23 03:10:25 -03:00
parent 2c0f2b380c
commit d72ca9eeae
6 changed files with 63 additions and 33 deletions

View File

@ -14,5 +14,5 @@ pub enum Entry {
}
pub trait Compressor {
fn compress(&self, from: Vec<PathBuf>) -> OuchResult<Vec<u8>>;
fn compress(&self, from: Entry) -> OuchResult<Vec<u8>>;
}

View File

@ -2,4 +2,5 @@ mod tar;
mod compressor;
pub use compressor::{Compressor};
pub use self::tar::TarCompressor;
pub use self::tar::TarCompressor;
pub use self::compressor::Entry;

View File

@ -4,7 +4,9 @@ use colored::Colorize;
use tar::{Builder, Header};
use walkdir::WalkDir;
use crate::{compressors::Compressor, error::{Error, OuchResult}, file::{self, File}};
use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File};
use super::compressor::Entry;
pub struct TarCompressor {}
@ -33,19 +35,19 @@ impl TarCompressor {
Ok(b.into_inner()?)
}
fn make_archive_from_files(input_files: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
let buf = Vec::new();
let mut b = Builder::new(buf);
for file in input_files {
for entry in WalkDir::new(&file) {
let entry = entry.unwrap();
for filename in input_filenames {
for entry in WalkDir::new(&filename) {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
continue;
}
b.append_file(path, &mut fs::File::open(path).unwrap()).unwrap();
b.append_file(path, &mut fs::File::open(path)?)?;
}
}
@ -54,9 +56,19 @@ impl TarCompressor {
}
impl Compressor for TarCompressor {
fn compress(&self, from: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
Ok(
TarCompressor::make_archive_from_files(from)?
)
fn compress(&self, from: Entry) -> OuchResult<Vec<u8>> {
match from {
Entry::Files(filenames) => {
Ok(
Self::make_archive_from_files(filenames)?
)
},
Entry::InMemory(file) => {
Ok(
Self::make_archive_from_memory(file)?
)
}
}
}
}

View File

@ -86,4 +86,12 @@ impl From<niffler::error::Error> for Error {
NifErr::IOError(io_err) => Self::from(io_err)
}
}
}
impl From<walkdir::Error> for Error {
fn from(err: walkdir::Error) -> Self {
eprintln!("{}: {}", "error".red(), err);
Self::InvalidInput
}
}

View File

@ -2,7 +2,7 @@ use std::{ffi::OsStr, fs, io::Write, path::PathBuf};
use colored::Colorize;
use crate::{compressors::TarCompressor, decompressors::TarDecompressor};
use crate::{compressors::{Entry, TarCompressor}, decompressors::TarDecompressor};
use crate::decompressors::ZipDecompressor;
use crate::{
cli::{Command, CommandKind},
@ -155,8 +155,34 @@ impl Evaluator {
Ok(())
}
fn compress_files(files: Vec<PathBuf>, output: File) -> error::OuchResult<()> {
let (first_decompressor, second_decompressor) = Self::get_compressor(&output)?;
fn compress_files(files: Vec<PathBuf>, mut output: File) -> error::OuchResult<()> {
let (first_compressor, second_compressor) = Self::get_compressor(&output)?;
let output_path = output.path.clone();
let bytes = match first_compressor {
Some(first_compressor) => {
let mut entry = Entry::Files(files);
let bytes = first_compressor.compress(entry)?;
output.contents = Some(bytes);
entry = Entry::InMemory(output);
second_compressor.compress(entry)?
},
None => {
let entry = Entry::Files(files);
second_compressor.compress(entry)?
}
};
println!("{}: writing to {:?}. ({} bytes)", "info".yellow(), &output_path, bytes.len());
fs::write(
output_path,
bytes)?;
Ok(())
}

View File

@ -29,23 +29,6 @@ fn main() -> error::OuchResult<()>{
print_error(err)
}
}
// let compressor = TarCompressor {};
// let file = File {
// path: PathBuf::from("target"),
// contents: None,
// extension: None,
// };
// let ok = compressor.compress(vec![file])?;
// let ok = match ok {
// CompressionResult::TarArchive(bytes) => bytes,
// _ => unreachable!()
// };
// fs::write(Path::new("great.tar"), ok)?;
Ok(())
}