From cc8653be12ff3e9ccfb45efbe601e3de9ea1353a Mon Sep 17 00:00:00 2001 From: Adam Lesperance Date: Wed, 31 Oct 2018 04:34:59 -0500 Subject: [PATCH] Compress output files if they end in gz or gzip --- src/main.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 6dab72a..bd8432f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,9 +32,17 @@ use std::{ collections::BTreeMap, fs::File, io::{self, BufWriter}, + path::PathBuf, thread, }; +fn is_gz(path: &PathBuf) -> bool { + match path.extension() { + None => false, + Some(e) => e == "gz" || e == "gzip", + } +} + fn main() -> io::Result<()> { let opts = opts::get_opts()?; let has_std = opts.validate()?; @@ -58,9 +66,16 @@ fn main() -> io::Result<()> { let mut c_writer: Option>> = if let Some(ref p) = opts.csv { if p.to_string_lossy() == "-" { Some(csv::Writer::from_writer(Box::new(io::stdout()))) + } else if is_gz(p) { + Some(csv::Writer::from_writer(Box::new( + flate2::write::GzEncoder::new( + File::create(p).expect("Couldn't create the csv file"), + flate2::Compression::new(3), + ), + ))) } else { Some(csv::Writer::from_writer(Box::new( - File::create(p.clone()).expect("Couldn't create the csv file"), + File::create(p).expect("Couldn't create the csv file"), ))) } } else { @@ -70,6 +85,11 @@ fn main() -> io::Result<()> { let mut j_writer: Option> = if let Some(ref p) = opts.json { if p.to_string_lossy() == "-" { Some(Box::new(io::stdout())) + } else if is_gz(p) { + Some(Box::new(flate2::write::GzEncoder::new( + File::create(p).expect("Couldn't create the json out file"), + flate2::Compression::new(3), + ))) } else { Some(Box::new(BufWriter::new( File::create(p).expect("Couldn't create the json out file"), @@ -86,6 +106,11 @@ fn main() -> io::Result<()> { let t = thread::spawn(move || { let mut c: csv::Writer> = if upath.to_string_lossy() == "-" { csv::Writer::from_writer(Box::new(stdout.lock())) + } else if is_gz(&upath) { + csv::Writer::from_writer(Box::new(flate2::write::GzEncoder::new( + File::create(upath).expect("Couldn't create the uniques csv file"), + flate2::Compression::new(3), + ))) } else { csv::Writer::from_writer(Box::new( File::create(upath).expect("Couldn't create the uniques csv file"),