Flate2 ZlibDecoder returning empty slice

Hi everyone

I cannot seem to get ZlibDecoder to decompress my data. It is giving me an empty slice.

Here is the code

use std::io::Read;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let buf = &std::fs::read("img.dat")?[..];

    let mut z = flate2::read::ZlibDecoder::new(buf);
    let mut inflated = Vec::<u8>::new();
    let x = z.read(&mut inflated)?;

    println!("z.read returned: {}", x);
    println!("inflated len: {}", inflated.len());

    Ok(())
}

When I run it, 0 is printed twice.

In Cargo.toml is:

[dependencies]
flate2 = "1.1.8"

Here is a gist of the file (base64 encoded) img.dat.b64 · GitHub

The first bytes are 78 9c which should indicate zlib compressed data. Python's zlib library and OpenSSL (from the command line) both correctly decompress it (when decompressed it should have length 3000 * 1841 * 3 = 16569000)

Apologies if there's something obvious I've missed, and thanks in advance.

The problem is that read takes a &mut [u8] (which cannot be grown), not a &mut Vec<u8> or something. The function dutifully fills the 0-length slice you pass to it. Perhaps read_to_end would be better?

(For maximal performance, another option is to manually control the length of the buffer instead of letting read_to_end handle everything. Either manually resize the Vec and fill the new capacity with 0 bytes, or to avoid the “fill it with 0 bytes” step, you could call slightly lower-level APIs like decompress_vec; you can figure out how to use it by copying-and-pasting parts of the implementation of read_to_end and the internal flate2::zio::read function.)

read_to_end was what I wanted.
Thanks for the help