mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-25 13:00:21 +00:00
Merge pull request #191 from budziq/reword_decompress
Added "Compress a directory into tarball" example and reworded decompress example
This commit is contained in:
commit
9728ca8964
2 changed files with 65 additions and 26 deletions
85
src/app.md
85
src/app.md
|
@ -3,7 +3,8 @@
|
||||||
| Recipe | Crates | Categories |
|
| Recipe | Crates | Categories |
|
||||||
|--------|--------|------------|
|
|--------|--------|------------|
|
||||||
| [Parse command line arguments][ex-clap-basic] | [![clap-badge]][clap] | [![cat-command-line-badge]][cat-command-line] |
|
| [Parse command line arguments][ex-clap-basic] | [![clap-badge]][clap] | [![cat-command-line-badge]][cat-command-line] |
|
||||||
| [Unzip a tarball to a temporary directory][ex-tar-temp] | [![flate2-badge]][flate2] [![tar-badge]][tar] [![tempdir-badge]][tempdir] | [![cat-filesystem-badge]][cat-filesystem] [![cat-compression-badge]][cat-compression] |
|
| [Decompress a tarball][ex-tar-decompress] | [![flate2-badge]][flate2] [![tar-badge]][tar] | [![cat-compression-badge]][cat-compression] |
|
||||||
|
| [Compress a directory into a tarball][ex-tar-compress] | [![flate2-badge]][flate2] [![tar-badge]][tar] | [![cat-compression-badge]][cat-compression] |
|
||||||
| [Recursively find duplicate file names][ex-dedup-filenames] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
| [Recursively find duplicate file names][ex-dedup-filenames] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
||||||
| [Recursively find all files with given predicate][ex-file-predicate] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
| [Recursively find all files with given predicate][ex-file-predicate] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
||||||
| [Recursively calculate file sizes at given depth][ex-file-sizes] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
| [Recursively calculate file sizes at given depth][ex-file-sizes] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
||||||
|
@ -103,29 +104,25 @@ The file passed is: myfile.txt
|
||||||
Your favorite number must be 256.
|
Your favorite number must be 256.
|
||||||
```
|
```
|
||||||
|
|
||||||
[ex-tar-temp]: #ex-tar-temp
|
[ex-tar-decompress]: #ex-tar-decompress
|
||||||
<a name="ex-tar-temp"></a>
|
<a name="ex-tar-decompress"></a>
|
||||||
## Unzip a tarball to a temporary directory
|
## Decompress a tarball
|
||||||
|
|
||||||
[![flate2-badge]][flate2] [![tar-badge]][tar] [![tempdir-badge]][tempdir] [![cat-filesystem-badge]][cat-filesystem] [![cat-compression-badge]][cat-compression]
|
[![flate2-badge]][flate2] [![tar-badge]][tar] [![cat-compression-badge]][cat-compression]
|
||||||
|
|
||||||
Uncompress ([`flate2::read::GzDecoder::new`]) and
|
Decompress ([`flate2::read::GzDecoder::new`]) and
|
||||||
extract ([`tar::Archive::unpack`]) all files form a zipped tarball
|
extract ([`tar::Archive::unpack`]) all files from a compressed tarball
|
||||||
named archive.tar.gz located in the current working directory
|
named `archive.tar.gz` located in the current working directory.
|
||||||
inside a temporary directory ([`tempdir::TempDir::new`])
|
|
||||||
and delete everything.
|
|
||||||
|
|
||||||
```rust,no_run
|
```rust,no_run
|
||||||
# #[macro_use]
|
# #[macro_use]
|
||||||
# extern crate error_chain;
|
# extern crate error_chain;
|
||||||
extern crate flate2;
|
extern crate flate2;
|
||||||
extern crate tar;
|
extern crate tar;
|
||||||
extern crate tempdir;
|
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use flate2::read::GzDecoder;
|
use flate2::read::GzDecoder;
|
||||||
use tar::Archive;
|
use tar::Archive;
|
||||||
use tempdir::TempDir;
|
|
||||||
#
|
#
|
||||||
# error_chain! {
|
# error_chain! {
|
||||||
# foreign_links {
|
# foreign_links {
|
||||||
|
@ -136,23 +133,62 @@ use tempdir::TempDir;
|
||||||
fn run() -> Result<()> {
|
fn run() -> Result<()> {
|
||||||
let path = "archive.tar.gz";
|
let path = "archive.tar.gz";
|
||||||
|
|
||||||
// Open our zipped tarball
|
// Open a compressed tarball
|
||||||
let tar_gz = File::open(path)?;
|
let tar_gz = File::open(path)?;
|
||||||
// Uncompressed it
|
// Decompress it
|
||||||
let tar = GzDecoder::new(tar_gz)?;
|
let tar = GzDecoder::new(tar_gz)?;
|
||||||
// Load the archive from the tarball
|
// Load the archive from the tarball
|
||||||
let mut archive = Archive::new(tar);
|
let mut archive = Archive::new(tar);
|
||||||
// Create a directory inside of `std::env::temp_dir()`, named with
|
// Unpack the archive inside curent working directory
|
||||||
// the prefix "temp".
|
archive.unpack(".")?;
|
||||||
let tmp_dir = TempDir::new("temp")?;
|
|
||||||
// Unpack the archive inside the temporary directory
|
|
||||||
archive.unpack(tmp_dir.path())?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
#
|
#
|
||||||
# quick_main!(run);
|
# quick_main!(run);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[ex-tar-compress]: #ex-tar-compress
|
||||||
|
<a name="ex-tar-compress"></a>
|
||||||
|
## Compress a directory into tarball
|
||||||
|
|
||||||
|
[![flate2-badge]][flate2] [![tar-badge]][tar] [![cat-compression-badge]][cat-compression]
|
||||||
|
|
||||||
|
Compresses `/var/log` directory into `archive.tar.gz`.
|
||||||
|
|
||||||
|
Creates a [`File`] wrapped in [`flate2::write::GzEncoder`]
|
||||||
|
and [`tar::Builder`]. </br>Adds contents of `/var/log` directory recursively into the archive
|
||||||
|
under `backup/logs`path with [`Builder::append_dir_all`].
|
||||||
|
[`flate2::write::GzEncoder`] is responsible for transparently compressing the
|
||||||
|
data prior to writing it into `archive.tar.gz`.
|
||||||
|
|
||||||
|
```rust,no_run
|
||||||
|
# #[macro_use]
|
||||||
|
# extern crate error_chain;
|
||||||
|
extern crate tar;
|
||||||
|
extern crate flate2;
|
||||||
|
#
|
||||||
|
# error_chain! {
|
||||||
|
# foreign_links {
|
||||||
|
# Io(std::io::Error);
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
|
||||||
|
use std::fs::File;
|
||||||
|
use flate2::Compression;
|
||||||
|
use flate2::write::GzEncoder;
|
||||||
|
|
||||||
|
fn run() -> Result<()> {
|
||||||
|
let tar_gz = File::create("archive.tar.gz")?;
|
||||||
|
let enc = GzEncoder::new(tar_gz, Compression::Default);
|
||||||
|
let mut tar = tar::Builder::new(enc);
|
||||||
|
tar.append_dir_all("backup/logs", "/var/log")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
#
|
||||||
|
# quick_main!(run);
|
||||||
|
```
|
||||||
|
|
||||||
[ex-dedup-filenames]: #ex-dedup-filenames
|
[ex-dedup-filenames]: #ex-dedup-filenames
|
||||||
<a name="ex-dedup-filenames"></a>
|
<a name="ex-dedup-filenames"></a>
|
||||||
## Recursively find duplicate file names
|
## Recursively find duplicate file names
|
||||||
|
@ -281,15 +317,16 @@ fn main() {
|
||||||
[flate2]: https://docs.rs/flate2/
|
[flate2]: https://docs.rs/flate2/
|
||||||
[tar-badge]: https://badge-cache.kominick.com/crates/v/tar.svg?label=tar
|
[tar-badge]: https://badge-cache.kominick.com/crates/v/tar.svg?label=tar
|
||||||
[tar]: https://docs.rs/tar/
|
[tar]: https://docs.rs/tar/
|
||||||
[tempdir-badge]: https://badge-cache.kominick.com/crates/v/tempdir.svg?label=tempdir
|
|
||||||
[tempdir]: https://docs.rs/tempdir/
|
|
||||||
[walkdir-badge]: https://badge-cache.kominick.com/crates/v/walkdir.svg?label=walkdir
|
[walkdir-badge]: https://badge-cache.kominick.com/crates/v/walkdir.svg?label=walkdir
|
||||||
[walkdir]: https://docs.rs/walkdir/
|
[walkdir]: https://docs.rs/walkdir/
|
||||||
|
|
||||||
<!-- Reference -->
|
<!-- Reference -->
|
||||||
|
|
||||||
[`flate2::read::GzDecoder::new`]: https://docs.rs/flate2/0.2.19/flate2/read/struct.GzDecoder.html#method.new
|
[`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
|
||||||
[`tar::Archive::unpack`]: https://docs.rs/tar/0.4.12/tar/struct.Archive.html#method.new
|
[`flate2::read::GzDecoder::new`]: https://docs.rs/flate2/*/flate2/read/struct.GzDecoder.html#method.new
|
||||||
[`tempdir::TempDir::new`]: https://docs.rs/tempdir/0.3.5/tempdir/struct.TempDir.html#method.new
|
[`flate2::write::GzEncoder`]: https://docs.rs/flate2/*/flate2/write/struct.GzEncoder.html
|
||||||
|
[`tar::Archive::unpack`]: https://docs.rs/tar/*/tar/struct.Archive.html#method.unpack
|
||||||
|
[`tar::Builder`]: https://docs.rs/tar/*/tar/struct.Builder.html
|
||||||
|
[`Builder::append_dir_all`]: https://docs.rs/tar/*/tar/struct.Builder.html#method.append_dir_all
|
||||||
[`WalkDir::min_depth`]: https://docs.rs/walkdir/*/walkdir/struct.WalkDir.html#method.min_depth
|
[`WalkDir::min_depth`]: https://docs.rs/walkdir/*/walkdir/struct.WalkDir.html#method.min_depth
|
||||||
[`WalkDir::max_depth`]: https://docs.rs/walkdir/*/walkdir/struct.WalkDir.html#method.max_depth
|
[`WalkDir::max_depth`]: https://docs.rs/walkdir/*/walkdir/struct.WalkDir.html#method.max_depth
|
||||||
|
|
|
@ -74,7 +74,8 @@ community. It needs and welcomes help. For details see
|
||||||
| Recipe | Crates | Categories |
|
| Recipe | Crates | Categories |
|
||||||
|--------|--------|------------|
|
|--------|--------|------------|
|
||||||
| [Parse command line arguments][ex-clap-basic] | [![clap-badge]][clap] | [![cat-command-line-badge]][cat-command-line] |
|
| [Parse command line arguments][ex-clap-basic] | [![clap-badge]][clap] | [![cat-command-line-badge]][cat-command-line] |
|
||||||
| [Unzip a tarball to a temporary directory][ex-tar-temp] | [![flate2-badge]][flate2] [![tar-badge]][tar] [![tempdir-badge]][tempdir] | [![cat-filesystem-badge]][cat-filesystem] [![cat-compression-badge]][cat-compression] |
|
| [Decompress a tarball][ex-tar-decompress] | [![flate2-badge]][flate2] [![tar-badge]][tar] | [![cat-compression-badge]][cat-compression] |
|
||||||
|
| [Compress a directory into a tarball][ex-tar-compress] | [![flate2-badge]][flate2] [![tar-badge]][tar] | [![cat-compression-badge]][cat-compression] |
|
||||||
| [Recursively find duplicate file names][ex-dedup-filenames] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
| [Recursively find duplicate file names][ex-dedup-filenames] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
||||||
| [Recursively find all files with given predicate][ex-file-predicate] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
| [Recursively find all files with given predicate][ex-file-predicate] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
||||||
| [Recursively calculate file sizes at given depth][ex-file-sizes] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
| [Recursively calculate file sizes at given depth][ex-file-sizes] | [![walkdir-badge]][walkdir] | [![cat-filesystem-badge]][cat-filesystem] |
|
||||||
|
@ -230,7 +231,8 @@ Keep lines sorted.
|
||||||
[ex-rest-head]: net.html#ex-rest-head
|
[ex-rest-head]: net.html#ex-rest-head
|
||||||
[ex-rest-post]: net.html#ex-rest-post
|
[ex-rest-post]: net.html#ex-rest-post
|
||||||
[ex-std-read-lines]: basics.html#ex-std-read-lines
|
[ex-std-read-lines]: basics.html#ex-std-read-lines
|
||||||
[ex-tar-temp]: app.html#ex-tar-temp
|
[ex-tar-compress]: app.html#ex-tar-compress
|
||||||
|
[ex-tar-decompress]: app.html#ex-tar-decompress
|
||||||
[ex-toml-config]: encoding.html#ex-toml-config
|
[ex-toml-config]: encoding.html#ex-toml-config
|
||||||
[ex-url-base]: net.html#ex-url-base
|
[ex-url-base]: net.html#ex-url-base
|
||||||
[ex-url-basic]: net.html#ex-url-basic
|
[ex-url-basic]: net.html#ex-url-basic
|
||||||
|
|
Loading…
Reference in a new issue