feat: switch to zip from zip-extract

This commit is contained in:
rvcas
2023-02-01 14:38:26 -05:00
committed by Lucas
parent 651f757b63
commit 618ea0c8dc
4 changed files with 267 additions and 28 deletions

View File

@@ -36,4 +36,4 @@ tokio = { version = "1.23.1", features = ["full"] }
toml = "0.5.9"
uplc = { path = '../uplc', version = "0.0.28" }
walkdir = "2.3.2"
zip-extract = "0.1.1"
zip = "0.6.4"

View File

@@ -1,7 +1,11 @@
use std::{io::Cursor, path::Path};
use std::{
io::{self, Cursor, Read},
path::{Path, PathBuf},
};
use futures::future;
use reqwest::Client;
use zip::result::ZipError;
use crate::{
error::Error,
@@ -117,7 +121,10 @@ impl<'a> Downloader<'a> {
let d = destination.clone();
tokio::task::spawn_blocking(move || {
zip_extract::extract(Cursor::new(zipball), &d, true)
let mut archive =
zip::ZipArchive::new(Cursor::new(zipball)).expect("failed to load zip archive");
extract_zip(&mut archive, &d)
})
.await?
};
@@ -131,3 +138,45 @@ impl<'a> Downloader<'a> {
Ok(true)
}
}
fn extract_zip<R: Read + io::Seek, P: AsRef<Path>>(
archive: &mut zip::ZipArchive<R>,
directory: P,
) -> Result<(), ZipError> {
use std::fs;
for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
let filepath = file
.enclosed_name()
.ok_or(ZipError::InvalidArchive("Invalid file path"))?
.iter()
.skip(1)
.collect::<PathBuf>();
let outpath = directory.as_ref().join(filepath);
if file.name().ends_with('/') {
fs::create_dir_all(&outpath)?;
} else {
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(p)?;
}
}
let mut outfile = fs::File::create(&outpath)?;
std::io::copy(&mut file, &mut outfile)?;
}
// Get and Set permissions
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() {
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode))?;
}
}
}
Ok(())
}

View File

@@ -20,7 +20,7 @@ use std::{
path::{Path, PathBuf},
};
use uplc::machine::cost_model::ExBudget;
use zip_extract::ZipExtractError;
use zip::result::ZipError;
#[allow(dead_code)]
#[derive(thiserror::Error)]
@@ -48,7 +48,7 @@ pub enum Error {
Http(#[from] reqwest::Error),
#[error(transparent)]
ZipExtract(#[from] ZipExtractError),
ZipExtract(#[from] ZipError),
#[error(transparent)]
JoinError(#[from] tokio::task::JoinError),