File: stream.rs

package info (click to toggle)
rust-zstd 0.13.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 344 kB
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download
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
use std::env;
use std::io::{self, Write};
use std::str::FromStr;

fn main() {
    match env::args().nth(1) {
        None => {
            writeln!(
                &mut io::stderr(),
                "Invalid option. Usage: `stream [-d|-1..-22]`"
            )
            .unwrap();
        }
        Some(ref option) if option == "-d" => decompress(),
        Some(ref option) => {
            if option.starts_with('-') {
                let level = match i32::from_str(&option[1..]) {
                    Ok(level) => level,
                    Err(e) => panic!("Error parsing compression level: {}", e),
                };
                compress(level);
            } else {
                writeln!(
                    &mut io::stderr(),
                    "Invalid option. Usage: `stream [-d|-1..-22]`"
                )
                .unwrap();
            }
        }
    }
}

fn compress(level: i32) {
    zstd::stream::copy_encode(io::stdin(), io::stdout(), level).unwrap();
}

fn decompress() {
    zstd::stream::copy_decode(io::stdin(), io::stdout()).unwrap();
}