use std::io;
use std::fs;
use tar::Builder;
use super::super::{Path, PathType, directory};
pub fn create(archive_path: &Path, paths_host: &Path) -> Result<(), io::Error> {
let file = fs::File::create(archive_path.to_string())?;
let mut builder = Builder::new(file);
for i in directory::list_items(paths_host)? {
if i.exists() == false {
return Err(io::Error::new(io::ErrorKind::NotFound, "Tried adding a path that does not exist to tar archive!"));
}
let inside = i.to_string().replace(&format!("{}{sc}", paths_host.to_string(), sc = Path::SPLIT_CHAR), "");
if i.path_type() == PathType::Directory {
builder.append_dir_all(inside, i.to_string())?;
}
else {
builder.append_path_with_name(i.to_string(), inside)?;
}
}
return Ok(());
}