use std::{io, fs};
use std::path::{Path, PathBuf};
use std::fs::{File};
use std::io::{Seek, Write, Read};
use zip::write::FileOptions;
use walkdir::DirEntry;
use crypto::md5::Md5;
use crypto::digest::Digest;
use zip::result::ZipError;
use zip_extensions::*;
pub fn file_md5(path: &str) -> String{
let mut f = File::open(path).unwrap();
let mut buffer = Vec::new();
f.read_to_end(&mut buffer).unwrap();
let mut hasher = Md5::new();
hasher.input(&buffer);
return hasher.result_str();
}
pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
pub fn zip_dir<T>(it: &mut dyn Iterator<Item=DirEntry>, prefix: &str, writer: T) -> zip::result::ZipResult<()>
where T: Write + Seek {
let mut zip = zip::ZipWriter::new(writer);
let options = FileOptions::default()
.compression_method(zip::CompressionMethod::Stored) .unix_permissions(0o755);
let mut buffer = Vec::new();
for entry in it {
let path = entry.path();
let name = path.strip_prefix(Path::new(prefix)).unwrap();
if path.is_file() {
zip.start_file_from_path(name, options)?;
let mut f = File::open(path)?;
f.read_to_end(&mut buffer)?;
zip.write_all(&*buffer)?;
buffer.clear();
} else if name.as_os_str().len() != 0 { zip.add_directory_from_path(name, options)?;
}
}
zip.finish()?;
Result::Ok(())
}
pub fn unzip_to_path(zip_f: &str, tar_p: &str) -> Result<(), ZipError>{
let zip_ar = zip::ZipArchive::new(File::open(zip_f).unwrap()).unwrap();
let out = Path::new(tar_p);
zip_extract(&Path::new(zip_f).to_path_buf(), &out.to_path_buf())
}