impl except test

This commit is contained in:
misilelab 2023-05-11 20:28:05 +09:00 committed by João Marcos
parent d0d227b234
commit db0bc8a7d9
6 changed files with 64 additions and 31 deletions

View File

@ -1,5 +1,6 @@
//! Archive compression algorithms
pub mod rar;
pub mod sevenz;
pub mod tar;
pub mod zip;

40
src/archive/sevenz.rs Normal file
View File

@ -0,0 +1,40 @@
//! SevenZip archive format compress function
use std::{
env::current_dir,
path::{Path, PathBuf},
};
pub fn compress_sevenz(files: Vec<PathBuf>, output_path: &Path) -> crate::Result<bool> {
let mut writer = sevenz_rust::SevenZWriter::create(output_path).map_err(crate::Error::SevenzipError)?;
for filep in files.iter() {
writer
.push_archive_entry::<std::fs::File>(
sevenz_rust::SevenZWriter::<std::fs::File>::create_archive_entry(
filep,
filep
.strip_prefix(current_dir()?)
.expect("StripPrefix Failed")
.as_os_str()
.to_str()
.unwrap()
.to_string(),
),
None,
)
.map_err(crate::Error::SevenzipError)?;
}
writer.finish()?;
Ok(true)
}
pub fn decompress_sevenz(input_file_path: &Path, output_path: &Path) -> crate::Result<usize> {
let mut count: usize = 0;
sevenz_rust::decompress_file_with_extract_fn(input_file_path, output_path, |entry, reader, dest| {
count += 1;
sevenz_rust::default_entry_extract_fn(entry, reader, dest)
})
.map_err(crate::Error::SevenzipError)?;
Ok(count)
}

View File

@ -1,5 +1,4 @@
use std::{
env::current_dir,
io::{self, BufWriter, Cursor, Seek, Write},
path::{Path, PathBuf},
};
@ -128,28 +127,7 @@ pub fn compress_files(
return Ok(false);
},
SevenZip => {
let mut writer =
sevenz_rust::SevenZWriter::create(output_path).map_err(|e| crate::Error::SevenzipError(e))?;
for filep in files.iter() {
writer
.push_archive_entry::<std::fs::File>(
sevenz_rust::SevenZWriter::<std::fs::File>::create_archive_entry(
filep,
filep
.strip_prefix(current_dir()?)
.expect("StripPrefix Failed")
.as_os_str()
.to_str()
.unwrap()
.to_string(),
),
None,
)
.map_err(|e| crate::Error::SevenzipError(e))?;
}
writer.finish()?;
archive::sevenz::compress_sevenz(files, output_path)?;
}
}

View File

@ -165,14 +165,16 @@ pub fn decompress_file(
}
},
SevenZip => {
let mut count = 0;
sevenz_rust::decompress_file_with_extract_fn(input_file_path, output_dir,
|entry, reader, dest| {
count += 1;
sevenz_rust::default_entry_extract_fn(entry, reader, dest)
if let ControlFlow::Continue(files) = smart_unpack(
|output_dir| crate::archive::sevenz::decompress_sevenz(input_file_path, output_dir),
output_dir,
&output_file_path,
question_policy,
)? {
files
} else {
return Ok(());
}
).map_err(|x| crate::Error::SevenzipError(x))?;
count
}
};

View File

@ -88,6 +88,13 @@ pub fn list_archive_contents(
}
},
SevenZip => {
if formats.len() > 1 {
warn_user_about_loading_zip_in_memory();
if !user_wants_to_continue(archive_path, question_policy, QuestionAction::Decompression)? {
return Ok(());
}
}
let mut a = Vec::new();
sevenz_rust::decompress_file_with_extract_fn(archive_path, ".", |entry, _, _| {
@ -97,7 +104,7 @@ pub fn list_archive_contents(
}));
Ok(true)
})
.map_err(|e| crate::Error::SevenzipError(e))?;
.map_err(crate::Error::SevenzipError)?;
Box::new(a.into_iter())
}
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {

View File

@ -91,6 +91,9 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
&& buf.starts_with(&[0x52, 0x61, 0x72, 0x21, 0x1A, 0x07])
&& (buf[6] == 0x00 || (buf.len() >= 8 && buf[6..=7] == [0x01, 0x00]))
}
fn is_sevenz(buf: &[u8]) -> bool {
buf.starts_with(&[0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])
}
let buf = {
let mut buf = [0; 270];
@ -124,6 +127,8 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
Some(Extension::new(&[Zstd], "zst"))
} else if is_rar(&buf) {
Some(Extension::new(&[Rar], "rar"))
} else if is_sevenz(&buf) {
Some(Extension::new(&[SevenZip], "7z"))
} else {
None
}