sile / libflate

A Rust implementation of DEFLATE algorithm and related formats (ZLIB, GZIP)
https://docs.rs/libflate
MIT License
180 stars 35 forks source link

Error while decoding just encoded data #70

Closed Julian-Alberts closed 1 year ago

Julian-Alberts commented 1 year ago

While testing my code, I encountered an issue where encoded data could not be decoded.

The following example panics with the message "called Result::unwrap() on an Err value: Error { kind: UnexpectedEof, message: "failed to fill whole buffer" }" at the last unwarp.

 let mut encoded = Vec::new();
 libflate::gzip::Encoder::new(&mut encoded)
    .unwrap()
    .write_all(b"Hello World")
    .unwrap();
assert!(encoded.len() > 0);

let mut decoded = Vec::default();
libflate::gzip::Decoder::new(encoded.as_slice())
    .unwrap()
    .read_to_end(&mut decoded)
    .unwrap();

assert_eq!(decoded.as_slice(), b"Hello World")

libflate version 1.3.0 rust version 1.68.0

sile commented 1 year ago

You need to call Encoder::finish() as follows:

     let mut encoded = Vec::new();
     let mut encoder = libflate::gzip::Encoder::new(&mut encoded).unwrap();
     encoder.write_all(b"Hello World").unwrap();
     encoder.finish().unwrap();
     assert!(encoded.len() > 0);

     let mut decoded = Vec::default();
     libflate::gzip::Decoder::new(encoded.as_slice())
         .unwrap()
         .read_to_end(&mut decoded)
         .unwrap();

     assert_eq!(decoded.as_slice(), b"Hello World")
Julian-Alberts commented 1 year ago

Thanks, I don't know how I missed this.