Compare commits
No commits in common. "475524d36d05019e30f3be4152d5654ce5d03f88" and "abec71ce52caef0d0ee0a901852b0f2ba454c8ee" have entirely different histories.
475524d36d
...
abec71ce52
@ -10,4 +10,3 @@ cert_lib = { path = "../cert_lib" }
|
|||||||
clap = { version = "4.5.4", features = ["derive"] }
|
clap = { version = "4.5.4", features = ["derive"] }
|
||||||
serde = { version = "1.0.197", features = ["derive"] }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
serde_json = "1.0.115"
|
serde_json = "1.0.115"
|
||||||
zip = "0.6.6"
|
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{BufWriter, Read, Write},
|
io::{BufWriter, Write},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
use zip::{write::FileOptions, ZipWriter};
|
|
||||||
|
|
||||||
#[derive(Debug, Args)]
|
#[derive(Debug, Args)]
|
||||||
pub struct PowerEqualResultOption {
|
pub struct PowerEqualResultOption {
|
||||||
@ -13,70 +12,13 @@ pub struct PowerEqualResultOption {
|
|||||||
key_file: PathBuf,
|
key_file: PathBuf,
|
||||||
#[arg(short, long, default_value = "false", help = "Export to power.conf")]
|
#[arg(short, long, default_value = "false", help = "Export to power.conf")]
|
||||||
export: bool,
|
export: bool,
|
||||||
#[arg(
|
|
||||||
short,
|
|
||||||
long,
|
|
||||||
default_value = "false",
|
|
||||||
help = "Replace power.conf in given zip file, will override export option"
|
|
||||||
)]
|
|
||||||
zip_file: Option<PathBuf>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn calculate_equal_result(options: PowerEqualResultOption) {
|
pub fn calculate_equal_result(options: PowerEqualResultOption) {
|
||||||
let cert = cert_lib::load_certificate(options.key_file).expect("load certificate failed");
|
let cert = cert_lib::load_certificate(options.key_file).expect("load certificate failed");
|
||||||
let result =
|
let result =
|
||||||
cert_lib::calculate_power_euqal_result(cert).expect("calculate equal result failed");
|
cert_lib::calculate_power_euqal_result(cert).expect("calculate equal result failed");
|
||||||
if let Some(zip_file) = options.zip_file {
|
if options.export {
|
||||||
let mut buffer: Vec<u8> = Vec::new();
|
|
||||||
{
|
|
||||||
let mut buf_writer = BufWriter::new(&mut buffer);
|
|
||||||
buf_writer
|
|
||||||
.write("[Args]\n\n[Result]\n".as_bytes())
|
|
||||||
.expect("write power.conf failed");
|
|
||||||
buf_writer
|
|
||||||
.write(result.as_bytes())
|
|
||||||
.expect("write power.conf failed");
|
|
||||||
buf_writer.flush().expect("write power.conf failed");
|
|
||||||
}
|
|
||||||
// 将zip_file的文件名增加一个`_replaced`后缀并形成一个新的位于相同目录的PathBuf实例。
|
|
||||||
let mut zip_file_path = zip_file.clone();
|
|
||||||
let file_name = zip_file_path.file_stem().unwrap().to_str().unwrap();
|
|
||||||
let mut zip_file_name = file_name.to_string();
|
|
||||||
zip_file_name.push_str("_replaced.");
|
|
||||||
zip_file_name.push_str(&zip_file_path.extension().unwrap().to_string_lossy());
|
|
||||||
zip_file_path.set_file_name(zip_file_name);
|
|
||||||
// 创建一个新的zip文件并将zip_file的内容复制到新的zip文件中。
|
|
||||||
let zip_file = File::open(zip_file).expect("open zip file failed");
|
|
||||||
let mut zip_file = zip::ZipArchive::new(zip_file).expect("open zip file failed");
|
|
||||||
let zip_file_path = File::create(zip_file_path).expect("create zip file failed");
|
|
||||||
let mut zip_file_writer = ZipWriter::new(zip_file_path);
|
|
||||||
for i in 0..zip_file.len() {
|
|
||||||
let mut file = zip_file.by_index(i).expect("get file from zip failed");
|
|
||||||
let options = FileOptions::default()
|
|
||||||
.compression_method(file.compression())
|
|
||||||
.unix_permissions(file.unix_mode().unwrap());
|
|
||||||
|
|
||||||
let file_name = file.name().to_owned();
|
|
||||||
let file_name = file_name.replace("power.conf", "power.conf");
|
|
||||||
|
|
||||||
let content = if file_name.ends_with("power.conf") {
|
|
||||||
buffer.clone()
|
|
||||||
} else {
|
|
||||||
let mut content = Vec::new();
|
|
||||||
file.read_to_end(&mut content).expect("read file failed");
|
|
||||||
content
|
|
||||||
};
|
|
||||||
|
|
||||||
zip_file_writer
|
|
||||||
.start_file(file_name, options)
|
|
||||||
.expect("start output file failed");
|
|
||||||
zip_file_writer
|
|
||||||
.write_all(&content)
|
|
||||||
.expect("write output file failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
zip_file_writer.finish().expect("write zip file failed");
|
|
||||||
} else if options.export {
|
|
||||||
let mut power_conf_path = PathBuf::new();
|
let mut power_conf_path = PathBuf::new();
|
||||||
power_conf_path.push(".");
|
power_conf_path.push(".");
|
||||||
power_conf_path.push("power.conf");
|
power_conf_path.push("power.conf");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user