Initial commit.
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
11
Cargo.toml
Normal file
11
Cargo.toml
Normal 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
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