decompressors.tar: now working!

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-21 04:23:00 -03:00
parent b6d4e50cca
commit d351a8ef7b
2 changed files with 43 additions and 40 deletions

View File

@ -1,33 +1,53 @@
use std::{fs, path::{Path, PathBuf}}; use std::{
fs,
use crate::file::File; path::{Path, PathBuf},
use crate::error::OuchResult; };
use colored::Colorize; use colored::Colorize;
use tar;
use crate::error::OuchResult;
use crate::file::File;
pub struct Decompressor {} pub struct Decompressor {}
impl Decompressor { impl Decompressor {
pub fn decompress(from: &File, into: &Option<File>) -> OuchResult<()> { pub fn decompress(from: &File, into: &Option<File>) -> OuchResult<()> {
let destination_path = match into { let destination_path = match into {
Some(output) => { Some(output) => {
// Must be None according to the way command-line arg. parsing in Ouch works // Must be None according to the way command-line arg. parsing in Ouch works
assert_eq!(output.extension, None); assert_eq!(output.extension, None);
Path::new(&output.path) Path::new(&output.path)
} }
None => Path::new(".") None => Path::new("."),
}; };
if !destination_path.exists() { if !destination_path.exists() {
println!("{}: attempting to create folder {:?}.", "info".yellow(), &destination_path); println!(
"{}: attempting to create folder {:?}.",
"info".yellow(),
&destination_path
);
std::fs::create_dir_all(destination_path)?; std::fs::create_dir_all(destination_path)?;
println!("{}: directory {:#?} created.", "info".yellow(), fs::canonicalize(&destination_path)?); println!(
"{}: directory {:#?} created.",
"info".yellow(),
fs::canonicalize(&destination_path)?
);
} }
let file = fs::File::open(&from.path)?;
let mut archive = tar::Archive::new(file);
for file in archive.entries().unwrap() {
let mut file = file?;
file.unpack_in(destination_path)?;
// TODO: save all unpacked files into a 'transaction' metadata
}
Ok(()) Ok(())
} }
} }

View File

@ -1,6 +1,7 @@
use std::{convert::TryFrom, fs::File, path::{Path, PathBuf}}; use std::{convert::TryFrom, fs::File, path::{Path, PathBuf}};
use colored::Colorize; use colored::Colorize;
use error::{Error, OuchResult};
use tar::Archive; use tar::Archive;
mod cli; mod cli;
@ -12,34 +13,16 @@ mod evaluator;
mod decompressors; mod decompressors;
fn main() { fn main() -> OuchResult<()>{
// let matches = cli::get_matches(); let matches = cli::get_matches();
// match cli::Command::try_from(matches) { match cli::Command::try_from(matches) {
// Ok(command) => { Ok(command) => {
// let mut eval = evaluator::Evaluator::new(command); let mut eval = evaluator::Evaluator::new(command);
// eval.evaluate(); eval.evaluate()?;
// } }
// Err(err) => { Err(err) => {
// print!("{}: {}\n", "error".red(), err); print!("{}: {}\n", "error".red(), err);
// } }
// }
// Testing tar unarchival
let file = File::open("ouch.tar").unwrap();
let mut a = Archive::new(file);
for file in a.entries().unwrap() {
// Make sure there wasn't an I/O error
let mut file = file.unwrap();
let path = if let Ok(path) = file.path() {
path
} else {
continue;
};
let name = path.to_string_lossy();
file.unpack(format!("{}", name)).unwrap();
} }
Ok(())
} }