8 use tar::{self, Archive};
10 use super::decompressor::{DecompressionResult, Decompressor};
11 use crate::{bytes::Bytes, dialogs::Confirmation, file::File, utils};
14 pub struct TarDecompressor {}
16 impl TarDecompressor {
17 fn unpack_files(from: File, into: &Path, flags: &oof::Flags) -> crate::Result<Vec<PathBuf>> {
19 "{}: attempting to decompress {:?}",
23 let mut files_unpacked = vec![];
24 let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE"));
26 let mut archive: Archive<Box<dyn Read>> = match from.contents_in_memory {
27 Some(bytes) => tar::Archive::new(Box::new(Cursor::new(bytes))),
29 let file = fs::File::open(&from.path)?;
30 tar::Archive::new(Box::new(file))
34 for file in archive.entries()? {
37 let file_path = PathBuf::from(into).join(file.path()?);
39 && !utils::permission_for_overwriting(&file_path, flags, &confirm)?
41 // The user does not want to overwrite the file
45 file.unpack_in(into)?;
48 "{}: {:?} extracted. ({})",
50 into.join(file.path()?),
51 Bytes::new(file.size())
54 let file_path = fs::canonicalize(file_path)?;
55 files_unpacked.push(file_path);
62 impl Decompressor for TarDecompressor {
68 ) -> crate::Result<DecompressionResult> {
69 let destination_path = utils::get_destination_path(into);
71 utils::create_path_if_non_existent(destination_path)?;
73 let files_unpacked = Self::unpack_files(from, destination_path, flags)?;
75 Ok(DecompressionResult::FilesUnpacked(files_unpacked))