added bz2 support
This commit is contained in:
parent
5e152b4a1b
commit
b85719ed83
4 changed files with 37 additions and 6 deletions
|
|
@ -1,10 +1,11 @@
|
|||
/*
|
||||
TODO_2: implement remaining extractor functions and write tests
|
||||
*/
|
||||
use std::{fs::{self, File}, io::{self, ErrorKind}, path::Path,};
|
||||
use std::{fs::{self, File}, io::{self, ErrorKind, Write, Read}, path::Path,};
|
||||
use unrar::Archive;
|
||||
use lzma::reader::LzmaReader;
|
||||
use flate2::read::GzDecoder;
|
||||
use bzip2::read::BzDecoder;
|
||||
pub fn extract_zip(zip_file: &Path) -> io::Result<()> {
|
||||
let file = fs::File::open(zip_file)?;
|
||||
let mut archive = zip::ZipArchive::new(file)?;
|
||||
|
|
@ -136,12 +137,39 @@ pub fn extract_gz(input_path: &Path, output_directory: &Path) -> Result<(), io::
|
|||
Err(err) => Err(io::Error::new(ErrorKind::Other, err.to_string())),
|
||||
}
|
||||
}
|
||||
// pub fn extract_bz2(){}
|
||||
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();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
// pub fn extract_tbz2(){}
|
||||
// pub fn extract_tgz(){}
|
||||
// pub fn extract_txz(){}
|
||||
// pub fn extract_lzma(){}
|
||||
// pub fn extract_z(){}
|
||||
// pub fn extract_7z(){}
|
||||
// pub fn extract_arj(){}
|
||||
// pub fn extract_cab(){}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ use extractors::{
|
|||
extract_rar,
|
||||
extract_tar,
|
||||
extract_xz,
|
||||
// extract_bz2,
|
||||
extract_bz2,
|
||||
// extract_tbz2,
|
||||
// extract_tgz,
|
||||
// extract_txz,
|
||||
|
|
@ -88,13 +88,14 @@ fn run() -> i32 {
|
|||
return 1;
|
||||
}
|
||||
}
|
||||
/*
|
||||
"bz2" => {
|
||||
if let Err(err) = extract_bz2(&fname) {
|
||||
let output_directory = Path::new("output_directory"); // Change this to your desired output directory
|
||||
if let Err(err) = extract_bz2(&fname, &output_directory) {
|
||||
println!("Error extracting BZ2: {}", err);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/*
|
||||
"tbz2" => {
|
||||
if let Err(err) = extract_tbz2(&fname) {
|
||||
println!("Error extracting TBZ2: {}", err);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue