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

@@ -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,17 +66,18 @@ 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()?;
let mut from = File::open(from_file)?;
encrypt_read_write(&mut from, &mut pgp_encrypt_writer)?;
pgp_encrypt_writer.finalize()?;
Ok(())
}
}