File: flate.rs

package info (click to toggle)
rust-libflate 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 412 kB
  • sloc: makefile: 2
file content (113 lines) | stat: -rw-r--r-- 3,584 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#[cfg(not(feature = "std"))]
fn main() {}

#[cfg(feature = "std")]
fn main() {
    use clap::Parser;
    use libflate::gzip;
    use libflate::zlib;
    use std::fs;
    use std::io;
    use std::io::Read;
    use std::io::Write;

    #[derive(Parser)]
    struct Args {
        #[clap(short, long, default_value = "-")]
        input: String,

        #[clap(short, long, default_value = "-")]
        output: String,

        #[clap(short, long)]
        verbose: bool,

        #[clap(subcommand)]
        command: Command,
    }

    #[derive(clap::Subcommand)]
    enum Command {
        Copy,
        ByteRead {
            #[clap(short, long, default_value = "1")]
            unit: usize,
        },
        GzipDecode,
        GzipDecodeMulti,
        GzipEncode,
        ZlibDecode,
        ZlibEncode,
    }

    let args = Args::parse();
    let input_filename = &args.input;
    let input: Box<dyn io::Read> = if input_filename == "-" {
        Box::new(io::stdin())
    } else {
        Box::new(
            fs::File::open(input_filename).expect(&format!("Can't open file: {}", input_filename)),
        )
    };
    let mut input = io::BufReader::new(input);

    let output_filename = &args.output;
    let output: Box<dyn io::Write> = if output_filename == "-" {
        Box::new(io::stdout())
    } else if output_filename == "/dev/null" {
        Box::new(io::sink())
    } else {
        Box::new(
            fs::File::create(output_filename)
                .expect(&format!("Can't create file: {}", output_filename)),
        )
    };
    let mut output = io::BufWriter::new(output);

    let verbose = args.verbose;
    match args.command {
        Command::Copy => {
            io::copy(&mut input, &mut output).expect("Coyping failed");
        }
        Command::ByteRead { unit } => {
            let mut buf = vec![0; unit];
            let mut reader = input;
            let mut count = 0;
            while let Ok(size) = reader.read(&mut buf) {
                if size == 0 {
                    break;
                }
                count += size;
            }
            println!("COUNT: {}", count);
        }
        Command::GzipDecode => {
            let mut decoder = gzip::Decoder::new(input).expect("Read GZIP header failed");
            if verbose {
                let _ = writeln!(&mut io::stderr(), "HEADER: {:?}", decoder.header());
            }
            io::copy(&mut decoder, &mut output).expect("Decoding GZIP stream failed");
        }
        Command::GzipDecodeMulti => {
            let mut decoder = gzip::MultiDecoder::new(input).expect("Read GZIP header failed");
            io::copy(&mut decoder, &mut output).expect("Decoding GZIP stream failed");
        }
        Command::GzipEncode => {
            let mut encoder = gzip::Encoder::new(output).unwrap();
            io::copy(&mut input, &mut encoder).expect("Encoding GZIP stream failed");
            encoder.finish().into_result().unwrap();
        }
        Command::ZlibDecode => {
            let mut decoder = zlib::Decoder::new(input).expect("Read ZLIB header failed");
            if verbose {
                let _ = writeln!(&mut io::stderr(), "HEADER: {:?}", decoder.header());
            }
            io::copy(&mut decoder, &mut output).expect("Decoding ZLIB stream failed");
        }
        Command::ZlibEncode => {
            let mut encoder = zlib::Encoder::new(output).unwrap();
            io::copy(&mut input, &mut encoder).expect("Encoding ZLIB stream failed");
            encoder.finish().into_result().unwrap();
        }
    }
}