mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-29 07:02:57 +00:00

* 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>
31 lines
926 B
Rust
31 lines
926 B
Rust
use std::io::{self, BufRead, Read};
|
|
|
|
use bzip2::bufread::BzDecoder;
|
|
use flate2::bufread::GzDecoder;
|
|
use tar::Archive;
|
|
use xz2::bufread::XzDecoder;
|
|
use zstd::stream::Decoder as ZstdDecoder;
|
|
|
|
use super::TarBasedFmt;
|
|
|
|
pub fn create_tar_decoder(
|
|
dat: impl BufRead + 'static,
|
|
fmt: TarBasedFmt,
|
|
) -> io::Result<Archive<Box<dyn Read>>> {
|
|
use TarBasedFmt::*;
|
|
|
|
let r: Box<dyn Read> = match fmt {
|
|
Tar => Box::new(dat),
|
|
Tbz2 => Box::new(BzDecoder::new(dat)),
|
|
Tgz => Box::new(GzDecoder::new(dat)),
|
|
Txz => Box::new(XzDecoder::new(dat)),
|
|
Tzstd => {
|
|
// The error can only come from raw::Decoder::with_dictionary as of zstd 0.10.2 and
|
|
// 0.11.2, which is specified as `&[]` by `ZstdDecoder::new`, thus `ZstdDecoder::new`
|
|
// should not return any error.
|
|
Box::new(ZstdDecoder::with_buffer(dat)?)
|
|
}
|
|
};
|
|
|
|
Ok(Archive::new(r))
|
|
}
|