mongo_embedded/
extractor.rs

1use anyhow::{anyhow, Result};
2use std::path::Path;
3use std::fs::File;
4use flate2::read::GzDecoder;
5use tar::Archive;
6
7pub fn extract(archive_path: &Path, extract_to: &Path) -> Result<()> {
8    if !extract_to.exists() {
9        std::fs::create_dir_all(extract_to)?;
10    }
11
12    let extension = archive_path
13        .extension()
14        .and_then(|e| e.to_str())
15        .ok_or_else(|| anyhow!("Unknown file extension"))?;
16
17    match extension {
18        "tgz" | "gz" => {
19            let file = File::open(archive_path)?;
20            let tar = GzDecoder::new(file);
21            let mut archive = Archive::new(tar);
22            archive.unpack(extract_to)?;
23        }
24        "zip" => {
25            let file = File::open(archive_path)?;
26            let mut archive = zip::ZipArchive::new(file)?;
27            archive.extract(extract_to)?;
28        }
29        _ => return Err(anyhow!("Unsupported archive format: {}", extension)),
30    }
31
32    Ok(())
33}