use flate2::{Compression, GzBuilder};
use std::fs::File;
use std::io;
use std::path::Path;
use xz2::stream::MtStreamBuilder;
use xz2::write::XzEncoder;
use crate::error::{Error, ErrorKind};
#[allow(dead_code)]
pub fn create_gz(in_path: &Path, out_path: &Path) -> Result<(), Error> {
log::info!("create_gz(), in: {:?}, out: {:?}", in_path, out_path);
let out_file = File::create(out_path)?;
let mut encoder = GzBuilder::new().write(out_file, Compression::default());
let mut in_file = File::open(in_path)?;
io::copy(&mut in_file, &mut encoder)?;
encoder.finish()?;
Ok(())
}
#[allow(clippy::cast_possible_truncation)]
pub fn create_xz2(in_path: &Path, out_path: &Path) -> Result<(), Error> {
log::info!("create_xz2(), in: {:?}, out: {:?}", in_path, out_path);
let xz_level = 6;
let stream = MtStreamBuilder::new()
.preset(xz_level)
.threads(num_cpus::get() as u32)
.encoder()?;
let out_file = File::create(out_path).map_err(|err| {
Error::from_string(
ErrorKind::IoError,
format!("Failed to create out file: {out_path:?}, error: {err:?}"),
)
})?;
log::info!("Create xz encoder");
let mut encoder = XzEncoder::new_stream(out_file, stream);
let mut in_file = File::open(in_path).map_err(|err| {
Error::from_string(
ErrorKind::IoError,
format!("Failed to open file {in_path:?}, err: {err:?}"),
)
})?;
io::copy(&mut in_file, &mut encoder).map_err(|err| {
Error::from_string(
ErrorKind::IoError,
format!("Failed to copy file from {in_file:?} to {out_path:?}, err: {err:?}"),
)
})?;
encoder.finish()?;
Ok(())
}