<commit_before>use std::fs;
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
use std::path::MAIN_SEPARATOR;

use directories::BaseDirs;
use flate2::read::GzDecoder;
use regex::Regex;
use tar::Archive;
use zip::ZipArchive;

use crate::manager::OS::WINDOWS;

const CACHE_FOLDER: &str = ".cache/selenium";
const ZIP: &str = "zip";
const GZ: &str = "gz";

pub fn clear_cache() {
    let cache_path = compose_cache_folder();
    if cache_path.exists() {
        log::debug!("Clearing cache at: {}", cache_path.display());
        fs::remove_dir_all(&cache_path).unwrap();
    }
}

pub fn create_path_if_not_exists(path: &Path) {
    if !path.exists() {
        fs::create_dir_all(&path).unwrap();
    }
}

pub fn uncompress(compressed_file: &String, target: PathBuf) {
    let file = File::open(compressed_file).unwrap();
    let kind = infer::get_from_path(compressed_file).unwrap().unwrap();
    let extension = kind.extension();
    log::trace!("The detected extension of the compressed file is {}", extension);

    if extension.eq_ignore_ascii_case(ZIP) {
        unzip(file, target);
    } else if extension.eq_ignore_ascii_case(GZ) {
        untargz(file, target);
    } else {
        let error_msg = format!("Downloaded file cannot be uncompressed ({} extension)", extension);
        log::error!("{}", error_msg);
        panic!("{}", error_msg);
    }
}

pub fn untargz(file: File, target: PathBuf) {
    log::trace!("Untargz file to {}", target.display());
    let tar = GzDecoder::new(file);
    let mut archive = Archive::new(tar);
    archive.unpack(target.parent().unwrap()).unwrap();
}

pub fn unzip(file: File, target: PathBuf) {
    log::trace!("Unzipping file to {}", target.display());
    let mut archive = ZipArchive::new(file).unwrap();

    for i in 0..archive.len() {
        let mut file = archive.by_index(i).unwrap();
        if (file.name()).ends_with('/') {
            continue;<commit_msg>[rust] Fix msedgedriver uncompression in Selenium Manager (#11180)<commit_after>use std::fs;
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
use std::path::MAIN_SEPARATOR;

use directories::BaseDirs;
use flate2::read::GzDecoder;
use regex::Regex;
use tar::Archive;
use zip::ZipArchive;

use crate::manager::OS::WINDOWS;

const CACHE_FOLDER: &str = ".cache/selenium";
const ZIP: &str = "zip";
const GZ: &str = "gz";

pub fn clear_cache() {
    let cache_path = compose_cache_folder();
    if cache_path.exists() {
        log::debug!("Clearing cache at: {}", cache_path.display());
        fs::remove_dir_all(&cache_path).unwrap();
    }
}

pub fn create_path_if_not_exists(path: &Path) {
    if !path.exists() {
        fs::create_dir_all(&path).unwrap();
    }
}

pub fn uncompress(compressed_file: &String, target: PathBuf) {
    let file = File::open(compressed_file).unwrap();
    let kind = infer::get_from_path(compressed_file).unwrap().unwrap();
    let extension = kind.extension();
    log::trace!("The detected extension of the compressed file is {}", extension);

    if extension.eq_ignore_ascii_case(ZIP) {
        unzip(file, target);
    } else if extension.eq_ignore_ascii_case(GZ) {
        untargz(file, target);
    } else {
        let error_msg = format!("Downloaded file cannot be uncompressed ({} extension)", extension);
        log::error!("{}", error_msg);
        panic!("{}", error_msg);
    }
}

pub fn untargz(file: File, target: PathBuf) {
    log::trace!("Untargz file to {}", target.display());
    let tar = GzDecoder::new(file);
    let mut archive = Archive::new(tar);
    archive.unpack(target.parent().unwrap()).unwrap();
}

pub fn unzip(file: File, target: PathBuf) {
    log::trace!("Unzipping file to {}", target.display());
    let mut archive = ZipArchive::new(file).unwrap();

    for i in 0..archive.len() {
        let mut file = archive.by_index(i).unwrap();
        if (file.name()).ends_with('/') || target.file_name().unwrap().to_str().unwrap() != file.name() {
            continue;
{'fraction_non_alphanumeric': 0.17081135393117308, 'fraction_numerical': 0.002260738507912585, 'mean_word_length': 3.9712858926342074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1630830', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1332, 'n_words': 300}