Debian Codegraph @

examples/zstdcat.rs

raw symbols SCIP
1 use clap::Parser;
2 use std::fs;
3 use std::io;
4
5 #[derive(Parser, Debug)]
6 #[command(author, version, about, long_about=None)]
7 struct Args {
8 /// Files to decompress. With no file, or when given -, read standard input.
9 file: Vec<String>,
10 }
11
12 fn main() {
13 // This will be a simple application:
14 // takes a single (repeatable and optional) argument.
15 let args = Args::parse();
16
17 // If nothing was given, act as if `-` was there.
18 if args.file.is_empty() {
19 decompress_file("-").unwrap();
20 } else {
21 for file in &args.file {
22 decompress_file(file).unwrap();
23 }
24 }
25 }
26
27 // Dispatch the source reader depending on the filename
28 fn decompress_file(file: &str) -> io::Result<()> {
29 match file {
30 "-" => decompress_from(io::stdin()),
31 other => decompress_from(io::BufReader::new(fs::File::open(other)?)),
32 }
33 }
34
35 // Decompress from a `Reader` into stdout
36 fn decompress_from<R: io::Read>(r: R) -> io::Result<()> {
37 let mut decoder = zstd::Decoder::new(r)?;
38 io::copy(&mut decoder, &mut io::stdout())?;
39 Ok(())
40 }