add zip_util.rs
This commit is contained in:
@@ -18,6 +18,7 @@ reqwest = "0.9.22"
|
||||
sequoia-openpgp = "0.12.0"
|
||||
oss-rust-sdk = "0.1.12"
|
||||
chrono = "0.4.10"
|
||||
zip = "0.5.3"
|
||||
rust_util = { git = "https://github.com/jht5945/rust_util" }
|
||||
|
||||
|
||||
|
||||
37
src/main.rs
37
src/main.rs
@@ -2,6 +2,7 @@ extern crate sequoia_openpgp as openpgp;
|
||||
pub mod oss_util;
|
||||
pub mod pgp_util;
|
||||
pub mod config_util;
|
||||
pub mod zip_util;
|
||||
pub mod opt;
|
||||
|
||||
use std::{
|
||||
@@ -11,7 +12,7 @@ use rust_util::{
|
||||
XResult,
|
||||
util_msg::*,
|
||||
};
|
||||
use config_util::*;
|
||||
// use config_util::*;
|
||||
// use pgp_util::OpenPGPTool;
|
||||
use opt::{
|
||||
Options,
|
||||
@@ -28,30 +29,24 @@ fn main() -> XResult<()> {
|
||||
}
|
||||
|
||||
println!("Hello, world!");
|
||||
|
||||
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")?;
|
||||
// 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();
|
||||
|
||||
println!("{}", j);
|
||||
|
||||
let c = parse_config(&j);
|
||||
|
||||
println!("{:?}", c);
|
||||
|
||||
println!("");
|
||||
|
||||
for i in &c.items {
|
||||
println!("{:?}", i);
|
||||
println!("{}", make_oss_key(&c, &i, "gpg"));
|
||||
println!("{}", make_oss_key(&c, &i, "asc"));
|
||||
}
|
||||
// let config_json = get_config_json();
|
||||
// let j = config_json.unwrap();
|
||||
// println!("{}", j);
|
||||
// let c = parse_config(&j);
|
||||
// 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(())
|
||||
}
|
||||
|
||||
@@ -52,6 +52,9 @@ impl OpenPGPTool {
|
||||
}
|
||||
|
||||
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() {
|
||||
return Err(new_box_error(&format!("To file exists: {}", to_file)));
|
||||
}
|
||||
@@ -63,10 +66,11 @@ impl OpenPGPTool {
|
||||
None => return Err(new_box_error("Encryption key not found in TPK")),
|
||||
Some(r) => r,
|
||||
};
|
||||
let bw = BufWriter::new(File::create(to_file)?);
|
||||
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 {
|
||||
Message::new(BufWriter::new(File::create(to_file)?))
|
||||
Message::new(bw)
|
||||
};
|
||||
let encryptor = Encryptor::for_recipient(message, recipient).build()?;
|
||||
let mut pgp_encrypt_writer = LiteralWriter::new(encryptor).build()?;
|
||||
|
||||
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