8000 Added tar.xz extraction example by David-OConnor · Pull Request #53 · alexcrichton/xz2-rs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added tar.xz extraction example #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ edition = "2018"
[workspace]
members = ["systest"]


[dependencies]
lzma-sys = { path = "lzma-sys", version = "0.1.11" }
tokio-io = { version = "0.1.12", optional = true }
Expand All @@ -26,6 +27,7 @@ futures = { version = "0.1.26", optional = true }
[dev-dependencies]
rand = "0.7.0"
quickcheck = "0.9.0"
tar = "0.4.26"
tokio-core = "0.1.17"

[features]
Expand Down
19 changes: 19 additions & 0 deletions examples/unpack-archive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::{fs, io::Read, path::PathBuf};
use tar::Archive;
use xz2::read::XzDecoder;

fn unpack_tar_xz(archive_path: &PathBuf, dest: &PathBuf) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be called during the example to show off how it can be executed?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that &Path is also more idiomatic than &PathBuf

let archive_bytes = fs::read(archive_path).expect("Problem reading archive as bytes");

let mut tar: Vec<u8> = Vec::new();
let mut decompressor = XzDecoder::new(&archive_bytes[..]);
decompressor.read_to_end(&mut tar).expect("Problem decompressing archive");

// We've decompressed the .xz; now unpack the tar.
let mut archive = Archive::new(&tar[..]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that the decompress/extract step has to happen in two phases, the Archive could be passed XzDecoder directly, and additionally we don't need fs::read but rather the XzDecoder could be passed the raw file.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just stumbled upon this and wondering if here the decompress/extract step has to happen in two phases because there's no other way to archive.set_preserve_mtime(false); if it were to happen in one phase ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn mor 8D05 e.

I think you can just call that on archive at any time after construction, and constructing an Archive doesn't actually do any decompression until it's needed to do so.

archive.unpack(dest).expect("Problem unpacking tar");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this use ? and other more idiomatic error handling techniques rather than .unwrap()?

}

fn main() {

}
0