libroast/
decompress.rs

1// SPDX-License-Identifier: MPL-2.0
2
3// Copyright (C) 2024 Soc Virnyl Estela and contributors
4
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9use std::{
10    fs,
11    io,
12    io::Seek,
13    path::Path,
14};
15use tar;
16#[allow(unused_imports)]
17use tracing::{
18    debug,
19    error,
20    info,
21    warn,
22};
23
24pub fn targz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
25{
26    use flate2::bufread::GzDecoder;
27    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
28    src.seek(io::SeekFrom::Start(0))?;
29    let enc = GzDecoder::new(src);
30    let mut ar = tar::Archive::new(enc);
31    ar.unpack(outdir.as_ref())?;
32    debug!(
33        "Successfully decompressed and extracted tape gz-compressed archive from {} to {}",
34        srcpath.as_ref().to_string_lossy(),
35        outdir.as_ref().to_string_lossy(),
36    );
37    Ok(())
38}
39
40pub fn tarzst(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
41{
42    use zstd::Decoder;
43    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
44    src.seek(io::SeekFrom::Start(0))?;
45    let enc = Decoder::new(src)?;
46    let mut ar = tar::Archive::new(enc);
47    ar.unpack(outdir.as_ref())?;
48    debug!(
49        "Successfully decompressed and extracted tape zstd-compressed archive from {} to {}",
50        srcpath.as_ref().to_string_lossy(),
51        outdir.as_ref().to_string_lossy(),
52    );
53    Ok(())
54}
55
56pub fn tarxz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
57{
58    use xz2::read::XzDecoder;
59    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
60    src.seek(io::SeekFrom::Start(0))?;
61    let enc = XzDecoder::new(src);
62    let mut ar = tar::Archive::new(enc);
63    ar.unpack(outdir.as_ref())?;
64    debug!(
65        "Successfully decompressed and extracted tape xz-compressed archive from {} to {}",
66        srcpath.as_ref().to_string_lossy(),
67        outdir.as_ref().to_string_lossy(),
68    );
69    Ok(())
70}
71
72pub fn tarbz2(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
73{
74    use bzip2::bufread::MultiBzDecoder;
75
76    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
77    src.seek(io::SeekFrom::Start(0))?;
78    let enc = MultiBzDecoder::new(src);
79    let mut ar = tar::Archive::new(enc);
80    ar.unpack(outdir.as_ref())?;
81    debug!(
82        "Successfully decompressed and extracted tape bz2-compressed archive from {} to {}",
83        srcpath.as_ref().to_string_lossy(),
84        outdir.as_ref().to_string_lossy(),
85    );
86    Ok(())
87}
88
89pub fn vanilla(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
90{
91    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
92    src.seek(io::SeekFrom::Start(0))?;
93    let mut ar = tar::Archive::new(src);
94    ar.unpack(outdir.as_ref())?;
95    debug!(
96        "Successfully extracted tape archive from {} to {}",
97        srcpath.as_ref().to_string_lossy(),
98        outdir.as_ref().to_string_lossy(),
99    );
100    Ok(())
101}