diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-02-15 21:43:43 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-02-15 21:43:43 +0100 |
commit | 13d6b9ac1ca17315c975e7a6d10ace21f879ceb4 (patch) | |
tree | c410305c921a5fb24de9ed5e305264e73cd8b802 /src | |
parent | 08fd2bd54802c803e20edcdbde78d6b53b5af541 (diff) |
decompress zip with same crate. Clippy
Diffstat (limited to 'src')
-rw-r--r-- | src/compress.rs | 25 | ||||
-rw-r--r-- | src/decompress.rs | 22 | ||||
-rw-r--r-- | src/event_exec.rs | 4 | ||||
-rw-r--r-- | src/fm_error.rs | 7 | ||||
-rw-r--r-- | src/status.rs | 2 |
5 files changed, 29 insertions, 31 deletions
diff --git a/src/compress.rs b/src/compress.rs index 73609d8..c7879bc 100644 --- a/src/compress.rs +++ b/src/compress.rs @@ -26,9 +26,8 @@ pub struct Compresser { pub index: usize, } -impl Compresser { - /// Creates a new compresser. - pub fn new() -> Self { +impl Default for Compresser { + fn default() -> Self { Self { content: vec![ CompressionMethod::ZIP, @@ -40,7 +39,9 @@ impl Compresser { index: 0, } } +} +impl Compresser { /// Archive the files with tar and compress them with the selected method. /// The compression method is chosen by the user. pub fn compress(&self, files: Vec<std::path::PathBuf>) -> FmResult<()> { @@ -63,9 +64,9 @@ impl Compresser { let mut archive = Builder::new(&mut encoder); for file in files.iter() { if file.is_dir() { - archive.append_dir_all(&file, &file)?; + archive.append_dir_all(file, file)?; } else { - archive.append_path(&file)?; + archive.append_path(file)?; } } } @@ -85,9 +86,9 @@ impl Compresser { let mut archive = Builder::new(&mut encoder); for file in files.iter() { if file.is_dir() { - archive.append_dir_all(&file, &file)?; + archive.append_dir_all(file, file)?; } else { - archive.append_path(&file)?; + archive.append_path(file)?; } } } @@ -107,9 +108,9 @@ impl Compresser { let mut archive = Builder::new(&mut encoder); for file in files.iter() { if file.is_dir() { - archive.append_dir_all(&file, &file)?; + archive.append_dir_all(file, file)?; } else { - archive.append_path(&file)?; + archive.append_path(file)?; } } } @@ -121,7 +122,7 @@ impl Compresser { } fn compress_zip(archive_name: &str, files: Vec<std::path::PathBuf>) -> FmResult<()> { - let archive = std::fs::File::create(&archive_name).unwrap(); + let archive = std::fs::File::create(archive_name).unwrap(); let mut zip = zip::ZipWriter::new(archive); for file in files.iter() { zip.start_file( @@ -147,9 +148,9 @@ impl Compresser { let mut archive = Builder::new(&mut encoder); for file in files.iter() { if file.is_dir() { - archive.append_dir_all(&file, &file)?; + archive.append_dir_all(file, file)?; } else { - archive.append_path(&file)?; + archive.append_path(file)?; } } } diff --git a/src/decompress.rs b/src/decompress.rs index 7ba8559..007edbc 100644 --- a/src/decompress.rs +++ b/src/decompress.rs @@ -1,8 +1,6 @@ use std::fs::File; use std::path::Path; -use compress_tools::*; - use crate::fm_error::{FmError, FmResult}; /// Decompress a compressed file into its parent directory. @@ -10,20 +8,28 @@ use crate::fm_error::{FmError, FmResult}; /// which should be impossible. /// It used `compress_tools` which is a wrapper around `libarchive`. pub fn decompress(source: &Path) -> FmResult<()> { + let file = File::open(source)?; + let mut zip = zip::ZipArchive::new(file)?; + let parent = source .parent() .ok_or_else(|| FmError::custom("decompress", "source should have a parent"))?; - let file = File::open(source)?; - Ok(uncompress_archive(&file, parent, Ownership::Preserve)?) + zip.extract(parent)?; + + Ok(()) } -/// Returns a list of compressed files within the archive. -/// it may fail if the file can't be opened or if libarchive -/// can't read it. pub fn list_files<P>(source: P) -> FmResult<Vec<String>> where P: AsRef<Path>, { let file = File::open(source)?; - Ok(list_archive_files(file)?) + let mut content = vec![]; + let mut zip = zip::ZipArchive::new(file)?; + + for i in 0..zip.len() { + let file = zip.by_index(i)?; + content.push(file.name().to_owned()); + } + Ok(content) } diff --git a/src/event_exec.rs b/src/event_exec.rs index 83a3360..7aaeece 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -1744,9 +1744,7 @@ impl EventExec { .flagged .content .iter() - .map(|abs_path| pathdiff::diff_paths(abs_path, &cwd)) - .filter(|rel_path| rel_path.is_some()) - .map(|rel_path| rel_path.unwrap()) + .filter_map(|abs_path| pathdiff::diff_paths(abs_path, &cwd)) .collect(); status.compression.compress(files_with_relative_paths) } diff --git a/src/fm_error.rs b/src/fm_error.rs index 3d5817d..89e5359 100644 --- a/src/fm_error.rs +++ b/src/fm_error.rs @@ -25,7 +25,6 @@ pub enum ErrorVariant { NOTIFY, FMT, STRUM, - COMPRESSTOOLS, IMAGEERROR, SERDEYAML, CHRONO, @@ -153,12 +152,6 @@ impl From<strum::ParseError> for FmError { } } -impl From<compress_tools::Error> for FmError { - fn from(error: compress_tools::Error) -> Self { - Self::new(ErrorVariant::COMPRESSTOOLS, &error.to_string()) - } -} - impl From<image::error::ImageError> for FmError { fn from(error: image::error::ImageError) -> Self { Self::new(ErrorVariant::IMAGEERROR, &error.to_string()) diff --git a/src/status.rs b/src/status.rs index bf7edf0..a1562f8 100644 --- a/src/status.rs +++ b/src/status.rs @@ -92,7 +92,7 @@ impl Status { tab2.shortcut .extend_with_mount_points(&Self::disks_mounts(sys.disks())); let trash = Trash::new()?; - let compression = Compresser::new(); + let compression = Compresser::default(); Ok(Self { tabs: [tab2, tab], |