/// Recursively generates an archive from the given [path].
Recursively generates an archive from the given [path].
Recursively generates an archive from the given [ path ] .
fn archive_from_path(
    zip_writer: &mut ZipWriter<File>,
    path: &Path,
    output_dir: &Path,
    exclude_dir_name: &str,
) -> Result<(), Box<dyn Error>> {
    if path.is_dir() {
        // Relative path of the directory from the output directory.
        let path_rel = path.strip_prefix(output_dir)?.to_str().unwrap();
        // `\` must be replaced with `/` otherwise the builder won't be able to
        // locate files.
        let path_rel = path_rel.replace("\\", "/");
        if !path.file_name().unwrap().eq(exclude_dir_name) {
            zip_writer.add_directory(path_rel, FileOptions::default())?;
        }
        // List all the entities in this directory
        let entities = fs::read_dir(path)?;
        // Then recursively add all the entities to the archive
        for entity in entities {
            let entity_path = entity?.path();
            archive_from_path(
                zip_writer,
                entity_path.as_path(),
                output_dir,
                exclude_dir_name,
            )?;
        }
    } else {
        let file = open_file(path, false);
        let contents = contents_as_bytes(file)?;
        // Relative path of the file from the output directory.
        let mut path_rel = path
            .strip_prefix(output_dir)?
            .to_str()
            .unwrap()
            .replace("\\", "/");
        if !exclude_dir_name.is_empty() {
            path_rel = path_rel.replace(&format!("{}/", exclude_dir_name), "");
        }
        // Add this file in the archive
        zip_writer.start_file(path_rel, FileOptions::default())?;
        // Then write out it's contents
        zip_writer.write_all(contents.as_slice())?;
    }
    Ok(())
}
fn archive_from_path ( zip_writer : & mut ZipWriter < File > , path : & Path , output_dir : & Path , exclude_dir_name : & str , ) -> Result < ( ) , Box < dyn Error > > { if path . is_dir ( ) { // Relative path of the directory from the output directory. let path_rel = path . strip_prefix ( output_dir ) ? . to_str ( ) . unwrap ( ) ; // `\` must be replaced with `/` otherwise the builder won't be able to // locate files. let path_rel = path_rel . replace ( " \\ " , " " ) ; if ! path . file_name ( ) . unwrap ( ) . eq ( exclude_dir_name ) { zip_writer . add_directory ( path_rel , FileOptions :: default ( ) ) ? ; } // List all the entities in this directory let entities = fs :: read_dir ( path ) ? ; // Then recursively add all the entities to the archive for entity in entities { let entity_path = entity ? . path ( ) ; archive_from_path ( zip_writer , entity_path . as_path ( ) , output_dir , exclude_dir_name , ) ? ; } } else { let file = open_file ( path , false ) ; let contents = contents_as_bytes ( file ) ? ; // Relative path of the file from the output directory. let mut path_rel = path . strip_prefix ( output_dir ) ? . to_str ( ) . unwrap ( ) . replace ( " \\ " , " " ) ; if ! exclude_dir_name . is_empty ( ) { path_rel = path_rel . replace ( & format ! ( " " exclude_dir_name ) , " " ) ; } // Add this file in the archive zip_writer . start_file ( path_rel , FileOptions :: default ( ) ) ? ; // Then write out it's contents zip_writer . write_all ( contents . as_slice ( ) ) ? ; } Ok ( ( ) ) }
Recursively generates an archive from the given [path].
Recursively generates an archive from the given [ path ] .
zip_writer
&mut ZipWriter<File>
path
&Path
output_dir
&Path
exclude_dir_name
&str