Add tar support
This commit is contained in:
parent
0862632600
commit
f2bf17fde9
6 changed files with 76 additions and 8 deletions
18
src/common/utils/extractor/mod.rs
Normal file
18
src/common/utils/extractor/mod.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use std::path::{PathBuf};
|
||||
|
||||
mod zip;
|
||||
mod tar;
|
||||
|
||||
pub fn extract_file(path: &PathBuf, outdir: &str) -> Result<(), String> {
|
||||
let file_extension = path.extension()
|
||||
.ok_or(format!("Failed to get extension"))?
|
||||
.to_str()
|
||||
.ok_or(format!("Failed to convert extension to &str"))?;
|
||||
match file_extension {
|
||||
"zip" => Ok(zip::extract_file(&path, outdir).ok_or(format!("Extract failed"))?),
|
||||
"bz2" => Ok(zip::extract_file(&path, outdir).ok_or(format!("Extract failed"))?),
|
||||
"tar" => Ok(tar::extract_file(&path, outdir).ok_or(format!("Extract failed"))?),
|
||||
"gz" => Ok(tar::extract_file(&path, outdir).ok_or(format!("Extract failed"))?),
|
||||
_ => Err(format!("Format not supported"))
|
||||
}
|
||||
}
|
15
src/common/utils/extractor/tar.rs
Normal file
15
src/common/utils/extractor/tar.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use std::{fs::File, path::{Path, PathBuf}};
|
||||
use flate2::read::GzDecoder;
|
||||
use tar::Archive;
|
||||
use crate::common;
|
||||
|
||||
|
||||
pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> {
|
||||
let file = File::open(path).ok()?;
|
||||
let tar = GzDecoder::new(file);
|
||||
let mut archive = Archive::new(tar);
|
||||
let path = Path::new(common::TEMP_FOLDER).join(outdir);
|
||||
archive.unpack(path).ok()?;
|
||||
|
||||
Some(())
|
||||
}
|
|
@ -5,7 +5,7 @@ use std::{fs::{
|
|||
}, io, path::{self, PathBuf}};
|
||||
|
||||
pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> {
|
||||
let file = File::open(&path).unwrap();
|
||||
let file = File::open(&path).ok()?;
|
||||
let mut archive = match zip::ZipArchive::new(file) {
|
||||
Ok(archive) => archive,
|
||||
Err(err) => {
|
||||
|
@ -23,7 +23,7 @@ pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> {
|
|||
|
||||
if (&*file.name()).ends_with('/') {
|
||||
println!("File {} extracted to \"{}\"", i, output_path.display());
|
||||
create_dir_all(&output_path).unwrap();
|
||||
create_dir_all(&output_path).ok()?;
|
||||
} else {
|
||||
println!(
|
||||
"File {} extracted to \"{}\" ({} bytes)",
|
||||
|
@ -33,11 +33,11 @@ pub fn extract_file(path: &PathBuf, outdir: &str) -> Option<()> {
|
|||
);
|
||||
if let Some(p) = output_path.parent() {
|
||||
if !p.exists() {
|
||||
create_dir_all(&p).unwrap();
|
||||
create_dir_all(&p).ok()?;
|
||||
}
|
||||
}
|
||||
let mut outfile = File::create(&output_path).unwrap();
|
||||
io::copy(&mut file, &mut outfile).unwrap();
|
||||
let mut outfile = File::create(&output_path).ok()?;
|
||||
io::copy(&mut file, &mut outfile).ok()?;
|
||||
}
|
||||
}
|
||||
|
Reference in a new issue