Rename to Decoder.
Implement BufDecoder. Added methods to get stream_len and stream_position.
This commit is contained in:
144
src/lib.rs
144
src/lib.rs
@@ -1,35 +1,135 @@
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
pub mod read {
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
use std::path::Path;
|
||||
|
||||
use bzip2::read::BzDecoder;
|
||||
use flate2::read::GzDecoder;
|
||||
use bzip2::read::BzDecoder;
|
||||
use flate2::read::GzDecoder;
|
||||
|
||||
pub enum FileDecompressed {
|
||||
pub enum Decoder {
|
||||
Raw(File),
|
||||
Gz(GzDecoder<File>),
|
||||
Bz(BzDecoder<File>),
|
||||
}
|
||||
|
||||
impl Read for FileDecompressed {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
FileDecompressed::Raw(file) => file.read(buf),
|
||||
FileDecompressed::Gz(file) => file.read(buf),
|
||||
FileDecompressed::Bz(file) => file.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open<P>(path: P) -> Result<FileDecompressed, std::io::Error>
|
||||
where
|
||||
impl Decoder {
|
||||
pub fn open<P>(path: P) -> Result<Decoder, 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") => FileDecompressed::Gz(GzDecoder::new(file)),
|
||||
Some("application/x-bzip2") => FileDecompressed::Bz(BzDecoder::new(file)),
|
||||
_ => FileDecompressed::Raw(file),
|
||||
Some("application/x-gzip") => Decoder::Gz(GzDecoder::new(file)),
|
||||
Some("application/x-bzip2") => Decoder::Bz(BzDecoder::new(file)),
|
||||
_ => Decoder::Raw(file),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn stream_len(&mut self) -> std::io::Result<u64> {
|
||||
let file = match self {
|
||||
Decoder::Raw(file) => file,
|
||||
Decoder::Gz(file) => file.get_mut(),
|
||||
Decoder::Bz(file) => file.get_mut(),
|
||||
};
|
||||
|
||||
let old_pos = file.seek(SeekFrom::Current(0))?;
|
||||
let len = file.seek(SeekFrom::End(0))?;
|
||||
|
||||
if old_pos != len {
|
||||
file.seek(SeekFrom::Start(old_pos))?;
|
||||
}
|
||||
|
||||
Ok(len)
|
||||
}
|
||||
|
||||
pub fn stream_position(&mut self) -> std::io::Result<u64> {
|
||||
let file = match self {
|
||||
Decoder::Raw(file) => file,
|
||||
Decoder::Gz(file) => file.get_mut(),
|
||||
Decoder::Bz(file) => file.get_mut(),
|
||||
};
|
||||
|
||||
file.seek(SeekFrom::Current(0))
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for Decoder {
|
||||
#[inline]
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
Decoder::Raw(file) => file.read(buf),
|
||||
Decoder::Gz(file) => file.read(buf),
|
||||
Decoder::Bz(file) => file.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod bufread {
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read, Seek, SeekFrom};
|
||||
use std::path::Path;
|
||||
|
||||
use bzip2::bufread::BzDecoder;
|
||||
use flate2::bufread::GzDecoder;
|
||||
|
||||
pub enum BufDecoder {
|
||||
Raw(BufReader<File>),
|
||||
Gz(GzDecoder<BufReader<File>>),
|
||||
Bz(BzDecoder<BufReader<File>>),
|
||||
}
|
||||
|
||||
impl BufDecoder {
|
||||
pub fn open<P>(path: P) -> Result<BufDecoder, std::io::Error>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let file = File::open(&path).map(BufReader::new)?;
|
||||
|
||||
Ok(match mime_guess::from_path(path).first_raw() {
|
||||
Some("application/x-gzip") => BufDecoder::Gz(GzDecoder::new(file)),
|
||||
Some("application/x-bzip2") => BufDecoder::Bz(BzDecoder::new(file)),
|
||||
_ => BufDecoder::Raw(file),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn stream_len(&mut self) -> std::io::Result<u64> {
|
||||
let file = match self {
|
||||
BufDecoder::Raw(file) => file,
|
||||
BufDecoder::Gz(file) => file.get_mut(),
|
||||
BufDecoder::Bz(file) => file.get_mut(),
|
||||
};
|
||||
|
||||
let old_pos = file.seek(SeekFrom::Current(0))?;
|
||||
let len = file.seek(SeekFrom::End(0))?;
|
||||
|
||||
if old_pos != len {
|
||||
file.seek(SeekFrom::Start(old_pos))?;
|
||||
}
|
||||
|
||||
Ok(len)
|
||||
}
|
||||
|
||||
pub fn stream_position(&mut self) -> std::io::Result<u64> {
|
||||
let file = match self {
|
||||
BufDecoder::Raw(file) => file,
|
||||
BufDecoder::Gz(file) => file.get_mut(),
|
||||
BufDecoder::Bz(file) => file.get_mut(),
|
||||
};
|
||||
|
||||
file.seek(SeekFrom::Current(0))
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for BufDecoder {
|
||||
#[inline]
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
BufDecoder::Raw(file) => file.read(buf),
|
||||
BufDecoder::Gz(file) => file.read(buf),
|
||||
BufDecoder::Bz(file) => file.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user