1 use std::{fs, path::PathBuf};
7 use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File};
9 use super::compressor::Entry;
11 pub struct TarCompressor {}
15 // TODO: this function does not seem to be working correctly ;/
16 fn make_archive_from_memory(input: File) -> OuchResult<Vec<u8>> {
18 let _contents = match input.contents_in_memory {
21 eprintln!("{}: reached TarCompressor::make_archive_from_memory without known content.", "internal error".red());
22 return Err(Error::InvalidInput);
31 fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
34 let mut b = Builder::new(buf);
36 for filename in input_filenames {
37 // TODO: check if file exists
39 for entry in WalkDir::new(&filename) {
41 let path = entry.path();
45 b.append_file(path, &mut fs::File::open(path)?)?;
53 impl Compressor for TarCompressor {
54 fn compress(&self, from: Entry) -> OuchResult<Vec<u8>> {
57 Entry::Files(filenames) => {
59 Self::make_archive_from_files(filenames)?
62 Entry::InMemory(file) => {
64 Self::make_archive_from_memory(file)?