mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
decompressors.tar: now working!
This commit is contained in:
parent
b6d4e50cca
commit
d351a8ef7b
@ -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(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
41
src/main.rs
41
src/main.rs
@ -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(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user