I need decompress and unpack big .tar.gz files (e.g. ~5Gb) in download process without save an archive file on disk. I use reqwest crate for downloading files, flate2 crate for decompressing and tar crate for unpacking. I try to do it with tar.gz format. But there are the zip and tar.bz2 formats available. (Which one is easier to work with?) It seems that I managed to implement this, but unexpectedly unpacking ended with an error:
thread 'main' panicked at 'Cannot unpack archive: Custom { kind: UnexpectedEof, error: TarError { desc: "failed to unpack `/home/ruut/Projects/GreatWar/launcher/gamedata/gamedata-master/.vscode/settings.json`", io: Custom { kind: UnexpectedEof, error: TarError { desc: "failed to unpack `gamedata-master/.vscode/settings.json` into `/home/ruut/Projects/GreatWar/launcher/gamedata/gamedata-master/.vscode/settings.json`", io: Kind(UnexpectedEof) } } } }', /home/ruut/Projects/GreatWar/launcher/src/gitlab.rs:87:38
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
My code:
let full_url = format!("{}/{}/{}", HOST, repo_info.url, repo_info.download_url);
let mut response;
match self.client.get(&full_url).send().await {
Ok(res) => response = res,
Err(error) => {
return Err(Error::new(ErrorKind::InvalidData, error));
}
};
if response.status() == reqwest::StatusCode::OK {
let mut stream = response.bytes_stream();
while let Some(item) = stream.next().await {
let chunk = item
.or(Err(format!("Error while downloading file")))
.unwrap();
let b: &[u8] = &chunk.to_vec();
let gz = GzDecoder::new(b);
let mut archive = Archive::new(gz);
archive.unpack("./gamedata").expect("Cannot unpack archive");
}
}
The archive.unpack throw error after first getting chunk.
What am I doing wrong?