Crates.io | libz-rs-sys |
lib.rs | libz-rs-sys |
version | 0.2.1 |
source | src |
created_at | 2024-02-23 08:44:59.656473 |
updated_at | 2024-07-08 09:06:28.170523 |
description | A memory-safe zlib implementation written in rust |
homepage | https://github.com/memorysafety/zlib-rs |
repository | https://github.com/memorysafety/zlib-rs |
max_upload_size | |
id | 1150347 |
size | 187,458 |
This crate is a C API for zlib-rs. The API is broadly equivalent to zlib-sys
and zlib-ng-sys
, but does not currently provide the gz*
family of functions.
From a rust perspective, this API is not very ergonomic. Use the flate2
crate for a more
ergonomic rust interface to zlib.
This example compresses ("deflates") the string "Hello, World!"
and then decompresses
("inflates") it again.
let mut strm = libz_rs_sys::z_stream::default();
let version = libz_rs_sys::zlibVersion();
let stream_size = core::mem::size_of_val(&strm) as i32;
let level = 6; // the default compression level
let err = unsafe { libz_rs_sys::deflateInit_(&mut strm, level, version, stream_size) };
assert_eq!(err, libz_rs_sys::Z_OK);
let input = "Hello, World!";
strm.avail_in = input.len() as _;
strm.next_in = input.as_ptr();
let mut output = [0u8; 32];
strm.avail_out = output.len() as _;
strm.next_out = output.as_mut_ptr();
let err = unsafe { libz_rs_sys::deflate(&mut strm, libz_rs_sys::Z_FINISH) };
assert_eq!(err, libz_rs_sys::Z_STREAM_END);
let err = unsafe { libz_rs_sys::deflateEnd(&mut strm) };
assert_eq!(err, libz_rs_sys::Z_OK);
let deflated = &mut output[..strm.total_out as usize];
let mut strm = libz_rs_sys::z_stream::default();
let err = unsafe { libz_rs_sys::inflateInit_(&mut strm, version, stream_size) };
assert_eq!(err, libz_rs_sys::Z_OK);
strm.avail_in = deflated.len() as _;
strm.next_in = deflated.as_ptr();
let mut output = [0u8; 32];
strm.avail_out = output.len() as _;
strm.next_out = output.as_mut_ptr();
let err = unsafe { libz_rs_sys::inflate(&mut strm, libz_rs_sys::Z_FINISH) };
assert_eq!(err, libz_rs_sys::Z_STREAM_END);
let err = unsafe { libz_rs_sys::inflateEnd(&mut strm) };
assert_eq!(err, libz_rs_sys::Z_OK);
let inflated = &output[..strm.total_out as usize];
assert_eq!(inflated, input.as_bytes())