rust-cab icon indicating copy to clipboard operation
rust-cab copied to clipboard

unpack full contents

Open sify21 opened this issue 1 year ago • 1 comments

I want to unpack all contents from a cab file to destination directory. Currently there is no api to do that, and I can't read while iterating, because folder_entries() takes immutable reference &Cabinet while read_file() takes mutable reference &mut Cabinet. The only way is to get all filenames first and store them as cloned in a Vec<String>, and then read them later, this is cumbersome.

    let mut cab = cab::Cabinet::new(file)?;
    let mut all_files = vec![];
    for folder in cab.folder_entries() {
        for f in folder.file_entries() {
            all_files.push(f.name().to_string());
        }
    }
    for name in all_files.iter() {
        let out_path = dst.join(name);
        if let Some(out_dir) = out_path.parent() {
            if std::fs::create_dir_all(out_dir).is_ok() {
                if let Ok(mut out_file) = std::fs::File::create(out_path) {
                    if let Ok(mut reader) = cab.read_file(name) {
                        std::io::copy(&mut reader, &mut out_file).ok();
                    }
                }
            }
        }
    }

I hope there is a better api for that purpose.

sify21 avatar Nov 28 '24 08:11 sify21

Just for reference, cpio_archive uses a cursor-like approch, I kind of like that design.

sify21 avatar Nov 28 '24 17:11 sify21