1 use std::{env, fs, path::PathBuf};
7 use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File};
9 use super::compressor::Entry;
11 pub struct TarCompressor {}
15 // TODO: implement this
16 fn make_archive_from_memory(_input: File) -> OuchResult<Vec<u8>> {
17 println!("{}: .tar.tar and .zip.tar is currently unimplemented.", "error".red());
18 Err(Error::InvalidZipArchive(""))
21 fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
23 let change_dir_and_return_parent = |filename: &PathBuf| -> OuchResult<PathBuf> {
24 let previous_location = env::current_dir()?;
25 let parent = filename.parent().unwrap();
26 env::set_current_dir(parent)?;
31 let mut b = Builder::new(buf);
33 for filename in input_filenames {
34 let previous_location = change_dir_and_return_parent(&filename)?;
35 // Safe unwrap since this filename came from `fs::canonicalize`.
36 let filename = filename.file_name().unwrap();
37 for entry in WalkDir::new(&filename) {
39 let path = entry.path();
43 b.append_file(path, &mut fs::File::open(path)?)?;
45 env::set_current_dir(previous_location)?;
52 impl Compressor for TarCompressor {
53 fn compress(&self, from: Entry) -> OuchResult<Vec<u8>> {
56 Entry::Files(filenames) => {
58 Self::make_archive_from_files(filenames)?
61 Entry::InMemory(file) => {
63 Self::make_archive_from_memory(file)?