Article
miniz — fast single-file ZIP implementation in C
I have an unhealthy obsession with one-file implementations of useful things in C, which you can just drop into your project and use without configuring libraries with dozens of files. Miniz is one such project: it is a single 225 KB
miniz.c
, which contains a zlib
replacement plus functions to read/write ZIP files.
miniz performance
But that's not all: miniz has a very fast level 1 (BEST_SPEED) compression implementation, which is much faster than zlib:Fills a single threaded performance vs. compression ratio gap between several popular real-time compressors and zlib. For example, at level 1, miniz.c compresses around 5-9% better than minilzo, but is approx. 35% slower. At levels 2-9, miniz.c is designed to compare favorably against zlib's ratio and speed.There are benchmarks that compare miniz with other compression libraries.
Using miniz
There's only one file —miniz.c
. There is no corresponding .h
, you just it as a header file:
#include "miniz.c"
miniz.c acts like a header file via magic ifdefs.
Then you can use either simple functions like compress
and uncompress
for in-memory compression and decompression:
// Returns Z_OK on success, or one of the error codes from deflate() on failure.
int compress(Byte *pDest, uLong *pDest_len, const Byte *pSource, uLong source_len);
// Like compress() but with more control, level may range from 0 (storing) to 9 (max. compression)
int compress2(Byte *pDest, uLong *pDest_len, const Byte *pSource, uLong source_len, int level);
// Returns Z_OK on success, or one of the error codes from inflate() on failure.
int uncompress(Byte *pDest, uLong *pDest_len, const Byte *pSource, uLong source_len);
or use zlib-compatibility functions:
deflateInit, deflateInit2, deflateReset, deflate, deflateEnd, deflateBound
inflateInit, inflateInit2, inflate, inflateEnd
You can also deal with ZIP files. For example, to extract a file from a ZIP archive to memory:
// Returns pointer to extracted file, or NULL on failure.
void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint zip_flags);
Check out examples in the repository, which show how to use miniz.