libroast/
decompress.rs

1// SPDX-License-Identifier: MPL-2.0
2
3// Copyright (C) 2025 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
9//! Decompress and extract core utility functions.
10
11use std::{
12    fs,
13    io,
14    io::Seek,
15    path::Path,
16};
17use tar;
18#[allow(unused_imports)]
19use tracing::{
20    debug,
21    error,
22    info,
23    warn,
24};
25
26/// Decompresses and extracts an archive with Gz.
27pub fn targz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
28{
29    use flate2::bufread::GzDecoder;
30    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
31    src.seek(io::SeekFrom::Start(0))?;
32    let enc = GzDecoder::new(src);
33    let mut ar = tar::Archive::new(enc);
34    ar.unpack(outdir.as_ref())?;
35    debug!(
36        "Successfully decompressed and extracted tape gz-compressed archive from {} to {}",
37        srcpath.as_ref().to_string_lossy(),
38        outdir.as_ref().to_string_lossy(),
39    );
40    Ok(())
41}
42
43/// Decompresses and extracts an archive with Zstd.
44pub fn tarzst(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
45{
46    use zstd::Decoder;
47    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
48    src.seek(io::SeekFrom::Start(0))?;
49    let enc = Decoder::new(src)?;
50    let mut ar = tar::Archive::new(enc);
51    ar.unpack(outdir.as_ref())?;
52    debug!(
53        "Successfully decompressed and extracted tape zstd-compressed archive from {} to {}",
54        srcpath.as_ref().to_string_lossy(),
55        outdir.as_ref().to_string_lossy(),
56    );
57    Ok(())
58}
59
60/// Decompresses and extracts an archive with Xz/Lzma.
61pub fn tarxz(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
62{
63    use xz2::read::XzDecoder;
64    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
65    src.seek(io::SeekFrom::Start(0))?;
66    let enc = XzDecoder::new(src);
67    let mut ar = tar::Archive::new(enc);
68    ar.unpack(outdir.as_ref())?;
69    debug!(
70        "Successfully decompressed and extracted tape xz-compressed archive from {} to {}",
71        srcpath.as_ref().to_string_lossy(),
72        outdir.as_ref().to_string_lossy(),
73    );
74    Ok(())
75}
76
77/// Decompresses and extracts an archive with Bz2.
78pub fn tarbz2(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
79{
80    use bzip2::bufread::MultiBzDecoder;
81
82    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
83    src.seek(io::SeekFrom::Start(0))?;
84    let enc = MultiBzDecoder::new(src);
85    let mut ar = tar::Archive::new(enc);
86    ar.unpack(outdir.as_ref())?;
87    debug!(
88        "Successfully decompressed and extracted tape bz2-compressed archive from {} to {}",
89        srcpath.as_ref().to_string_lossy(),
90        outdir.as_ref().to_string_lossy(),
91    );
92    Ok(())
93}
94
95/// Extracts an uncompressed archive.
96pub fn vanilla(outdir: impl AsRef<Path>, srcpath: impl AsRef<Path>) -> io::Result<()>
97{
98    let mut src = io::BufReader::new(fs::File::open(srcpath.as_ref())?);
99    src.seek(io::SeekFrom::Start(0))?;
100    let mut ar = tar::Archive::new(src);
101    ar.unpack(outdir.as_ref())?;
102    debug!(
103        "Successfully extracted tape archive from {} to {}",
104        srcpath.as_ref().to_string_lossy(),
105        outdir.as_ref().to_string_lossy(),
106    );
107    Ok(())
108}