rust中使用zip crate解压.gz文件

  1. 添加所需的库到Cargo.toml文件中:
zip = "0.6.6"
  1. 直接上代码, 都在酒里了.
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("===写入文件失败!!==="); // 将缓冲区文件存入文件.
}
}

posted on   睡觉督导员  阅读(99)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示