Optimize extract_zip: Use async_zip::read::stream::ZipFileReader to avoid temporary file (#590)

* Add new dep async_zip v0.0.9 to binstalk-downloader
   with features "gzip", "zstd", "xz", "bzip2", "tokio".
* Refactor: Simplify `async_extracter::extract_*` API
* Refactor: Create newtype wrapper of `ZipError`
   so that the zip can be upgraded without affecting API of this crate.
* Enable feature fs of dep tokio in binstalk-downloader
* Rewrite `extract_zip` to use `async_zip::read::stream::ZipFileReader`
   which avoids writing the zip file to a temporary file and then read it
   back into memory.
* Refactor: Impl new fn `await_on_option` and use it
* Optimize `tokio::select!`: Make them biased and check for cancellation first
  to make cancellation takes effect ASAP.
* Rm unused dep zip from binstalk-downloader

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-12-12 11:44:34 +11:00 committed by GitHub
parent e6f969245a
commit 3b1a7f2c78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 372 additions and 94 deletions

View file

@ -1,18 +1,12 @@
use std::{
fs::File,
io::{self, BufRead, Read},
path::Path,
};
use std::io::{self, BufRead, Read};
use bzip2::bufread::BzDecoder;
use flate2::bufread::GzDecoder;
use tar::Archive;
use tracing::debug;
use xz2::bufread::XzDecoder;
use zip::read::ZipArchive;
use zstd::stream::Decoder as ZstdDecoder;
use super::{DownloadError, TarBasedFmt};
use super::TarBasedFmt;
pub fn create_tar_decoder(
dat: impl BufRead + 'static,
@ -35,12 +29,3 @@ pub fn create_tar_decoder(
Ok(Archive::new(r))
}
pub fn unzip(dat: File, dst: &Path) -> Result<(), DownloadError> {
debug!("Decompressing from zip archive to `{dst:?}`");
let mut zip = ZipArchive::new(dat)?;
zip.extract(dst)?;
Ok(())
}