Add function to BDFWriter to set the compression level

master
trivernis 5 years ago
parent 40374f77e7
commit 4ecf46ebc2

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

@ -144,9 +144,9 @@ impl GenericChunk {
} }
/// Compresses the data of the chunk using lzma with a level of 6 /// 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 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(); let mut compressed: Vec<u8> = Vec::new();
compressor.read_to_end(&mut compressed)?; compressor.read_to_end(&mut compressed)?;
self.length = compressed.len() as u32; self.length = compressed.len() as u32;

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

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

Loading…
Cancel
Save