use std::{
fs::File,
io::{self, BufRead, BufReader},
path::Path,
};
use flate2::read::GzDecoder;
pub fn line_count<P>(filename: P, is_gzip: bool) -> std::io::Result<usize>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
if is_gzip {
let reader = BufReader::new(GzDecoder::new(file));
return Ok(reader.lines().count());
} else {
let reader = BufReader::new(file);
let count = reader.lines().count();
return Ok(count);
}
}
pub fn is_gzip<P>(filepath: &P) -> bool
where
P: AsRef<Path>,
{
let file_result = File::open(filepath);
match file_result {
Err(_) => false,
Ok(file) => {
let gz = GzDecoder::new(io::BufReader::new(file));
match gz.header() {
Some(_) => true,
None => false,
}
}
}
}