tugger_common/
zipfile.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use {
6    anyhow::Result,
7    std::{
8        io::{Read, Seek, Write},
9        path::Path,
10    },
11};
12
13pub fn extract_zip<R: Read + Seek, P: AsRef<Path>>(reader: R, path: P) -> Result<()> {
14    let mut za = zip::ZipArchive::new(reader)?;
15
16    for i in 0..za.len() {
17        let mut file = za.by_index(i)?;
18
19        let dest_path = path.as_ref().join(file.name());
20        let parent = dest_path.parent().unwrap();
21
22        if !parent.exists() {
23            std::fs::create_dir_all(parent)?;
24        }
25
26        let mut b: Vec<u8> = Vec::new();
27        file.read_to_end(&mut b)?;
28        let mut fh = std::fs::File::create(dest_path)?;
29        fh.write_all(&b)?;
30    }
31
32    Ok(())
33}