add zip_util.rs
This commit is contained in:
51
src/zip_util.rs
Normal file
51
src/zip_util.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
use std::{
|
||||
fs::File,
|
||||
path::Path,
|
||||
io::{
|
||||
BufWriter,
|
||||
},
|
||||
};
|
||||
use zip::{
|
||||
CompressionMethod,
|
||||
write::{
|
||||
ZipWriter,
|
||||
FileOptions,
|
||||
},
|
||||
};
|
||||
use rust_util::{
|
||||
XResult,
|
||||
new_box_ioerror,
|
||||
};
|
||||
|
||||
pub fn zip_file(target: &str, zip_file: &str) -> XResult<()> {
|
||||
if Path::new(zip_file).exists() {
|
||||
return Err(new_box_ioerror(&format!("Zip file exists: {}", zip_file)));
|
||||
}
|
||||
|
||||
let target_path = Path::new(target);
|
||||
if !target_path.exists() {
|
||||
return Err(new_box_ioerror(&format!("Zip path NOT exists: {}", target)));
|
||||
}
|
||||
if !(target_path.is_file() || target_path.is_dir()) {
|
||||
return Err(new_box_ioerror(&format!("Zip path NOT file or dir: {}", target)));
|
||||
}
|
||||
|
||||
let bw = BufWriter::new(File::create(zip_file)?);
|
||||
let mut zip = ZipWriter::new(bw);
|
||||
if target_path.is_file() {
|
||||
let _options = FileOptions::default().compression_method(CompressionMethod::Stored);
|
||||
// zip.start_file_from_path(target_path, options)?;
|
||||
// TODO file
|
||||
} else {
|
||||
// TODO dir
|
||||
}
|
||||
// zip.start_file("a.txt", options)?;
|
||||
// zip.write(b"hello")?;
|
||||
|
||||
// zip.start_file_from_path(path: &std::path::Path, options: FileOptions)
|
||||
|
||||
zip.finish()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user