refactored extractors

This commit is contained in:
specCon18 2023-09-06 18:32:58 -04:00
parent 6fd3729888
commit b17d487665

View file

@ -6,27 +6,23 @@ use lzma::reader::LzmaReader;
use flate2::read::GzDecoder;
use bzip2::read::BzDecoder;
use unrar::Archive;
pub fn extract_zip(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
let file = fs::File::open(input_path)?;
let mut archive = zip::ZipArchive::new(file)?;
for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
let outpath = match file.enclosed_name() {
Some(path) => path.to_owned(),
None => continue,
};
// Modify the output path to use the specified output_directory
let full_outpath = output_directory.join(&outpath);
{
let comment = file.comment();
if !comment.is_empty() {
println!("File {} comment: {}", i, comment);
}
}
if (*file.name()).ends_with('/') {
fs::create_dir_all(&full_outpath).unwrap();
} else {
@ -38,7 +34,6 @@ pub fn extract_zip(input_path: &Path, output_directory: &Path) -> Result<(), io:
let mut outfile = fs::File::create(&full_outpath).unwrap();
io::copy(&mut file, &mut outfile).unwrap();
}
// Get and Set permissions
#[cfg(unix)]
{
@ -58,9 +53,7 @@ pub fn extract_rar(input_path: &Path, output_directory: &Path) -> Result<(), Box
.open_for_processing()
.unwrap();
while let Some(header) = archive.read_header()? {
// Create the complete output path by combining the output_directory and the filename
let output_path = output_directory.join(&header.entry().filename);
archive = if header.entry().is_file() {
header.extract_to(&output_path)?
} else {
@ -91,67 +84,40 @@ pub fn extract_tar(input_path: &Path, output_directory: &Path) -> Result<(), io:
Ok(())
}
pub fn extract_lzma(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
// Open the input XZ file
let input_file = File::open(input_path)?;
// Create a decompression reader
pub fn extract_lzma(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
let input_file = File::open(input_path)?;
let mut decompressor = LzmaReader::new_decompressor(input_file)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
// Determine the output file path
let output_file_path = output_directory.join(input_path.file_stem().unwrap());
// Create the output file
let mut output_file = File::create(&output_file_path)?;
// Read from the decompressor and write to the output file
io::copy(&mut decompressor, &mut output_file)?;
Ok(())
}
pub fn extract_gz(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
// Open the input GZ file
let input_file = File::open(input_path)?;
// Create a GZ decompression reader
let mut decompressor = GzDecoder::new(input_file);
// Determine the output file path
let output_file_path = output_directory.join(input_path.file_stem().unwrap());
// Create the output file
let mut output_file = File::create(&output_file_path)?;
// Read from the decompressor and write to the output file
match io::copy(&mut decompressor, &mut output_file) {
Ok(_) => Ok(()),
Err(err) => Err(io::Error::new(ErrorKind::Other, err.to_string())),
}
}
pub fn extract_bz2(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
// Open the input BZ2 file
let input_file = File::open(input_path)?;
// Create a BZ2 decompression reader
let bz2_reader = BzDecoder::new(input_file);
// Determine the output file path
let output_file_path = output_directory.join(input_path.file_stem().unwrap());
// Create the output file
let mut output_file = File::create(&output_file_path)?;
// Read from the decompressor and write to the output file
let mut buffer = Vec::new();
let mut decompressor = io::BufReader::new(bz2_reader);
loop {
let bytes_read = decompressor.read_to_end(&mut buffer)?;
if bytes_read == 0 {
break; // End of file
}
output_file.write_all(&buffer[..bytes_read])?;
buffer.clear();
}
@ -159,26 +125,17 @@ pub fn extract_bz2(input_path: &Path, output_directory: &Path) -> Result<(), io:
Ok(())
}
pub fn extract_7z(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
sevenz_rust::decompress_file(input_path, output_directory).expect("complete");
Ok(())
}
pub fn extract_tbz2(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
// Open the input TBZ2 file
let input_file = File::open(input_path)?;
// Create a BZ2 decompression reader
let bz2_reader = BzDecoder::new(input_file);
// Create a Tar archive reader
let mut archive = tar::Archive::new(bz2_reader);
for entry in archive.entries()? {
let mut entry = entry?;
let entry_path = entry.path()?;
let full_path = output_directory.join(entry_path);
if entry.header().entry_type().is_dir() {
fs::create_dir_all(&full_path)?;
} else {
@ -188,64 +145,45 @@ pub fn extract_tbz2(input_path: &Path, output_directory: &Path) -> Result<(), io
io::copy(&mut entry, &mut file)?;
}
}
Ok(())
}
pub fn extract_tgz(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
// Open the input TGZ file
let input_file = File::open(input_path)?;
// Create a GZ decompression reader
let mut decompressor = GzDecoder::new(input_file);
// Create a Tar archive reader
let mut archive = tar::Archive::new(&mut decompressor);
for entry in archive.entries()? {
let mut entry = entry?;
let entry_path = entry.path()?;
let full_path = output_directory.join(entry_path);
if entry.header().entry_type().is_dir() {
fs::create_dir_all(&full_path)?;
} else {
fs::create_dir_all(&full_path.parent().unwrap())?;
let mut file = fs::File::create(&full_path)?;
io::copy(&mut entry, &mut file)?;
}
}
Ok(())
}
pub fn extract_txz(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
// Open the input TXZ file
let input_file = File::open(input_path)?;
// Create an XZ decompression reader
let mut decompressor = LzmaReader::new_decompressor(input_file)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
// Create a Tar archive reader
let mut archive = tar::Archive::new(&mut decompressor);
for entry in archive.entries()? {
let mut entry = entry?;
let entry_path = entry.path()?;
let full_path = output_directory.join(entry_path);
if entry.header().entry_type().is_dir() {
fs::create_dir_all(&full_path)?;
} else {
fs::create_dir_all(&full_path.parent().unwrap())?;
let mut file = fs::File::create(&full_path)?;
io::copy(&mut entry, &mut file)?;
}
}
Ok(())
}