WIP Tar compression

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-23 01:06:57 -03:00
parent 9429fd8d67
commit fa2fb675de
6 changed files with 115 additions and 17 deletions

30
Cargo.lock generated
View File

@ -326,6 +326,7 @@ dependencies = [
"colored",
"niffler",
"tar",
"walkdir",
"zip",
]
@ -368,6 +369,15 @@ version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "strsim"
version = "0.8.0"
@ -466,6 +476,17 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
@ -488,6 +509,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -9,6 +9,7 @@ edition = "2018"
[dependencies]
colored = "2.0.0"
niffler = "2.3.1"
walkdir = "2.3.2"
clap = "2.33.3"
zip = "0.5.11"
tar = "0.4.33"

View File

@ -3,12 +3,13 @@ use std::path::PathBuf;
use crate::{error::OuchResult, file::File};
pub enum CompressionResult {
FilesUnpacked(Vec<PathBuf>),
ZipArchive(Vec<u8>),
TarArchive(Vec<u8>),
FileInMemory(Vec<u8>)
}
pub trait Compressor {
fn compress(&self, from: Vec<File>, into: &Option<File>) -> OuchResult<DecompressionResult>;
fn compress(&self, from: Vec<File>) -> OuchResult<CompressionResult>;
}
//

View File

@ -0,0 +1,5 @@
mod tar;
mod compressor;
pub use compressor::{Compressor, CompressionResult};
pub use self::tar::TarCompressor;

42
src/compressors/tar.rs Normal file
View File

@ -0,0 +1,42 @@
use std::{fs::File, path::Path};
use tar::Builder;
use walkdir::WalkDir;
use crate::{decompressors::TarDecompressor, error::OuchResult};
use crate::compressors::Compressor;
use super::compressor::CompressionResult;
pub struct TarCompressor {}
impl TarCompressor {
fn make_archive_in_memory(input_files: Vec<crate::file::File>) -> OuchResult<Vec<u8>> {
let buf = Vec::new();
let mut b = Builder::new(buf);
for file in input_files {
for entry in WalkDir::new(&file.path) {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
continue;
}
b.append_file(path, &mut File::open(path).unwrap()).unwrap();
}
}
Ok(b.into_inner()?)
}
}
impl Compressor for TarCompressor {
fn compress(&self, from: Vec<crate::file::File>) -> OuchResult<CompressionResult> {
Ok(CompressionResult::TarArchive(
TarCompressor::make_archive_in_memory(from)?
))
}
}
// fn compress(&self, from: Vec<File>, into: &Option<File>) -> OuchResult<CompressionResult>;

View File

@ -1,4 +1,4 @@
use std::convert::TryFrom;
use std::{convert::TryFrom, fs, path::{Path, PathBuf}};
use colored::Colorize;
@ -13,24 +13,43 @@ mod utils;
mod compressors;
mod decompressors;
use compressors::{CompressionResult, Compressor, TarCompressor};
use error::OuchResult;
use file::File;
fn main() -> OuchResult<()>{
let print_error = |err| {
println!("{}: {}", "error".red(), err);
// let print_error = |err| {
// println!("{}: {}", "error".red(), err);
// };
// let matches = cli::get_matches();
// match cli::Command::try_from(matches) {
// Ok(command) => {
// match evaluator::Evaluator::evaluate(command) {
// Ok(_) => {},
// Err(err) => print_error(err)
// }
// }
// Err(err) => {
// print_error(err)
// }
// }
let compressor = TarCompressor {};
let file = File {
path: PathBuf::from("target"),
contents: None,
extension: None,
};
let matches = cli::get_matches();
match cli::Command::try_from(matches) {
Ok(command) => {
match evaluator::Evaluator::evaluate(command) {
Ok(_) => {},
Err(err) => print_error(err)
}
}
Err(err) => {
print_error(err)
}
}
let ok = compressor.compress(vec![file])?;
let ok = match ok {
CompressionResult::TarArchive(bytes) => bytes,
_ => unreachable!()
};
fs::write(Path::new("great.tar"), ok)?;
Ok(())
}