This commit is contained in:
Dmitry Belyaev 2022-08-25 15:50:37 +03:00
parent 80382751df
commit 7d17e904fc
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

@ -244,22 +244,21 @@ impl<T> SplitTo<T> for Vec<T> {
} }
} }
fn process_files(files: &&[PathBuf]) { fn process_files(files: &&[PathBuf]) {
if files.is_empty() { if files.is_empty() {
return; return;
} }
let start_file = files[0].to_str().unwrap(); let start_file = files[0].to_str().unwrap();
println!("-> start from \"{}\" ({} files)", start_file, files.len()); println!("-> start from \"{}\" ({} files)", start_file, files.len());
let zip_file = fs::File::open(BASE_FILENAME).unwrap(); let zip_file = fs::File::open(BASE_FILENAME).unwrap();
let zip_reader = io::BufReader::new(zip_file); let zip_reader = io::BufReader::new(zip_file);
let mut archive = zip::ZipArchive::new(zip_reader).unwrap(); let mut archive = zip::ZipArchive::new(zip_reader).unwrap();
files.iter().for_each(|name| { files.iter().for_each(|name| {
let name_str = name.to_str().unwrap(); let name_str = name.to_str().unwrap();
// parse txt file // parse txt file
let file = archive.by_name(name_str).unwrap(); let file = archive.by_name(name_str).unwrap();
let data = parse_file(file).unwrap(); let data = parse_file(file).unwrap();
@ -273,7 +272,7 @@ fn process_files(files: &&[PathBuf]) {
let mut outfile = fs::File::create(outfilename).unwrap(); let mut outfile = fs::File::create(outfilename).unwrap();
data.write_pretty(&mut outfile, 1).unwrap(); data.write_pretty(&mut outfile, 1).unwrap();
}); });
println!("<- done {} files (from \"{}\")", files.len(), start_file); println!("<- done {} files (from \"{}\")", files.len(), start_file);
} }
@ -297,23 +296,27 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}) })
.collect(); .collect();
drop(archive); drop(archive);
// check output directory // check output directory
let out_dir: PathBuf = OUTPUT_PATH.into(); let out_dir: PathBuf = OUTPUT_PATH.into();
if out_dir.is_file() { if out_dir.is_file() {
return Err("output directory is file!".into()); return Err("output directory is file!".into());
} } else if !out_dir.exists() {
else if !out_dir.exists() {
fs::create_dir_all(out_dir)?; fs::create_dir_all(out_dir)?;
}; };
println!("processing {} files with {} threads...", source_files.len(), rayon::current_num_threads()); println!(
"processing {} files with {} threads...",
source_files.len(),
rayon::current_num_threads()
);
// split vector and process its parts in parallel // split vector and process its parts in parallel
source_files.split_to(rayon::current_num_threads()) source_files
.split_to(rayon::current_num_threads())
.par_iter() .par_iter()
.for_each(process_files); .for_each(process_files);
println!("done"); println!("done");
Ok(()) Ok(())
} }