1 use std::fs;
2 use std::io;
3
main()4 fn main() {
5 std::process::exit(real_main());
6 }
7
real_main() -> i328 fn real_main() -> i32 {
9 let args: Vec<_> = std::env::args().collect();
10 if args.len() < 2 {
11 println!("Usage: {} <filename>", args[0]);
12 return 1;
13 }
14 let fname = std::path::Path::new(&*args[1]);
15 let file = fs::File::open(fname).unwrap();
16
17 let mut archive = zip::ZipArchive::new(file).unwrap();
18
19 for i in 0..archive.len() {
20 let mut file = archive.by_index(i).unwrap();
21 let outpath = match file.enclosed_name() {
22 Some(path) => path.to_owned(),
23 None => continue,
24 };
25
26 {
27 let comment = file.comment();
28 if !comment.is_empty() {
29 println!("File {i} comment: {comment}");
30 }
31 }
32
33 if (*file.name()).ends_with('/') {
34 println!("File {} extracted to \"{}\"", i, outpath.display());
35 fs::create_dir_all(&outpath).unwrap();
36 } else {
37 println!(
38 "File {} extracted to \"{}\" ({} bytes)",
39 i,
40 outpath.display(),
41 file.size()
42 );
43 if let Some(p) = outpath.parent() {
44 if !p.exists() {
45 fs::create_dir_all(p).unwrap();
46 }
47 }
48 let mut outfile = fs::File::create(&outpath).unwrap();
49 io::copy(&mut file, &mut outfile).unwrap();
50 }
51
52 // Get and Set permissions
53 #[cfg(unix)]
54 {
55 use std::os::unix::fs::PermissionsExt;
56
57 if let Some(mode) = file.unix_mode() {
58 fs::set_permissions(&outpath, fs::Permissions::from_mode(mode)).unwrap();
59 }
60 }
61 }
62
63 0
64 }
65