rust-cab
rust-cab copied to clipboard
unpack full contents
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.
Just for reference, cpio_archive uses a cursor-like approch, I kind of like that design.