Add function to BDFWriter to set the compression level

master
trivernis 4 years ago
parent 40374f77e7
commit 4ecf46ebc2

@ -1,6 +1,6 @@
[package]
name = "bdflib"
version = "0.1.1"
version = "0.1.2"
authors = ["trivernis <trivernis@gmail.com>"]
edition = "2018"
license-file = "LICENSE"

@ -144,9 +144,9 @@ impl GenericChunk {
}
/// Compresses the data of the chunk using lzma with a level of 6
pub fn compress(&mut self) -> Result<(), Error> {
pub fn compress(&mut self, level: u32) -> Result<(), Error> {
let data = self.data.as_slice();
let mut compressor = XzEncoder::new(data, 1);
let mut compressor = XzEncoder::new(data, level);
let mut compressed: Vec<u8> = Vec::new();
compressor.read_to_end(&mut compressed)?;
self.length = compressed.len() as u32;

@ -24,6 +24,7 @@ pub struct BDFWriter {
data_entries: Vec<DataEntry>,
head_written: bool,
compressed: bool,
compression_level: u32,
}
impl BDFWriter {
@ -36,6 +37,7 @@ impl BDFWriter {
writer,
head_written: false,
compressed: compress,
compression_level: 1,
}
}
@ -81,7 +83,7 @@ impl BDFWriter {
let mut data_chunk =
GenericChunk::from_data_entries(&self.data_entries, &self.lookup_table);
if self.compressed {
data_chunk.compress()?;
data_chunk.compress(self.compression_level)?;
}
let data = data_chunk.serialize();
self.writer.write(data.as_slice())?;
@ -95,6 +97,11 @@ impl BDFWriter {
pub fn flush_writer(&mut self) -> Result<(), Error> {
self.writer.flush()
}
/// Sets the compression level for lzma compression
pub fn set_compression_level(&mut self, level: u32) {
self.compression_level = level;
}
}
impl BDFReader {

@ -37,6 +37,7 @@ mod tests {
#[test]
fn it_writes_compressed() -> Result<(), Error> {
let mut writer = new_writer("tmp2.bdf", 2, true)?;
writer.set_compression_level(3);
writer.add_lookup_entry(HashEntry::new(FOO.to_string(), 4))?;
writer.add_lookup_entry(HashEntry::new(BAR.to_string(), 5))?;

Loading…
Cancel
Save