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 | |
parent | 08fd2bd54802c803e20edcdbde78d6b53b5af541 (diff) |
decompress zip with same crate. Clippy
-rw-r--r-- | Cargo.lock | 30 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | development.md | 1 | ||||
-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 |
8 files changed, 30 insertions, 62 deletions
@@ -402,18 +402,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" [[package]] -name = "compress-tools" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a3b1511783270e75d95749c851008a01fba662a8136e488d29d2b3a51a56e08" -dependencies = [ - "derive_more", - "libc", - "pkg-config", - "vcpkg", -] - -[[package]] name = "concurrent-queue" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -703,17 +691,6 @@ dependencies = [ ] [[package]] -name = "derive_more" -version = "0.99.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] name = "destructure_traitobject" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1009,7 +986,6 @@ version = "0.1.17" dependencies = [ "chrono", "clap 4.0.32", - "compress-tools", "content_inspector", "copypasta", "flate2", @@ -3057,12 +3033,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" [[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - -[[package]] name = "vec_map" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -31,7 +31,6 @@ fs_extra = "1.2.0" [dependencies] chrono = "0.4.22" clap = { version = "4.0.2", features = ["derive"] } -compress-tools = "0.14.0" content_inspector = "0.2.4" copypasta = "0.8.1" flate2 = "1.0" diff --git a/development.md b/development.md index e6c3e0b..27373be 100644 --- a/development.md +++ b/development.md @@ -381,6 +381,7 @@ New view: Tree ! Toggle with 't', fold with 'z'. Navigate normally. - [x] tree: use the length of the screen to avoid parsing non displayed lines - [x] navigate in marks - [x] compress flagged files: method is chosen from a list of compression method. + - [ ] decompress any archive, not only zips... - [ ] Version 0.1.50 : safety & memory usage 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], |