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.
Source code and license
Website: https://github.com/richgel999/miniz
Author: Rich Geldreich (@richgel999)
License: MIT
Hi Rich, good stuff!
I’m facing a similar problem that’s driving me nuts: I have table in a database storing the binary representation of zip files (starting with magic bytes 0x504b0304) as BLOBs, each containing exactly one text file. I would like to unzip the data in memory and return the content of these documents as a string , without the need to cache the zip file on disk. Unfortunately, zLib does not seem to work as it can only handle gzip. How would you approach this problem? I would really appreciate your help!
Thanks
Oliver
See https://stackoverflow.com/questions/45358598/ms-access-unzip-a-document-stored-as-blob-in-memory-using-vba?noredirect=1#comment77686073_45358598 for details.
Hi Oliver,
In zlib there’s sample code for packing and unpacking ZIP formats — see https://github.com/madler/zlib/tree/master/contrib/minizip
Specifically, for unzipping see https://github.com/madler/zlib/blob/master/contrib/minizip/unzip.h
There’s also this https://github.com/nmoinvaz/minizip
But the project mentioned in the post (miniz, not minizip) would also work.