Skip to content

Commit

Permalink
feat: Update example to allow extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Waelwindows committed Nov 20, 2019
1 parent aebb167 commit 9aaa34d
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion examples/farc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,34 @@ enum Opt {
}

use std::fs::File;
use std::io::Read;
use std::io::{self, Read};

fn main() {
let opts = Opt::from_args();

match opts {
Opt::Extract { path, root } => {
let path = Path::new(&path);
let mut file = File::open(&path).expect("Failed to open file");
let mut input = vec![];
file.read_to_end(&mut input).unwrap();

let (_, farc) = GenericArchive::read(&input).expect("Failed to parse archive");
let root_dir = path.parent().unwrap();
let root_dir = if root { root_dir.to_owned() } else { root_dir.join(path.file_stem().unwrap()) };
std::fs::create_dir(&root_dir);
match farc {
GenericArchive::Base(a) => extract(&root_dir, &a.entries),
GenericArchive::Compress(a) => extract(&root_dir, &a.entries),
GenericArchive::Extended(a) => {
match a {
ExtendedArchives::Base(a) => extract(&root_dir, &a.0.entries),
ExtendedArchives::Compress(a) => extract(&root_dir, &a.0.entries),
_ => unimplemented!("Extracting encrypted archives is not yet supported")
}
}
};
},
Opt::View { path } => {
let path = Path::new(&path);
let mut file = File::open(&path).expect("Failed to open file");
Expand All @@ -50,3 +72,11 @@ fn main() {
_ => ()
};
}

fn extract<'a, E: EntryExtract<'a>>(root_dir: &Path, entries: &'a [E]) -> Result<(), Box<dyn std::error::Error>> {
for entry in entries {
let mut file = File::create(root_dir.join(&entry.name()))?;
io::copy(&mut entry.extractor(), &mut file)?;
}
Ok(())
}

0 comments on commit 9aaa34d

Please sign in to comment.