- 添加所需的库到
Cargo.toml
文件中:
zip = "0.6.6"
- 直接上代码, 都在酒里了.
use std::fs::File;
use std::io::{Read, Write};
use std::process::exit;
use std::path::{Path, PathBuf};
use zip::ZipArchive;
fn main() {
// ======设置输入输出路径======
let zip_path = Path::new("xxx.gz");
println!("{}", zip_path.display());
if !zip_path.exists() {
println!("===指定文件不存在!===");
exit(1);
}
let mut output_path = PathBuf::new();
output_path.push("./output");
// ======设置输入输出路径======
// ======读取压缩文件数据======
let zip_data = File::open(zip_path).expect("===读取压缩文件失败!!===");
let mut archive = ZipArchive::new(zip_data).unwrap();
// ======读取压缩文件数据======
// ======挨个处理压缩文件中的文件======
for index in 0..archive.len() {
let mut file_data: zip::read::ZipFile<'_> = archive.by_index(index).expect("===读取压缩文件失败!!===");
let out_file_name = match file_data.enclosed_name() {
Some(path) => path.to_owned(),
None => continue,
}; // 获取单个文件的文件名
let out_file_name: PathBuf = PathBuf::from(&output_path).join(out_file_name);
println!("解压文件==>:{}", out_file_name.display());
let mut outfile = File::create(&out_file_name).expect("===创建数据文件失败!!===");
let mut buffer: Vec<u8> = Vec::new();
file_data.read_to_end(&mut buffer).unwrap(); // 将文件数据读入缓冲区
outfile.write_all(&buffer).expect("===写入文件失败!!==="); // 将缓冲区文件存入文件.
}
}
标签:mut,name,zip,crate,gz,let,file,path
From: https://www.cnblogs.com/SleepSupervisor/p/17803615.html