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

34
Cargo.lock generated
View file

@ -38,11 +38,13 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
name = "autoconfig"
version = "0.1.0"
dependencies = [
"flate2",
"reqwest",
"serde",
"serde_derive",
"serde_json",
"structopt",
"tar",
"zip",
]
@ -158,6 +160,18 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "filetime"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d34cfa13a63ae058bfa601fe9e313bbdb3746427c1459185464ce0fcf62e1e8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"winapi",
]
[[package]]
name = "flate2"
version = "1.0.20"
@ -864,6 +878,17 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "tar"
version = "0.4.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d779dc6aeff029314570f666ec83f19df7280bb36ef338442cfa8c604021b80"
dependencies = [
"filetime",
"libc",
"xattr",
]
[[package]]
name = "tempfile"
version = "3.2.0"
@ -1195,6 +1220,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "xattr"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "244c3741f4240ef46274860397c7c74e50eb23624996930e484c16679633a54c"
dependencies = [
"libc",
]
[[package]]
name = "zip"
version = "0.5.13"

View file

@ -11,4 +11,6 @@ reqwest = { version = "0.11.4", features = ["blocking"] }
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
zip = "0.5"
zip = "0.5"
tar = "0.4"
flate2 = "1.0"

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);