add file zip compress
This commit is contained in:
@@ -31,7 +31,7 @@ fn main() -> XResult<()> {
|
||||
println!("Hello, world!");
|
||||
println!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
|
||||
|
||||
zip_util::zip_file("a", "aa.zip")?;
|
||||
zip_util::zip_file("hello.txt", "aa.zip")?;
|
||||
|
||||
// let openpgp_client = OpenPGPTool::from_file("sample.gpg")?;
|
||||
// openpgp_client.encrypt_file("a", "b.asc", true)?;
|
||||
|
||||
@@ -16,6 +16,7 @@ use zip::{
|
||||
use rust_util::{
|
||||
XResult,
|
||||
new_box_ioerror,
|
||||
util_io::*,
|
||||
};
|
||||
|
||||
// http://mvdnes.github.io/rust-docs/zip-rs/zip/index.html
|
||||
@@ -35,18 +36,25 @@ pub fn zip_file(target: &str, zip_file: &str) -> XResult<()> {
|
||||
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
|
||||
let options = FileOptions::default().compression_method(CompressionMethod::Stored);
|
||||
let zip_fn = get_file_name(target_path);
|
||||
zip.start_file(zip_fn, options)?;
|
||||
copy_io_with_head(&mut File::open(target_path)?, &mut zip, -1, "Compressing")?;
|
||||
} 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(())
|
||||
}
|
||||
|
||||
pub fn get_file_name(path: &Path) -> String {
|
||||
match path.file_name() {
|
||||
None => "no_file_name".to_string(),
|
||||
Some(f) => match f.to_os_string().into_string().ok() {
|
||||
None => "unknown_file_name".to_string(),
|
||||
Some(f) => f,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user