add compress_file

This commit is contained in:
2020-04-18 17:14:21 +08:00
parent de492e1fbd
commit 784c15c05c
4 changed files with 58 additions and 1 deletions

View File

@@ -131,7 +131,7 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem,
#[cfg(not(feature = "use_zip"))]
let zip_file = || {
if let Err(e) = zip_util::zip_file(target, temp_zip_file) {
if let Err(e) = zip_util::compress_file(target, temp_zip_file) {
print_message(MessageType::ERROR, &format!("Error in zip file: {}, at item index: {}", e, item_index));
return false;
};

View File

@@ -9,6 +9,10 @@ use zip::{
CompressionMethod,
write::{ ZipWriter, FileOptions, },
};
use flate2:: {
Compression,
write::GzEncoder,
};
use rust_util::{
XResult,
new_box_ioerror,
@@ -17,6 +21,22 @@ use rust_util::{
util_file::*,
};
pub fn compress_file(target: &str, compress_file: &str) -> XResult<()> {
match target {
target_zip if target_zip.ends_with(".zip") => zip_file(target, compress_file),
target_tar if target_tar.ends_with(".tar.gz") => tar_file(target, compress_file),
target => Err(new_box_ioerror(&format!("Unknown target type: {}", target))),
}
}
pub fn tar_file(target: &str, tar_file: &str) -> XResult<()> {
let target_tar_gz = File::create(target)?;
let enc = GzEncoder::new(target_tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.append_path(tar_file)?;
Ok(())
}
// http://mvdnes.github.io/rust-docs/zip-rs/zip/index.html
pub fn zip_file(target: &str, zip_file: &str) -> XResult<()> {
if Path::new(zip_file).exists() {