Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 1.12 KB

tar-compress.zh.md

File metadata and controls

32 lines (24 loc) · 1.12 KB

将目录压缩为一个 tarball

[![flate2-badge]][flate2] [![tar-badge]][tar] [![cat-compression-badge]][cat-compression]

/var/log目录压缩,为archive.tar.gz

创建一个File,包进GzEncoder,在包入tar::Builder

  • Builder::append_dir_all递归添加/var/log目录中的内容,归档到backup/logs路径下。
  • GzEncoder负责在数据写入archive.tar.gz之前,透明地压缩数据。
extern crate tar;
extern crate flate2;

use std::fs::File;
use flate2::Compression;
use flate2::write::GzEncoder;

fn main() -> Result<(), std::io::Error> {
    let tar_gz = File::create("archive.tar.gz")?;
    let enc = GzEncoder::new(tar_gz, Compression::default());
    let mut tar = tar::Builder::new(enc);
    tar.append_dir_all("backup/logs", "/var/log")?;
    Ok(())
}