Skip to content

Commit 727a4d7

Browse files
committed
support for zipped file extensions
1 parent c9d01f9 commit 727a4d7

File tree

7 files changed

+88
-6
lines changed

7 files changed

+88
-6
lines changed

Cargo.lock

+41-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "di-csv2xml"
3-
version = "1.0.14"
3+
version = "1.0.16"
44
authors = ["Markus Klein <[email protected]>"]
55
license = "MIT"
66
publish = false
@@ -18,6 +18,7 @@ strum = "0.17.1"
1818
strum_macros = "0.17.1"
1919
structopt = "0.3.7"
2020
quick-xml = "0.17.2"
21+
libflate = "0.1.27"
2122

2223
[dev-dependencies]
2324
assert_cli = "0.6.3"

Changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
1.0.16
5+
------
6+
7+
* Support for zipped file extensions
8+
49
1.0.15
510
------
611

input.xml.gz

10 Bytes
Binary file not shown.

src/main.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ mod record_type;
44

55
use crate::{generate_xml::generate_xml, read_csv::CsvSource, record_type::RecordType};
66
use indicatif::{ProgressBar, ProgressStyle};
7+
use libflate::gzip;
78
use quicli::prelude::*;
8-
use std::{fs::File, io};
9+
use std::{fs::File, io, path::Path};
910
use structopt::StructOpt;
1011
use 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+
}

tests/input.csv.gz

45 Bytes
Binary file not shown.

tests/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ fn simple() {
1010
.unwrap();
1111
}
1212

13+
#[test]
14+
fn input_gz() {
15+
assert_cli::Assert::main_binary()
16+
.with_args(&["Category", "--input", "tests/input.csv.gz"])
17+
.succeeds()
18+
.stdout()
19+
.is(include_str!("output.xml").replace("\r\n", "\n").as_str())
20+
.unwrap();
21+
}
22+
1323
#[test]
1424
fn mask_text() {
1525
assert_cli::Assert::main_binary()

0 commit comments

Comments
 (0)