-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathcmd_extract.rs
More file actions
65 lines (53 loc) · 2.16 KB
/
cmd_extract.rs
File metadata and controls
65 lines (53 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::reader::parse_events;
use common::event::Event;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fs::File;
use std::path::PathBuf;
pub fn extract(input: PathBuf, output: PathBuf) -> Result<(), std::io::Error> {
info!("Opening {:?}...", input);
let fp = File::open(input)?;
let (_, event_stream) = parse_events(fp)?;
info!("Creating {:?} if it doesn't exist...", output);
std::fs::create_dir_all(&output)?;
let mut counter = HashMap::new();
for event in event_stream {
let event = match event {
Ok(event) => event,
Err(_) => break,
};
match event {
Event::File { path, contents, .. } | Event::File64 { path, contents, .. } => {
let mut relative_path = &*path;
if relative_path.starts_with("/") {
relative_path = &relative_path[1..];
}
let mut target_path = output.join(relative_path);
info!("Extracting {:?} into {:?}...", path, target_path);
if let Some(parent) = target_path.parent() {
std::fs::create_dir_all(parent)?;
}
match counter.entry(target_path.clone()) {
Entry::Vacant(bucket) => {
bucket.insert(0);
}
Entry::Occupied(mut bucket) => {
let parent = target_path.parent().unwrap();
let mut filename = target_path.file_name().unwrap().to_os_string();
if *bucket.get() == 0 {
let mut filename = filename.clone();
filename.push(".000");
std::fs::rename(&target_path, parent.join(filename))?;
}
filename.push(format!(".{:03}", bucket.get()));
target_path = parent.join(filename);
*bucket.get_mut() += 1;
}
}
std::fs::write(target_path, contents)?;
}
_ => {}
}
}
Ok(())
}