Use BufRead inextract_compressed_from_readable

Use `BufRead` instead of `Read` to avoid copying of data and improve
efficiency.

`GzDecoder` and `XzDecoder` both have their `BufRead` counterpart
in mod `bufread` and their `read` counterpart merely wraps the input in
a `std::io::BufReader`.

`ZstdDecoder::new` also wraps it in a `BufReader` and pass it to
`ZstdDecoder::with_buffer`.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-11 15:44:16 +10:00
parent e753c9ec30
commit 4a882dc2cb
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -1,11 +1,11 @@
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::Read; use std::io::{BufRead, Read};
use std::path::Path; use std::path::Path;
use flate2::read::GzDecoder; use flate2::bufread::GzDecoder;
use log::debug; use log::debug;
use tar::Archive; use tar::Archive;
use xz2::read::XzDecoder; use xz2::bufread::XzDecoder;
use zip::read::ZipArchive; use zip::read::ZipArchive;
use zstd::stream::Decoder as ZstdDecoder; use zstd::stream::Decoder as ZstdDecoder;
@ -55,7 +55,7 @@ fn untar<Filter: FnMut(&Path) -> bool>(
/// Note that this is a best-effort and it only works when `fmt` /// Note that this is a best-effort and it only works when `fmt`
/// is not `PkgFmt::Bin` or `PkgFmt::Zip`. /// is not `PkgFmt::Bin` or `PkgFmt::Zip`.
pub(crate) fn extract_compressed_from_readable<Filter: FnMut(&Path) -> bool>( pub(crate) fn extract_compressed_from_readable<Filter: FnMut(&Path) -> bool>(
dat: impl Read, dat: impl BufRead,
fmt: PkgFmt, fmt: PkgFmt,
path: &Path, path: &Path,
filter: Option<Filter>, filter: Option<Filter>,
@ -89,7 +89,7 @@ pub(crate) fn extract_compressed_from_readable<Filter: FnMut(&Path) -> bool>(
// as of zstd 0.10.2 and 0.11.2, which is specified // as of zstd 0.10.2 and 0.11.2, which is specified
// as &[] by ZstdDecoder::new, thus ZstdDecoder::new // as &[] by ZstdDecoder::new, thus ZstdDecoder::new
// should not return any error. // should not return any error.
let tar = ZstdDecoder::new(dat)?; let tar = ZstdDecoder::with_buffer(dat)?;
untar(tar, path, filter)?; untar(tar, path, filter)?;
} }
PkgFmt::Zip => panic!("Unexpected PkgFmt::Zip!"), PkgFmt::Zip => panic!("Unexpected PkgFmt::Zip!"),