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