File: zstdcat.rs

package info (click to toggle)
rust-zstd 0.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 340 kB
  • sloc: makefile: 2
file content (40 lines) | stat: -rw-r--r-- 1,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use clap::Parser;
use std::fs;
use std::io;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about=None)]
struct Args {
    /// Files to decompress. With no file, or when given -, read standard input.
    file: Vec<String>,
}

fn main() {
    // This will be a simple application:
    // takes a single (repeatable and optional) argument.
    let args = Args::parse();

    // If nothing was given, act as if `-` was there.
    if args.file.is_empty() {
        decompress_file("-").unwrap();
    } else {
        for file in &args.file {
            decompress_file(file).unwrap();
        }
    }
}

// Dispatch the source reader depending on the filename
fn decompress_file(file: &str) -> io::Result<()> {
    match file {
        "-" => decompress_from(io::stdin()),
        other => decompress_from(io::BufReader::new(fs::File::open(other)?)),
    }
}

// Decompress from a `Reader` into stdout
fn decompress_from<R: io::Read>(r: R) -> io::Result<()> {
    let mut decoder = zstd::Decoder::new(r)?;
    io::copy(&mut decoder, &mut io::stdout())?;
    Ok(())
}