mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
impl except test
This commit is contained in:
parent
d0d227b234
commit
db0bc8a7d9
@ -1,5 +1,6 @@
|
|||||||
//! Archive compression algorithms
|
//! Archive compression algorithms
|
||||||
|
|
||||||
pub mod rar;
|
pub mod rar;
|
||||||
|
pub mod sevenz;
|
||||||
pub mod tar;
|
pub mod tar;
|
||||||
pub mod zip;
|
pub mod zip;
|
||||||
|
40
src/archive/sevenz.rs
Normal file
40
src/archive/sevenz.rs
Normal 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)
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
use std::{
|
use std::{
|
||||||
env::current_dir,
|
|
||||||
io::{self, BufWriter, Cursor, Seek, Write},
|
io::{self, BufWriter, Cursor, Seek, Write},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
@ -128,28 +127,7 @@ pub fn compress_files(
|
|||||||
return Ok(false);
|
return Ok(false);
|
||||||
},
|
},
|
||||||
SevenZip => {
|
SevenZip => {
|
||||||
let mut writer =
|
archive::sevenz::compress_sevenz(files, output_path)?;
|
||||||
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()?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,14 +165,16 @@ pub fn decompress_file(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
SevenZip => {
|
SevenZip => {
|
||||||
let mut count = 0;
|
if let ControlFlow::Continue(files) = smart_unpack(
|
||||||
sevenz_rust::decompress_file_with_extract_fn(input_file_path, output_dir,
|
|output_dir| crate::archive::sevenz::decompress_sevenz(input_file_path, output_dir),
|
||||||
|entry, reader, dest| {
|
output_dir,
|
||||||
count += 1;
|
&output_file_path,
|
||||||
sevenz_rust::default_entry_extract_fn(entry, reader, dest)
|
question_policy,
|
||||||
|
)? {
|
||||||
|
files
|
||||||
|
} else {
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
).map_err(|x| crate::Error::SevenzipError(x))?;
|
|
||||||
count
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -88,6 +88,13 @@ pub fn list_archive_contents(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
SevenZip => {
|
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();
|
let mut a = Vec::new();
|
||||||
|
|
||||||
sevenz_rust::decompress_file_with_extract_fn(archive_path, ".", |entry, _, _| {
|
sevenz_rust::decompress_file_with_extract_fn(archive_path, ".", |entry, _, _| {
|
||||||
@ -97,7 +104,7 @@ pub fn list_archive_contents(
|
|||||||
}));
|
}));
|
||||||
Ok(true)
|
Ok(true)
|
||||||
})
|
})
|
||||||
.map_err(|e| crate::Error::SevenzipError(e))?;
|
.map_err(crate::Error::SevenzipError)?;
|
||||||
Box::new(a.into_iter())
|
Box::new(a.into_iter())
|
||||||
}
|
}
|
||||||
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {
|
Gzip | Bzip | Lz4 | Lzma | Snappy | Zstd => {
|
||||||
|
@ -91,6 +91,9 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
|
|||||||
&& buf.starts_with(&[0x52, 0x61, 0x72, 0x21, 0x1A, 0x07])
|
&& buf.starts_with(&[0x52, 0x61, 0x72, 0x21, 0x1A, 0x07])
|
||||||
&& (buf[6] == 0x00 || (buf.len() >= 8 && buf[6..=7] == [0x01, 0x00]))
|
&& (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 buf = {
|
||||||
let mut buf = [0; 270];
|
let mut buf = [0; 270];
|
||||||
@ -124,6 +127,8 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
|
|||||||
Some(Extension::new(&[Zstd], "zst"))
|
Some(Extension::new(&[Zstd], "zst"))
|
||||||
} else if is_rar(&buf) {
|
} else if is_rar(&buf) {
|
||||||
Some(Extension::new(&[Rar], "rar"))
|
Some(Extension::new(&[Rar], "rar"))
|
||||||
|
} else if is_sevenz(&buf) {
|
||||||
|
Some(Extension::new(&[SevenZip], "7z"))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user