Compress the snapshot in a tarball

This commit is contained in:
Kerollmops 2022-10-25 15:51:15 +02:00 committed by Clément Renault
parent 4cafc63561
commit 942b7c338b
No known key found for this signature in database
GPG key ID: 92ADA4E935E71FA4
8 changed files with 65 additions and 7 deletions

View file

@ -0,0 +1,28 @@
use std::fs::{create_dir_all, File};
use std::io::Write;
use std::path::Path;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use tar::{Archive, Builder};
pub fn to_tar_gz(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> anyhow::Result<()> {
let mut f = File::create(dest)?;
let gz_encoder = GzEncoder::new(&mut 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()?;
f.flush()?;
Ok(())
}
pub fn from_tar_gz(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> anyhow::Result<()> {
let f = File::open(&src)?;
let gz = GzDecoder::new(f);
let mut ar = Archive::new(gz);
create_dir_all(&dest)?;
ar.unpack(&dest)?;
Ok(())
}

View file

@ -1,3 +1,4 @@
pub mod compression;
pub mod document_formats;
pub mod error;
pub mod index_uid;