Skip to content

Commit

Permalink
implement tar+{gzip, bzip2} archiver
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Apr 27, 2024
1 parent f9272ea commit 5543c0c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bzip2 = "0.4.4"
clap = { version = "4.5.4", features = ["derive"] }
flate2 = "1.0.29"
tar = "0.4.40"
time = "0.3.36"
zip = "1.1.1"
Expand Down
4 changes: 2 additions & 2 deletions src/archiver/rar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub struct RarArchiver {
}

impl Archiver for RarArchiver {
fn perform(&self, inout: InOut) -> Result<()> {
Err(ToatError::UnknownError("not implement yet".to_string()))
fn perform(&self, _: InOut) -> Result<()> {
Err(ToatError::UnsupportedFormat("only extraction support for rar".to_string()))
}
fn format(&self) -> Format {
Format::Rar
Expand Down
26 changes: 20 additions & 6 deletions src/archiver/tar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use flate2::write::GzEncoder;
use bzip2::write::BzEncoder;
use tar::Builder;

use crate::archiver::{Archiver, InOut, Format};
Expand Down Expand Up @@ -27,22 +29,34 @@ impl Archiver for TarArchiver {
}
impl Archiver for TarGzArchiver{
fn perform(&self, inout: InOut) -> Result<()> {
Err(ToatError::UnknownError("not implement yet".to_string()))
match inout.destination() {
Err(e) => Err(e),
Ok(file) => {
let enc = GzEncoder::new(file, flate2::Compression::default());
write_to_tar(enc, inout.targets(), inout.recursive)
}
}
}
fn format(&self) -> Format {
Format::TarGz
}
}
impl Archiver for TarBz2Archiver {
fn perform(&self, inout: InOut) -> Result<()> {
Err(ToatError::UnknownError("not implement yet".to_string()))
match inout.destination() {
Err(e) => Err(e),
Ok(file) => {
let enc = BzEncoder::new(file, bzip2::Compression::best());
write_to_tar(enc, inout.targets(), inout.recursive)
}
}
}
fn format(&self) -> Format {
Format::TarBz2
}
}

fn process_dir(builder: &mut Builder<File>, target: PathBuf, recursive: bool) -> Result<()> {
fn process_dir<W: Write>(builder: &mut Builder<W>, target: PathBuf, recursive: bool) -> Result<()> {
if let Err(e) = builder.append_dir(&target, &target) {
return Err(ToatError::ArchiverError(e.to_string()))
}
Expand All @@ -59,15 +73,15 @@ fn process_dir(builder: &mut Builder<File>, target: PathBuf, recursive: bool) ->
Ok(())
}

fn process_file(builder: &mut Builder<File>, target: PathBuf) -> Result<()> {
fn process_file<W: Write>(builder: &mut Builder<W>, target: PathBuf) -> Result<()> {
if let Err(e) = builder.append_path(target) {
Err(ToatError::ArchiverError(e.to_string()))
} else {
Ok(())
}
}

fn write_to_tar(file: File, targets: Vec<PathBuf>, recursive: bool) -> Result<()> {
fn write_to_tar<W: Write>(file: W, targets: Vec<PathBuf>, recursive: bool) -> Result<()> {
let mut builder = tar::Builder::new(file);
for target in targets {
let path = target.as_path();
Expand Down

0 comments on commit 5543c0c

Please sign in to comment.