summaryrefslogtreecommitdiffstats
path: root/src/modes/edit/decompress.rs
blob: 0b913509013de0013529abaf0ab487f002afe3da (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use anyhow::{Context, Result};
use flate2::read::{GzDecoder, ZlibDecoder};
use std::fs::File;
use std::path::Path;
use tar::Archive;

use crate::common::{path_to_string, TAR};
use crate::io::execute_and_output;

/// Decompress a zipped compressed file into its parent directory.
///
/// # Errors
///
/// It may fail if the file can't be opened or if [`zip::ZipArchive::new`] can't
/// read the archive.
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.
///
/// # Errors
///
/// It may fail if the file can't be opened.
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.
///
/// # Errors
///
/// It may fail if the file can't be opened.
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.
///
/// # Errors
///
/// It may fail if the source file can't be open.
/// It may also fail if [`zip::ZipArchive::new`] read the archive.
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(std::borrow::ToOwned::to_owned)
        .collect())
}

/// List files contained in a tar.something file.
/// Will return an error if `tar tvf source` can't list the content.
///
/// # Errors
///
/// It may fail if the `tar tvf` command returns an error.
pub fn list_files_tar<P>(source: P) -> Result<Vec<String>>
where
    P: AsRef<Path>,
{
    if let Ok(output) = execute_and_output(TAR, ["tvf", path_to_string(&source).as_str()]) {
        let output = String::from_utf8(output.stdout).unwrap_or_default();
        let content = output.lines().map(std::borrow::ToOwned::to_owned).collect();
        return Ok(content);
    }
    Err(anyhow::anyhow!("Tar couldn't read the file content"))
}