Initial commit.
This commit is contained in:
35
src/lib.rs
Normal file
35
src/lib.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
use bzip2::read::BzDecoder;
|
||||
use flate2::read::GzDecoder;
|
||||
|
||||
pub enum Reader {
|
||||
Raw(File),
|
||||
Gz(GzDecoder<File>),
|
||||
Bz(BzDecoder<File>),
|
||||
}
|
||||
|
||||
impl Read for Reader {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
Reader::Raw(file) => file.read(buf),
|
||||
Reader::Gz(file) => file.read(buf),
|
||||
Reader::Bz(file) => file.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open<P>(path: P) -> Result<Reader, std::io::Error>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let file = File::open(&path)?;
|
||||
|
||||
Ok(match mime_guess::from_path(path).first_raw() {
|
||||
Some("application/x-gzip") => Reader::Gz(GzDecoder::new(file)),
|
||||
Some("application/x-bzip2") => Reader::Bz(BzDecoder::new(file)),
|
||||
_ => Reader::Raw(file),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user