use crate::Error;
use std::fs::File;
use std::path::Path;
impl crate::Reader {
pub fn open_bzip2<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
Self::open_bzip2_with_limit(
path,
crate::io::reader_common::DEFAULT_MAX_DECOMPRESSED_BYTES,
)
}
pub fn open_bzip2_with_limit<P: AsRef<Path>>(path: P, max_bytes: u64) -> Result<Self, Error> {
Self::_open_bzip2(path, false, max_bytes).map(|(r, _)| r)
}
pub fn open_bzip2_permissive<P: AsRef<Path>>(path: P) -> Result<(Self, Vec<String>), Error> {
Self::_open_bzip2(
path,
true,
crate::io::reader_common::DEFAULT_MAX_DECOMPRESSED_BYTES,
)
}
fn _open_bzip2<P: AsRef<Path>>(
path: P,
permissive: bool,
max_bytes: u64,
) -> Result<(Self, Vec<String>), Error> {
let file = File::open(path)?;
let decoder = bzip2::read::BzDecoder::new(file);
let d = crate::io::reader_common::open_compressed(decoder, permissive, max_bytes)?;
Ok((
Self {
header: d.header,
ext_header: d.ext_header,
data: d.data,
endian: d.endian,
shape: d.shape,
},
d.warnings,
))
}
}
#[doc(hidden)]
#[derive(Debug)]
pub struct Bzip2Compressor;
impl crate::io::writer::Compressor for Bzip2Compressor {
fn compress(data: &[u8]) -> Result<Vec<u8>, Error> {
let mut encoder = bzip2::write::BzEncoder::new(Vec::new(), bzip2::Compression::default());
std::io::Write::write_all(&mut encoder, data)?;
Ok(encoder.finish()?)
}
}
pub type Bzip2Writer = crate::io::writer::CompressedWriter<Bzip2Compressor>;