(
zip: &mut ZipWriter<W>,
files: &Vec<String>,
)
| 380 | } |
| 381 | |
| 382 | fn include_files_in_zip<W: Write + Seek>( |
| 383 | zip: &mut ZipWriter<W>, |
| 384 | files: &Vec<String>, |
| 385 | ) -> Result<()> { |
| 386 | let mut file_map = HashMap::with_capacity(files.len()); |
| 387 | for file in files { |
| 388 | match file.split_once(':') { |
| 389 | None => file_map.insert(file.clone(), file.clone()), |
| 390 | Some((name, path)) => file_map.insert(name.into(), path.into()), |
| 391 | }; |
| 392 | } |
| 393 | |
| 394 | for (base, file) in file_map { |
| 395 | for entry in WalkDir::new(&file).into_iter().filter_map(|e| e.ok()) { |
| 396 | let path = entry.path(); |
| 397 | let base = base.clone(); |
| 398 | let file = file.clone(); |
| 399 | |
| 400 | let unix_base = convert_to_unix_path(Path::new(&base)) |
| 401 | .ok_or_else(|| BuildError::InvalidUnixFileName(base.into()))?; |
| 402 | let unix_file = convert_to_unix_path(Path::new(&file)) |
| 403 | .ok_or_else(|| BuildError::InvalidUnixFileName(file.into()))?; |
| 404 | |
| 405 | let source_name = convert_to_unix_path(path) |
| 406 | .ok_or_else(|| BuildError::InvalidUnixFileName(path.into()))?; |
| 407 | |
| 408 | let destination_name = source_name.replace(&unix_file, &unix_base); |
| 409 | |
| 410 | if path.is_dir() { |
| 411 | trace!(%destination_name, "creating directory in zip file"); |
| 412 | |
| 413 | zip.add_directory(&destination_name, SimpleFileOptions::default()) |
| 414 | .into_diagnostic() |
| 415 | .wrap_err_with(|| { |
| 416 | format!("failed to add directory `{destination_name}` to zip file") |
| 417 | })?; |
| 418 | } else { |
| 419 | trace!(%source_name, %destination_name, "including file in zip file"); |
| 420 | |
| 421 | let mut content = Vec::new(); |
| 422 | let mut file = File::open(path) |
| 423 | .into_diagnostic() |
| 424 | .wrap_err_with(|| format!("failed to open file `{path:?}`"))?; |
| 425 | file.read_to_end(&mut content) |
| 426 | .into_diagnostic() |
| 427 | .wrap_err_with(|| format!("failed to read file `{path:?}`"))?; |
| 428 | |
| 429 | let options = zip_file_options(&file, path)?; |
| 430 | |
| 431 | zip.start_file(destination_name.clone(), options) |
| 432 | .into_diagnostic() |
| 433 | .wrap_err_with(|| { |
| 434 | format!("failed to create zip content file `{destination_name:?}`") |
| 435 | })?; |
| 436 | |
| 437 | zip.write_all(&content) |
| 438 | .into_diagnostic() |
| 439 | .wrap_err_with(|| { |
no test coverage detected