|
@@ -1,7 +1,8 @@
|
|
|
use std::io;
|
|
|
use std::fs;
|
|
|
-use clap::{Arg, App};
|
|
|
use std::path::PathBuf;
|
|
|
+use clap::{Arg, App};
|
|
|
+use rand::seq::SliceRandom;
|
|
|
|
|
|
fn main() {
|
|
|
|
|
@@ -33,8 +34,8 @@ fn main() {
|
|
|
|
|
|
let archive = matches.value_of("ZIP").unwrap();
|
|
|
let filter = matches.value_of("filter");
|
|
|
- let ignorepath = matches.is_present("ignorepath");
|
|
|
- let random = matches.value_of("random");
|
|
|
+ let do_ignorepath = matches.is_present("ignorepath");
|
|
|
+ let do_random = matches.is_present("random");
|
|
|
|
|
|
|
|
|
let archive_path = std::path::Path::new(archive);
|
|
@@ -42,9 +43,23 @@ fn main() {
|
|
|
|
|
|
let mut zip_archive = zip::ZipArchive::new(zipfile).unwrap();
|
|
|
|
|
|
+ let mut indices = (0..zip_archive.len()).collect::<Vec<_>>().into_iter().filter(|x| {
|
|
|
+ //if a filter flag is passed
|
|
|
+ if let Some(f) = filter {
|
|
|
+ let file = zip_archive.by_index(*x).unwrap();
|
|
|
+ return file.name().contains(f)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }).collect::<Vec<_>>();
|
|
|
+
|
|
|
+ if do_random {
|
|
|
+ indices = vec![indices.choose(&mut rand::thread_rng()).unwrap_or(&0).clone()];
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- for i in 0..zip_archive.len() {
|
|
|
+ // for i in 0..zip_archive.len() {
|
|
|
+ for i in indices {
|
|
|
let mut file = zip_archive.by_index(i).unwrap();
|
|
|
let mut outpath = file.sanitized_name();
|
|
|
|
|
@@ -56,7 +71,7 @@ fn main() {
|
|
|
}
|
|
|
|
|
|
// If ignorepath is set, turn the filename into the path
|
|
|
- if ignorepath {
|
|
|
+ if do_ignorepath {
|
|
|
if let Some(p) = outpath.file_name() {
|
|
|
outpath = PathBuf::from(p);
|
|
|
}
|