added tgz tbz2 txz tar.xz tar.bz2 tar.gz support

This commit is contained in:
specCon18 2023-09-03 21:58:25 -04:00
parent 6bd66a07e2
commit e498cb12b0
2 changed files with 142 additions and 24 deletions

View file

@ -172,9 +172,91 @@ pub fn extract_7z(input_path: &Path, output_directory: &Path) -> Result<(), io::
Ok(()) Ok(())
} }
// pub fn extract_tbz2(){} pub fn extract_tbz2(input_path: &Path, output_directory: &Path) -> Result<(), io::Error> {
// pub fn extract_tgz(){} // Open the input TBZ2 file
// pub fn extract_txz(){} 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 {
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_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(())
}
// pub fn extract_arj(){} // pub fn extract_arj(){}
// pub fn extract_cab(){} // pub fn extract_cab(){}
// pub fn extract_chm(){} // pub fn extract_chm(){}

View file

@ -19,10 +19,9 @@ use extractors::{
extract_tar, extract_tar,
extract_lzma, extract_lzma,
extract_bz2, extract_bz2,
// extract_tbz2, extract_tbz2,
// extract_tgz, extract_tgz,
// extract_txz, extract_txz,
// extract_lzma,
extract_gz, extract_gz,
// extract_z, // extract_z,
extract_7z, extract_7z,
@ -72,6 +71,12 @@ fn run() -> i32 {
} }
"xz" => { "xz" => {
let output_directory = Path::new("output_directory"); // Change this to your desired output directory let output_directory = Path::new("output_directory"); // Change this to your desired output directory
if fname.to_str().unwrap().ends_with(".tar.xz") {
if let Err(err) = extract_txz(&fname,&output_directory) {
println!("Error extracting TXZ: {}", err);
return 1;
}
} else {
if let Err(err) = fs::create_dir_all(&output_directory) { if let Err(err) = fs::create_dir_all(&output_directory) {
println!("Error creating output directory: {}", err); println!("Error creating output directory: {}", err);
return 1; return 1;
@ -81,28 +86,43 @@ fn run() -> i32 {
return 1; return 1;
} }
} }
}
"gz" => { "gz" => {
let output_directory = Path::new("output_directory"); // Change this to your desired output directory let output_directory = Path::new("output_directory"); // Change this to your desired output directory
if let Err(err) = fs::create_dir_all(&output_directory) { if let Err(err) = fs::create_dir_all(&output_directory) {
println!("Error creating output directory: {}", err); println!("Error creating output directory: {}", err);
return 1; return 1;
} }
if fname.to_str().unwrap().ends_with(".tar.gz") {
if let Err(err) = extract_tgz(&fname,&output_directory) {
println!("Error extracting TGZ: {}", err);
return 1;
}
} else {
if let Err(err) = extract_gz(&fname, &output_directory) { if let Err(err) = extract_gz(&fname, &output_directory) {
println!("Error extracting GZ: {}", err); println!("Error extracting GZ: {}", err);
return 1; return 1;
} }
} }
}
"bz2" => { "bz2" => {
let output_directory = Path::new("output_directory"); // Change this to your desired output directory let output_directory = Path::new("output_directory"); // Change this to your desired output directory
if let Err(err) = fs::create_dir_all(&output_directory) { if let Err(err) = fs::create_dir_all(&output_directory) {
println!("Error creating output directory: {}", err); println!("Error creating output directory: {}", err);
return 1; return 1;
} }
if fname.to_str().unwrap().ends_with(".tar.bz2") {
if let Err(err) = extract_tbz2(&fname,&output_directory) {
println!("Error extracting TBZ2: {}", err);
return 1;
}
} else {
if let Err(err) = extract_bz2(&fname, &output_directory) { if let Err(err) = extract_bz2(&fname, &output_directory) {
println!("Error extracting BZ2: {}", err); println!("Error extracting BZ2: {}", err);
return 1; return 1;
} }
} }
}
"lzma" => { "lzma" => {
let output_directory = Path::new("output_directory"); // Change this to your desired output directory let output_directory = Path::new("output_directory"); // Change this to your desired output directory
if let Err(err) = fs::create_dir_all(&output_directory) { if let Err(err) = fs::create_dir_all(&output_directory) {
@ -125,25 +145,41 @@ fn run() -> i32 {
return 1; return 1;
} }
} }
/*
"tbz2" => { "tbz2" => {
if let Err(err) = extract_tbz2(&fname) { let output_directory = Path::new("output_directory"); // Change this to your desired output directory
if let Err(err) = fs::create_dir_all(&output_directory) {
println!("Error creating output directory: {}", err);
return 1;
}
if let Err(err) = extract_tbz2(&fname,&output_directory) {
println!("Error extracting TBZ2: {}", err); println!("Error extracting TBZ2: {}", err);
return 1; return 1;
} }
} }
"txz" => { "txz" => {
if let Err(err) = extract_txz(&fname) { let output_directory = Path::new("output_directory"); // Change this to your desired output directory
if let Err(err) = fs::create_dir_all(&output_directory) {
println!("Error creating output directory: {}", err);
return 1;
}
if let Err(err) = extract_txz(&fname,&output_directory) {
println!("Error extracting TXZ: {}", err); println!("Error extracting TXZ: {}", err);
return 1; return 1;
} }
} }
"tgz" => { "tgz" => {
if let Err(err) = extract_tgz(&fname) { let output_directory = Path::new("output_directory"); // Change this to your desired output directory
if let Err(err) = fs::create_dir_all(&output_directory) {
println!("Error creating output directory: {}", err);
return 1;
}
if let Err(err) = extract_tgz(&fname,&output_directory) {
println!("Error extracting TGZ: {}", err); println!("Error extracting TGZ: {}", err);
return 1; return 1;
} }
} }
/*
"arj" => { "arj" => {
if let Err(err) = extract_arj(&fname) { if let Err(err) = extract_arj(&fname) {
println!("Error extracting ARJ: {}", err); println!("Error extracting ARJ: {}", err);