Skip to content

Commit

Permalink
fix: add BufReader for zip extraction (#1144)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Oct 29, 2024
1 parent fe5090f commit f09886f
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/source/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,11 @@ pub(crate) fn extract_tar(
.with_style(log_handler.default_bytes_style()),
);

let file = File::open(archive).map_err(|_| SourceError::FileNotFound(archive.to_path_buf()))?;
let wrapped = progress_bar.wrap_read(file);
let buf_reader = std::io::BufReader::new(wrapped);
let file = File::open(archive)?;
let buf_reader = std::io::BufReader::with_capacity(1024 * 1024, file);
let wrapped = progress_bar.wrap_read(buf_reader);

let mut archive = tar::Archive::new(ext_to_compression(
archive.file_name(),
Box::new(buf_reader),
));
let mut archive = tar::Archive::new(ext_to_compression(archive.file_name(), Box::new(wrapped)));

let tmp_extraction_dir = tempfile::Builder::new().tempdir_in(target_directory)?;
archive
Expand Down Expand Up @@ -167,10 +164,11 @@ pub(crate) fn extract_zip(
.with_style(log_handler.default_bytes_style()),
);

let mut archive = zip::ZipArchive::new(progress_bar.wrap_read(
File::open(archive).map_err(|_| SourceError::FileNotFound(archive.to_path_buf()))?,
))
.map_err(|e| SourceError::InvalidZip(e.to_string()))?;
let file = File::open(archive)?;
let buf_reader = std::io::BufReader::with_capacity(1024 * 1024, file);
let wrapped = progress_bar.wrap_read(buf_reader);
let mut archive =
zip::ZipArchive::new(wrapped).map_err(|e| SourceError::InvalidZip(e.to_string()))?;

let tmp_extraction_dir = tempfile::Builder::new().tempdir_in(target_directory)?;
archive
Expand Down Expand Up @@ -231,8 +229,11 @@ mod test {
fn test_extract_fail() {
let fancy_log = LoggingOutputHandler::default();
let tempdir = tempfile::tempdir().unwrap();
let res = extract_zip("", tempdir.path(), &fancy_log);
assert!(matches!(res.err(), Some(SourceError::FileNotFound(_))));
let result = extract_zip("", tempdir.path(), &fancy_log);
assert!(matches!(
result,
Err(SourceError::Io(e)) if e.kind() == std::io::ErrorKind::NotFound
));
}

#[test]
Expand Down

0 comments on commit f09886f

Please sign in to comment.