mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-05 02:55:31 +00:00
(Small) Zip decompressor progress
This commit is contained in:
parent
837a6a6a57
commit
0a81384dd8
@ -4,3 +4,4 @@ mod zip;
|
||||
|
||||
pub use decompressor::Decompressor;
|
||||
pub use self::tar::TarDecompressor;
|
||||
pub use self::zip::ZipDecompressor;
|
@ -6,7 +6,7 @@ use std::{
|
||||
use colored::Colorize;
|
||||
use tar;
|
||||
|
||||
use crate::error::OuchResult;
|
||||
use crate::{error::OuchResult, utils};
|
||||
use crate::file::File;
|
||||
|
||||
use super::decompressor::Decompressor;
|
||||
@ -15,23 +15,6 @@ pub struct TarDecompressor {}
|
||||
|
||||
impl TarDecompressor {
|
||||
|
||||
fn create_path_if_non_existent(path: &Path) -> OuchResult<()> {
|
||||
if !path.exists() {
|
||||
println!(
|
||||
"{}: attempting to create folder {:?}.",
|
||||
"info".yellow(),
|
||||
&path
|
||||
);
|
||||
std::fs::create_dir_all(path)?;
|
||||
println!(
|
||||
"{}: directory {:#?} created.",
|
||||
"info".yellow(),
|
||||
fs::canonicalize(&path)?
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unpack_files(from: &Path, into: &Path) -> OuchResult<Vec<PathBuf>> {
|
||||
|
||||
let mut files_unpacked = vec![];
|
||||
@ -55,17 +38,9 @@ impl TarDecompressor {
|
||||
|
||||
impl Decompressor for TarDecompressor {
|
||||
fn decompress(&self, from: &File, into: &Option<File>) -> OuchResult<Vec<PathBuf>> {
|
||||
let destination_path = match into {
|
||||
Some(output) => {
|
||||
// Must be None according to the way command-line arg. parsing in Ouch works
|
||||
assert_eq!(output.extension, None);
|
||||
let destination_path = utils::get_destination_path(into);
|
||||
|
||||
Path::new(&output.path)
|
||||
}
|
||||
None => Path::new("."),
|
||||
};
|
||||
|
||||
Self::create_path_if_non_existent(destination_path)?;
|
||||
utils::create_path_if_non_existent(destination_path)?;
|
||||
|
||||
let files_unpacked = Self::unpack_files(&from.path, destination_path)?;
|
||||
|
||||
|
@ -6,15 +6,30 @@ use std::{
|
||||
use colored::Colorize;
|
||||
use zip;
|
||||
|
||||
use crate::error::OuchResult;
|
||||
use crate::{error::{self, OuchResult}, utils};
|
||||
use crate::file::File;
|
||||
|
||||
use super::decompressor::Decompressor;
|
||||
|
||||
pub struct ZipDecompressor {}
|
||||
|
||||
impl ZipDecompressor {
|
||||
fn unpack_files(from: &Path, into: &Path) -> OuchResult<Vec<PathBuf>> {
|
||||
// placeholder return
|
||||
Err(error::Error::IOError)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Decompressor for ZipDecompressor {
|
||||
fn decompress(&self, from: &File, into: &Option<File>) -> OuchResult<Vec<PathBuf>> {
|
||||
todo!()
|
||||
let destination_path = utils::get_destination_path(into);
|
||||
|
||||
utils::create_path_if_non_existent(destination_path)?;
|
||||
|
||||
let files_unpacked = Self::unpack_files(&from.path, destination_path)?;
|
||||
|
||||
// placeholder return
|
||||
Err(error::Error::IOError)
|
||||
}
|
||||
}
|
@ -2,8 +2,9 @@
|
||||
use colored::Colorize;
|
||||
|
||||
use crate::{cli::{Command, CommandKind}, error, extension::CompressionFormat, file::File};
|
||||
use crate::decompressors::TarDecompressor;
|
||||
use crate::decompressors::Decompressor;
|
||||
use crate::decompressors::TarDecompressor;
|
||||
use crate::decompressors::ZipDecompressor;
|
||||
|
||||
pub struct Evaluator {
|
||||
command: Command,
|
||||
@ -29,6 +30,9 @@ impl Evaluator {
|
||||
CompressionFormat::Tar => {
|
||||
Box::new(TarDecompressor{})
|
||||
},
|
||||
// CompressionFormat::Zip => {
|
||||
// Box::new(ZipDecompressor{})
|
||||
// }
|
||||
_ => {
|
||||
todo!()
|
||||
}
|
||||
|
@ -12,6 +12,26 @@ pub struct Extension {
|
||||
pub second_ext: CompressionFormat
|
||||
}
|
||||
|
||||
pub fn get_extension_from_filename(filename: &str) -> Option<(&str, &str)> {
|
||||
let path = Path::new(filename);
|
||||
|
||||
let ext = path
|
||||
.extension()
|
||||
.and_then(OsStr::to_str)?;
|
||||
|
||||
let previous_extension = path
|
||||
.file_stem()
|
||||
.and_then(OsStr::to_str)
|
||||
.and_then(get_extension_from_filename);
|
||||
|
||||
if let Some((_, prev)) = previous_extension {
|
||||
Some((prev, ext))
|
||||
} else {
|
||||
Some(("", ext))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<CompressionFormat> for Extension {
|
||||
fn from(second_ext: CompressionFormat) -> Self {
|
||||
Self {
|
||||
@ -67,25 +87,6 @@ impl Extension {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_extension_from_filename(filename: &str) -> Option<(&str, &str)> {
|
||||
let path = Path::new(filename);
|
||||
|
||||
let ext = path
|
||||
.extension()
|
||||
.and_then(OsStr::to_str)?;
|
||||
|
||||
let previous_extension = path
|
||||
.file_stem()
|
||||
.and_then(OsStr::to_str)
|
||||
.and_then(get_extension_from_filename);
|
||||
|
||||
if let Some((_, prev)) = previous_extension {
|
||||
Some((prev, ext))
|
||||
} else {
|
||||
Some(("", ext))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
/// Accepted extensions for input and output
|
||||
pub enum CompressionFormat {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use std::{path::PathBuf, process::Command};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::extension::{CompressionFormat, Extension};
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
use std::{convert::TryFrom, fs::File, path::{Path, PathBuf}};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use colored::Colorize;
|
||||
use error::{Error, OuchResult};
|
||||
use tar::Archive;
|
||||
|
||||
mod cli;
|
||||
mod error;
|
||||
@ -10,9 +8,11 @@ mod extension;
|
||||
mod file;
|
||||
mod test;
|
||||
mod evaluator;
|
||||
|
||||
mod utils;
|
||||
mod decompressors;
|
||||
|
||||
use error::OuchResult;
|
||||
|
||||
fn main() -> OuchResult<()>{
|
||||
let matches = cli::get_matches();
|
||||
match cli::Command::try_from(matches) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
#[cfg(test)]
|
||||
mod cli {
|
||||
|
||||
use crate::{cli::clap_app, extension};
|
||||
use crate::cli::clap_app;
|
||||
use crate::cli::Command;
|
||||
use crate::cli::CommandKind::*;
|
||||
use crate::error::OuchResult;
|
||||
|
33
src/utils.rs
Normal file
33
src/utils.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use colored::Colorize;
|
||||
use crate::{error::OuchResult, file::File};
|
||||
|
||||
pub (crate) fn create_path_if_non_existent(path: &Path) -> OuchResult<()> {
|
||||
if !path.exists() {
|
||||
println!(
|
||||
"{}: attempting to create folder {:?}.",
|
||||
"info".yellow(),
|
||||
&path
|
||||
);
|
||||
std::fs::create_dir_all(path)?;
|
||||
println!(
|
||||
"{}: directory {:#?} created.",
|
||||
"info".yellow(),
|
||||
fs::canonicalize(&path)?
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub (crate) fn get_destination_path(dest: &Option<File>) -> &Path {
|
||||
match dest {
|
||||
Some(output) => {
|
||||
// Must be None according to the way command-line arg. parsing in Ouch works
|
||||
assert_eq!(output.extension, None);
|
||||
|
||||
Path::new(&output.path)
|
||||
}
|
||||
None => Path::new("."),
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user