2020-12-12 20:32:06 +08:00
|
|
|
use flate2::read::GzDecoder;
|
|
|
|
use flate2::write::GzEncoder;
|
2021-03-16 01:11:10 +08:00
|
|
|
use flate2::Compression;
|
2020-12-12 20:32:06 +08:00
|
|
|
use std::fs::{create_dir_all, File};
|
|
|
|
use std::path::Path;
|
2021-03-16 01:11:10 +08:00
|
|
|
use tar::{Archive, Builder};
|
2020-12-12 20:32:06 +08:00
|
|
|
|
|
|
|
use crate::error::Error;
|
|
|
|
|
|
|
|
pub fn to_tar_gz(src: &Path, dest: &Path) -> Result<(), Error> {
|
|
|
|
let f = File::create(dest)?;
|
|
|
|
let gz_encoder = GzEncoder::new(f, Compression::default());
|
|
|
|
let mut tar_encoder = Builder::new(gz_encoder);
|
|
|
|
tar_encoder.append_dir_all(".", src)?;
|
|
|
|
let gz_encoder = tar_encoder.into_inner()?;
|
|
|
|
gz_encoder.finish()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-22 17:17:38 +08:00
|
|
|
pub fn from_tar_gz(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> Result<(), Error> {
|
2021-03-22 23:30:20 +08:00
|
|
|
println!("inflating from {:?} to {:?}", src.as_ref(), dest.as_ref());
|
2021-03-22 17:17:38 +08:00
|
|
|
let f = File::open(&src)?;
|
2020-12-12 20:32:06 +08:00
|
|
|
let gz = GzDecoder::new(f);
|
|
|
|
let mut ar = Archive::new(gz);
|
2021-03-22 17:17:38 +08:00
|
|
|
create_dir_all(&dest)?;
|
|
|
|
ar.unpack(&dest)?;
|
2021-03-22 23:30:20 +08:00
|
|
|
println!("here");
|
2020-12-12 20:32:06 +08:00
|
|
|
Ok(())
|
|
|
|
}
|