Skip to main content

compress_sized

Function compress_sized 

Source
pub fn compress_sized<R: Read>(
    source: R,
    level: i32,
    size: u64,
) -> Result<Vec<u8>>
Expand description

Compress everything from source, whose length you already know.

The length is recorded in the frame header, which is what lets whoever decompresses it size their buffer up front - worth about a factor of two to them. encode_all() cannot do this, because it has no way to know how much is coming.

size must be exactly how many bytes source yields; zstd fails the frame if it turns out to be wrong.

For data already in a slice, use crate::bulk::compress(), which knows the length without being told. For a file, compress_file().

let data = b"the length of this is known up front";

let frame = zstd::compress_sized(&data[..], 3, data.len() as u64)?;

assert_eq!(zstd::decompressed_size(&frame), Some(data.len() as u64));