1
0
Fork 0

Add tar support

This commit is contained in:
Florian RICHER (MrDev023) 2021-07-24 17:06:10 +02:00
parent 0862632600
commit f2bf17fde9
6 changed files with 76 additions and 8 deletions

View 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"))
}
}

View 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(())
}

View file

@ -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()?;
}
}

View file

@ -16,8 +16,7 @@ pub fn install() -> Result<(), String> {
let file = downloader::download_file(&url, &filename)
.ok_or(format!("Failed to download file"))?;
extractor::extract_file(&file, "fvm")
.ok_or(format!("Failed to extract file"))?;
extractor::extract_file(&file, "fvm")?;
println!("{}", url);