Initial commit.

This commit is contained in:
2020-07-26 09:55:03 +02:00
commit a168030929
3 changed files with 48 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
Cargo.lock

11
Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "io-decompress"
version = "0.1.0"
authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
edition = "2018"
[dependencies]
bzip2 = "0.4"
flate2 = "1.0"
mime = "0.3"
mime_guess = "2.0"

35
src/lib.rs Normal file
View 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),
})
}