Function dochy_archiver::create_archive_from_directory[][src]

pub fn create_archive_from_directory<P: AsRef<Path>>(
    dir_path: P,
    write: &mut impl Write,
    cancel_by_hash: impl Fn(u128) -> bool,
    opt: &ArchiveOptions
) -> ArcResult<CreateArchiveFromDirectory>

Archive a directory to the archive file. ArchiveOptions customise how it's archived.

cancel_by_hash must return if the archiving process is canceled.

If the canceling isn't needed, return false.

 use dochy_archiver::{create_archive_from_directory, ArchiveOptions};

 let mut archive : Vec<u8> = vec![];
 let archive = create_archive_from_directory(
     "foo/some_dir_path",
     &mut archive,
     |_hash| false,
     &ArchiveOptions::new());

The hash is calculated by every file's filename, modified time and file size.

If you record the hash and compare, you can cancel the archiving process when no file isn't modified.

 use dochy_archiver::ArcResult;
 fn main(){
     fn2();
 }
 fn fn2() -> ArcResult<()>{
 use dochy_archiver::{create_archive_from_directory, ArchiveOptions, CreateArchiveFromDirectory};
 use std::path::Path;
 use std::fs::File;
 use std::io::Write;
     let mut buf : Vec<u8> = vec![];
     let r = create_archive_from_directory("foo/some_dir_path", &mut buf,
         |hash|{
             //If the same hash already exists, all the files are not modified,
             //so you can safely cancel the process
             Path::new("bar/some_dir2").join(format!("{}", hash)).exists()
         }, &ArchiveOptions::new())?;
     if let CreateArchiveFromDirectory::WrittenSuccessfully(_size, hash) = r{
         //record the hash and the archived data in some way.
         let mut file = File::create(Path::new("bar/some_dir2").join(format!("{}", hash)))?;
         file.write_all(&buf);
     }
     Ok(())
 }