| 1 | use std::env; |
| 2 | use std::io::{self, Write}; |
| 3 | use std::str::FromStr; |
| 4 | |
| 5 | fn main() { |
| 6 | match env::args().nth(1) { |
| 7 | None => { |
| 8 | writeln!( |
| 9 | &mut io::stderr(), |
| 10 | "Invalid option. Usage: `stream [-d|-1..-22]`" |
| 11 | ) |
| 12 | .unwrap(); |
| 13 | } |
| 14 | Some(ref option) if option == "-d" => decompress(), |
| 15 | Some(ref option) => { |
| 16 | if option.starts_with('-') { |
| 17 | let level = match i32::from_str(&option[1..]) { |
| 18 | Ok(level) => level, |
| 19 | Err(e) => panic!("Error parsing compression level: {}", e), |
| 20 | }; |
| 21 | compress(level); |
| 22 | } else { |
| 23 | writeln!( |
| 24 | &mut io::stderr(), |
| 25 | "Invalid option. Usage: `stream [-d|-1..-22]`" |
| 26 | ) |
| 27 | .unwrap(); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn compress(level: i32) { |
| 34 | zstd::stream::copy_encode(io::stdin(), io::stdout(), level).unwrap(); |
| 35 | } |
| 36 | |
| 37 | fn decompress() { |
| 38 | zstd::stream::copy_decode(io::stdin(), io::stdout()).unwrap(); |
| 39 | } |