首页 > 解决方案 > 如何从 zip 文件中读取特定文件

问题描述

我完全被困在从 zip 文件的可变路径结构中读取文件而没有解压缩它。

我的文件位于此处:

/info/[random-string]/info.json

其中 [random-string] 是 info 文件夹中的唯一文件。所以它就像读取'info'文件夹读取第一个文件夹读取'info.json'。

任何想法如何使用这些库之一(ziprc_zip)来做到这一点?

let file_path = file.to_str().unwrap();
let file = File::open(file_path).unwrap();
let reader = BufReader::new(file);

let mut archive = zip::ZipArchive::new(reader).unwrap();
let info_folder = archive.by_name("info").unwrap();
// how to list files of info_folder

标签: rustzip

解决方案


给你:

use std::error::Error;
use std::ffi::OsStr;
use std::fs::File;
use std::path::Path;
use zip::ZipArchive; // zip 0.5.13

fn main() -> Result<(), Box<dyn Error>> {
    let archive = File::open("./info.zip")?;
    let mut archive = ZipArchive::new(archive)?;

    // iterate over all files, because you don't know the exact name
    for idx in 0..archive.len() {
        let entry = archive.by_index(idx)?;
        let name = entry.enclosed_name();
        
        if let Some(name) = name {
            // process only entries which are named  info.json
            if name.file_name() == Some(OsStr::new("info.json")) {
                // the ancestors() iterator lets you walk up the path segments
                let mut ancestors = name.ancestors();
               
                // skip self - the first entry is always the full path
                ancestors.next(); 

                // skip the random string
                ancestors.next(); 

                let expect_info = ancestors.next(); 

                // the reminder must be only 'info/' otherwise this is the wrong entry
                if expect_info == Some(Path::new("info/")) {
                    // do something with the file
                    println!("Found!!!");
                    break;
                }
            }
        }
    }

    Ok(())
}

推荐阅读