MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / unzip

Function unzip

rust/src/files.rs:478–565  ·  view source on GitHub ↗
(
    compressed_file: &str,
    target: &Path,
    log: &Logger,
    single_file: Option<String>,
)

Source from the content-addressed store, hash-verified

476}
477
478pub fn unzip(
479 compressed_file: &str,
480 target: &Path,
481 log: &Logger,
482 single_file: Option<String>,
483) -> Result<(), Error> {
484 let file = File::open(compressed_file)?;
485 let compressed_path = Path::new(compressed_file);
486 let tmp_path = compressed_path
487 .parent()
488 .unwrap_or(compressed_path)
489 .to_path_buf();
490 let final_path = if single_file.is_some() {
491 target.parent().unwrap_or(target).to_path_buf()
492 } else {
493 target.to_path_buf()
494 };
495 log.trace(format!(
496 "Unzipping {} to {}",
497 compressed_file,
498 final_path.display()
499 ));
500 let mut zip_archive = ZipArchive::new(file)?;
501 let mut unzipped_files = 0;
502
503 for i in 0..zip_archive.len() {
504 let mut file = zip_archive.by_index(i)?;
505 let path: PathBuf = match file.enclosed_name() {
506 // This logic is required since some zip files (e.g. chromedriver 115+)
507 // are zipped with a parent folder, while others (e.g. chromedriver 114-)
508 // are zipped without a parent folder
509 Some(p) => {
510 let iter = p.iter();
511 if iter.to_owned().count() > 1 {
512 iter.skip(1).collect()
513 } else {
514 iter.collect()
515 }
516 }
517 None => continue,
518 };
519 if file.name().ends_with('/') {
520 log.trace(format!("File extracted to {}", tmp_path.display()));
521 fs::create_dir_all(&tmp_path)?;
522 } else {
523 let target_path = tmp_path.join(path.clone());
524 create_parent_path_if_not_exists(target_path.as_path())?;
525 let mut outfile = File::create(&target_path)?;
526
527 // Set permissions in Unix-like systems
528 #[cfg(unix)]
529 {
530 use std::os::unix::fs::PermissionsExt;
531
532 if single_file.is_some() {
533 fs::set_permissions(&target_path, fs::Permissions::from_mode(0o755))?;
534 } else if let Some(mode) = file.unix_mode() {
535 fs::set_permissions(&target_path, fs::Permissions::from_mode(mode))?;

Callers 1

uncompressFunction · 0.70

Calls 9

createFunction · 0.85
copyFunction · 0.85
remove_fileFunction · 0.85
copy_folder_contentFunction · 0.85
parentMethod · 0.80
traceMethod · 0.80
skipMethod · 0.80
nameMethod · 0.65

Tested by

no test coverage detected