Skip to main content

compress_sized_into

Function compress_sized_into 

Source
pub fn compress_sized_into<R, W>(
    source: R,
    destination: W,
    level: i32,
    size: u64,
) -> Result<()>
where R: Read, W: Write,
Expand description

Compress everything from source, whose length you already know, into destination.

Like copy_encode(), but the length is recorded in the frame header, so whoever decompresses it can size their buffer up front - worth about a factor of two to them.

Nothing larger than the compressor’s own buffers is held in memory, so this is the one to reach for when the data is too big to want a Vec of it.

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

let input = std::fs::File::open("input.txt")?;
let size = input.metadata()?.len();
let output = std::fs::File::create("input.txt.zst")?;

zstd::compress_sized_into(input, output, 3, size)?;