add zip_util.rs

This commit is contained in:
2019-12-01 11:15:55 +08:00
parent 8e15d49a98
commit 2b44720224
4 changed files with 75 additions and 24 deletions

View File

@@ -18,6 +18,7 @@ reqwest = "0.9.22"
sequoia-openpgp = "0.12.0" sequoia-openpgp = "0.12.0"
oss-rust-sdk = "0.1.12" oss-rust-sdk = "0.1.12"
chrono = "0.4.10" chrono = "0.4.10"
zip = "0.5.3"
rust_util = { git = "https://github.com/jht5945/rust_util" } rust_util = { git = "https://github.com/jht5945/rust_util" }

View File

@@ -2,6 +2,7 @@ extern crate sequoia_openpgp as openpgp;
pub mod oss_util; pub mod oss_util;
pub mod pgp_util; pub mod pgp_util;
pub mod config_util; pub mod config_util;
pub mod zip_util;
pub mod opt; pub mod opt;
use std::{ use std::{
@@ -11,7 +12,7 @@ use rust_util::{
XResult, XResult,
util_msg::*, util_msg::*,
}; };
use config_util::*; // use config_util::*;
// use pgp_util::OpenPGPTool; // use pgp_util::OpenPGPTool;
use opt::{ use opt::{
Options, Options,
@@ -28,30 +29,24 @@ fn main() -> XResult<()> {
} }
println!("Hello, world!"); println!("Hello, world!");
println!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); println!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs());
zip_util::zip_file("a", "aa.zip")?;
// let openpgp_client = OpenPGPTool::from_file("sample.gpg")?; // let openpgp_client = OpenPGPTool::from_file("sample.gpg")?;
// openpgp_client.encrypt_file("a", "b.asc", true)?;
// openpgp_client.encrypt_file("a", "b.gpg", false)?; // let config_json = get_config_json();
// let j = config_json.unwrap();
let config_json = get_config_json(); // println!("{}", j);
// let c = parse_config(&j);
let j = config_json.unwrap(); // println!("{:?}", c);
// println!("");
println!("{}", j); // for i in &c.items {
// println!("{:?}", i);
let c = parse_config(&j); // println!("{}", make_oss_key(&c, &i, "gpg"));
// println!("{}", make_oss_key(&c, &i, "asc"));
println!("{:?}", c); // }
println!("");
for i in &c.items {
println!("{:?}", i);
println!("{}", make_oss_key(&c, &i, "gpg"));
println!("{}", make_oss_key(&c, &i, "asc"));
}
Ok(()) Ok(())
} }

View File

@@ -52,6 +52,9 @@ impl OpenPGPTool {
} }
pub fn encrypt_file(&self, from_file: &str, to_file: &str, armor: bool) -> XResult<()> { pub fn encrypt_file(&self, from_file: &str, to_file: &str, armor: bool) -> XResult<()> {
if !Path::new(from_file).exists() {
return Err(new_box_error(&format!("From file NOT exists: {}", from_file)));
}
if Path::new(to_file).exists() { if Path::new(to_file).exists() {
return Err(new_box_error(&format!("To file exists: {}", to_file))); return Err(new_box_error(&format!("To file exists: {}", to_file)));
} }
@@ -63,17 +66,18 @@ impl OpenPGPTool {
None => return Err(new_box_error("Encryption key not found in TPK")), None => return Err(new_box_error("Encryption key not found in TPK")),
Some(r) => r, Some(r) => r,
}; };
let bw = BufWriter::new(File::create(to_file)?);
let message = if armor { let message = if armor {
Message::new(armor::Writer::new(std::io::stdout(), armor::Kind::Message, &[])?) Message::new(armor::Writer::new(bw, armor::Kind::Message, &[])?)
} else { } else {
Message::new(BufWriter::new(File::create(to_file)?)) Message::new(bw)
}; };
let encryptor = Encryptor::for_recipient(message, recipient).build()?; let encryptor = Encryptor::for_recipient(message, recipient).build()?;
let mut pgp_encrypt_writer = LiteralWriter::new(encryptor).build()?; let mut pgp_encrypt_writer = LiteralWriter::new(encryptor).build()?;
let mut from = File::open(from_file)?; let mut from = File::open(from_file)?;
encrypt_read_write(&mut from, &mut pgp_encrypt_writer)?; encrypt_read_write(&mut from, &mut pgp_encrypt_writer)?;
pgp_encrypt_writer.finalize()?; pgp_encrypt_writer.finalize()?;
Ok(()) Ok(())
} }
} }

51
src/zip_util.rs Normal file
View 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(())
}