summaryrefslogtreecommitdiffstats
path: root/src/decompress.rs
blob: 916293a3233a3c29c0489c116884e23b15b52a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use anyhow::{Context, Result};
use flate2::read::{GzDecoder, ZlibDecoder};
use std::fs::File;
use std::path::Path;
use tar::Archive;

/// Decompress a zipped compressed file into its parent directory.
pub fn decompress_zip(source: &Path) -> Result<()> {
    let file = File::open(source)?;
    let mut zip = zip::ZipArchive::new(file)?;

    let parent = source
        .parent()
        .context("decompress: source should have a parent")?;
    zip.extract(parent)?;

    Ok(())
}

/// Decompress a gz compressed file into its parent directory.
pub fn decompress_gz(source: &Path) -> Result<()> {
    let tar_gz = File::open(source)?;
    let tar = GzDecoder::new(tar_gz);
    let mut archive = Archive::new(tar);
    let parent = source
        .parent()
        .context("decompress: source should have a parent")?;
    archive.unpack(parent)?;

    Ok(())
}

/// Decompress a zlib compressed file into its parent directory.
pub fn decompress_xz(source: &Path) -> Result<()> {
    let tar_xz = File::open(source)?;
    let tar = ZlibDecoder::new(tar_xz);
    let mut archive = Archive::new(tar);
    let parent = source
        .parent()
        .context("decompress: source should have a parent")?;
    archive.unpack(parent)?;

    Ok(())
}

/// List files contained in a ZIP file.
/// Will return an error if the ZIP file is corrupted.
pub fn list_files_zip<P>(source: P) -> Result<Vec<String>>
where
    P: AsRef<Path>,
{
    let file = File::open(source)?;
    let zip = zip::ZipArchive::new(file)?;
    Ok(zip.file_names().map(|f| f.to_owned()).collect())
}