@@ -4,8 +4,9 @@ mod record_type;
44
55use crate :: { generate_xml:: generate_xml, read_csv:: CsvSource , record_type:: RecordType } ;
66use indicatif:: { ProgressBar , ProgressStyle } ;
7+ use libflate:: gzip;
78use quicli:: prelude:: * ;
8- use std:: { fs:: File , io} ;
9+ use std:: { fs:: File , io, path :: Path } ;
910use structopt:: StructOpt ;
1011use strum;
1112
@@ -55,21 +56,46 @@ fn main() -> CliResult {
5556 . template ( fmt)
5657 . progress_chars ( "#>-" ) ,
5758 ) ;
58- Box :: new ( progress_bar. wrap_read ( file) )
59+ let file_with_pbar = progress_bar. wrap_read ( file) ;
60+
61+ if has_gz_extension ( & input) {
62+ Box :: new ( gzip:: Decoder :: new ( file_with_pbar) ?)
63+ } else {
64+ Box :: new ( file_with_pbar)
65+ }
5966 } else {
60- Box :: new ( file)
67+ // Repeat if to avoid extra Box.
68+ if has_gz_extension ( & input) {
69+ Box :: new ( gzip:: Decoder :: new ( file) ?)
70+ } else {
71+ Box :: new ( file)
72+ }
6173 }
6274 } else {
6375 // just use stdin
6476 Box :: new ( io:: stdin ( ) )
6577 } ;
78+
6679 let reader = CsvSource :: new ( input, args. delimiter as u8 ) ?;
6780
6881 let mut out: Box < dyn io:: Write > = if let Some ( output) = args. output {
69- Box :: new ( io:: BufWriter :: new ( File :: create ( & output) ?) )
82+ let writer = io:: BufWriter :: new ( File :: create ( & output) ?) ;
83+
84+ if has_gz_extension ( & output) {
85+ Box :: new ( gzip:: Encoder :: new ( writer) ?)
86+ } else {
87+ Box :: new ( writer)
88+ }
7089 } else {
7190 Box :: new ( io:: stdout ( ) )
7291 } ;
7392 generate_xml ( & mut out, reader, & args. category , args. record_type ) ?;
7493 Ok ( ( ) )
7594}
95+
96+ fn has_gz_extension ( path : & Path ) -> bool {
97+ match path. extension ( ) {
98+ Some ( ext) if ext == "gz" => true ,
99+ _ => false ,
100+ }
101+ }
0 commit comments