use std::fs::File;
use std::io::{Error as IoError, Write};
use std::path::Path;
use tar::Builder as TarBuilder;
pub type Result<T> = ::std::result::Result<T, IoError>;
pub struct Archiver<W: Write> {
inner: TarBuilder<W>,
}
impl<W: Write> Archiver<W> {
pub fn new(writer: W) -> Archiver<W> {
Archiver {
inner: TarBuilder::new(writer),
}
}
pub fn append_path<P, Q>(&mut self, path: P, src_path: Q) -> Result<()>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
if src_path.as_ref().is_file() {
self.append_file(path, &mut File::open(src_path)?)
} else if src_path.as_ref().is_dir() {
self.append_dir(path, src_path)
} else {
panic!("Unable to append path to archive, not a file or directory");
}
}
pub fn append_file<P>(&mut self, path: P, file: &mut File) -> Result<()>
where
P: AsRef<Path>,
{
self.inner.append_file(path, file)
}
pub fn append_dir<P, Q>(&mut self, path: P, src_path: Q) -> Result<()>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
self.inner.append_dir_all(path, src_path)
}
pub fn finish(mut self) -> Result<()> {
self.inner.finish()
}
}