Impl new fn helpers::extracter::untar

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-09 13:15:23 +10:00
parent cc13a23b07
commit be5e8616a2
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::fs::File;
use std::io::Read;
use std::path::Path;
@ -11,6 +12,31 @@ use zstd::stream::Decoder as ZstdDecoder;
use crate::{BinstallError, PkgFmt};
fn untar(
dat: impl Read,
path: &Path,
desired_outputs: Option<&[Cow<'_, Path>]>,
) -> Result<(), BinstallError> {
let mut tar = Archive::new(dat);
if let Some(desired_outputs) = desired_outputs {
for res in tar.entries()? {
let mut entry = res?;
let entry_path = entry.path()?;
if desired_outputs.contains(&entry_path) {
let dst = path.join(entry_path);
entry.unpack(dst)?;
}
}
} else {
tar.unpack(path)?;
}
Ok(())
}
/// Extract files from the specified source onto the specified path.
///
/// * `fmt` - must not be `PkgFmt::Bin` or `PkgFmt::Zip`.