mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 19:45:29 +00:00
Tar compression seemingly working
This commit is contained in:
parent
2c0f2b380c
commit
d72ca9eeae
@ -14,5 +14,5 @@ pub enum Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait Compressor {
|
pub trait Compressor {
|
||||||
fn compress(&self, from: Vec<PathBuf>) -> OuchResult<Vec<u8>>;
|
fn compress(&self, from: Entry) -> OuchResult<Vec<u8>>;
|
||||||
}
|
}
|
@ -3,3 +3,4 @@ mod compressor;
|
|||||||
|
|
||||||
pub use compressor::{Compressor};
|
pub use compressor::{Compressor};
|
||||||
pub use self::tar::TarCompressor;
|
pub use self::tar::TarCompressor;
|
||||||
|
pub use self::compressor::Entry;
|
@ -4,7 +4,9 @@ use colored::Colorize;
|
|||||||
use tar::{Builder, Header};
|
use tar::{Builder, Header};
|
||||||
use walkdir::WalkDir;
|
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 {}
|
pub struct TarCompressor {}
|
||||||
|
|
||||||
@ -33,19 +35,19 @@ impl TarCompressor {
|
|||||||
Ok(b.into_inner()?)
|
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 buf = Vec::new();
|
||||||
let mut b = Builder::new(buf);
|
let mut b = Builder::new(buf);
|
||||||
|
|
||||||
for file in input_files {
|
for filename in input_filenames {
|
||||||
for entry in WalkDir::new(&file) {
|
for entry in WalkDir::new(&filename) {
|
||||||
let entry = entry.unwrap();
|
let entry = entry?;
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
continue;
|
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 {
|
impl Compressor for TarCompressor {
|
||||||
fn compress(&self, from: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
|
fn compress(&self, from: Entry) -> OuchResult<Vec<u8>> {
|
||||||
|
|
||||||
|
match from {
|
||||||
|
Entry::Files(filenames) => {
|
||||||
Ok(
|
Ok(
|
||||||
TarCompressor::make_archive_from_files(from)?
|
Self::make_archive_from_files(filenames)?
|
||||||
|
)
|
||||||
|
},
|
||||||
|
Entry::InMemory(file) => {
|
||||||
|
Ok(
|
||||||
|
Self::make_archive_from_memory(file)?
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -87,3 +87,11 @@ impl From<niffler::error::Error> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<walkdir::Error> for Error {
|
||||||
|
fn from(err: walkdir::Error) -> Self {
|
||||||
|
eprintln!("{}: {}", "error".red(), err);
|
||||||
|
|
||||||
|
Self::InvalidInput
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@ use std::{ffi::OsStr, fs, io::Write, path::PathBuf};
|
|||||||
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
|
||||||
use crate::{compressors::TarCompressor, decompressors::TarDecompressor};
|
use crate::{compressors::{Entry, TarCompressor}, decompressors::TarDecompressor};
|
||||||
use crate::decompressors::ZipDecompressor;
|
use crate::decompressors::ZipDecompressor;
|
||||||
use crate::{
|
use crate::{
|
||||||
cli::{Command, CommandKind},
|
cli::{Command, CommandKind},
|
||||||
@ -155,8 +155,34 @@ impl Evaluator {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compress_files(files: Vec<PathBuf>, output: File) -> error::OuchResult<()> {
|
fn compress_files(files: Vec<PathBuf>, mut output: File) -> error::OuchResult<()> {
|
||||||
let (first_decompressor, second_decompressor) = Self::get_compressor(&output)?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
src/main.rs
17
src/main.rs
@ -30,22 +30,5 @@ fn main() -> error::OuchResult<()>{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user