rust-lang / flate2-rs

DEFLATE, gzip, and zlib bindings for Rust
https://docs.rs/flate2
Apache License 2.0
869 stars 159 forks source link

zLib decompressor returns empty result #353

Closed Phill030 closed 1 year ago

Phill030 commented 1 year ago

I'm trying to decompress a vec which works in C# as well as in TypeScript, but in Rust it doesn't want to work and I don't know why

  let mut buffer2: Vec<u8> = Vec::new();
  let tmp_read = binary_reader.read_bytes_at(file_record.size as usize, file_record.offset as usize).unwrap();

  let mut decompressor = Decompress::new(true);
  let status = decompressor.decompress_vec(&tmp_read[..], &mut buffer2, flate2::FlushDecompress::None).unwrap();

  println!("{:?}", status);
  println!("{:?}", buffer2);
  println!("{:?}", decompressor);

value of tmp_read is [120, 218, 251, 255, 207, 144, 193, 138, 193, 151, 161, 146, 33, 143, 33, 149, 161, 156, 161, 24, 72, 38, 51, 148, 48, 100, 50, 228, 3, 69, 120, 25, 184, 24]

prints:

Ok
[]
Decompress { inner: miniz_oxide inflate internal state. total_in: 34, total_out: 0 }
Byron commented 1 year ago

The problem is that buffer2 has no capacity. Even though decompress_vec will manage the output buffer's length, it will not grow it.

This playground shows how to do it, and validates that it writes 33 bytes into the output buffer.

Note that decompressing like this may end up with the input not being fully consumed as the output vector isn't big enough. It's probably easier to use types from the read module instead.