Fixes Gzip and Lzma compression
[ouch.git] / src / compressors / tar.rs
blob8900c16c86f81d58debf14c500eee6e9eaec10dd
1 use std::{fs, path::PathBuf};
3 use colored::Colorize;
4 use tar::Builder;
5 use walkdir::WalkDir;
7 use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File};
9 use super::compressor::Entry;
11 pub struct TarCompressor {}
13 impl TarCompressor {
15     // TODO: this function does not seem to be working correctly ;/
16     fn make_archive_from_memory(input: File) -> OuchResult<Vec<u8>> {
17         
18         let _contents = match input.contents_in_memory {
19             Some(bytes) => bytes,
20             None => {
21                 eprintln!("{}: reached TarCompressor::make_archive_from_memory without known content.", "internal error".red());
22                 return Err(Error::InvalidInput);
23             }
24         };
26         println!("todo");
28         Ok(vec![])
29     }
31     fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
32     
33         let buf = Vec::new();
34         let mut b = Builder::new(buf);
35     
36         for filename in input_filenames {
37             // TODO: check if file exists
39             for entry in WalkDir::new(&filename) {
40                 let entry = entry?;
41                 let path = entry.path();
42                 if path.is_dir() {
43                     continue;
44                 }
45                 b.append_file(path, &mut fs::File::open(path)?)?;
46             }
47         }
48         
49         Ok(b.into_inner()?)
50     }
53 impl Compressor for TarCompressor {
54     fn compress(&self, from: Entry) -> OuchResult<Vec<u8>> {
56         match from {
57             Entry::Files(filenames) => {
58                 Ok(
59                     Self::make_archive_from_files(filenames)?
60                 )
61             },
62             Entry::InMemory(file) => {
63                 Ok(
64                     Self::make_archive_from_memory(file)?
65                 )
66             }
67         }        
68     }