Convert Figma logo to code with AI

richgel999 logominiz

miniz: Single C source file zlib-replacement library, originally from code.google.com/p/miniz

2,147
321
2,147
113

Top Related Projects

5,575

A massively spiffy yet delicately unobtrusive compression library.

10,226

Extremely Fast Compression algorithm

23,176

Zstandard - Fast real-time compression algorithm

6,094

A fast compressor/decompressor

13,454

Brotli compression format

Quick Overview

Miniz is a lightweight, single-source-file, zlib-replacement library written in C. It provides compression and decompression functionality, including support for the deflate, inflate, and zip file formats. Miniz is designed to be easy to integrate and use in various projects.

Pros

  • Single-file implementation, making it easy to include in projects
  • Lightweight and fast, suitable for embedded systems and resource-constrained environments
  • Compatible with zlib, allowing for easy replacement in existing projects
  • Supports both compression and decompression of deflate, inflate, and zip formats

Cons

  • Limited features compared to full-fledged compression libraries
  • May not be as optimized for specific use cases as more specialized libraries
  • Documentation could be more comprehensive
  • Not actively maintained (last update was in 2019)

Code Examples

  1. Compressing data:
#define MINIZ_HEADER_FILE_ONLY
#include "miniz.c"

// Compress data
size_t src_len = strlen(source);
size_t cmp_len = compressBound(src_len);
unsigned char *pCmp = (unsigned char *)malloc(cmp_len);
int cmp_status = compress(pCmp, &cmp_len, (const unsigned char *)source, src_len);
if (cmp_status != Z_OK) {
    printf("compress() failed!\n");
}
  1. Decompressing data:
#define MINIZ_HEADER_FILE_ONLY
#include "miniz.c"

// Decompress data
size_t uncomp_len = src_len;
unsigned char *pUncomp = (unsigned char *)malloc(uncomp_len);
int uncomp_status = uncompress(pUncomp, &uncomp_len, pCmp, cmp_len);
if (uncomp_status != Z_OK) {
    printf("uncompress() failed!\n");
}
  1. Creating a ZIP archive:
#define MINIZ_HEADER_FILE_ONLY
#include "miniz.c"

mz_zip_archive zip_archive;
memset(&zip_archive, 0, sizeof(zip_archive));
if (!mz_zip_writer_init_file(&zip_archive, "archive.zip", 0)) {
    printf("mz_zip_writer_init_file() failed!\n");
}

if (!mz_zip_writer_add_mem(&zip_archive, "test.txt", "Test string", strlen("Test string"), MZ_DEFAULT_COMPRESSION)) {
    printf("mz_zip_writer_add_mem() failed!\n");
}

mz_zip_writer_finalize_archive(&zip_archive);
mz_zip_writer_end(&zip_archive);

Getting Started

To use Miniz in your project, follow these steps:

  1. Download the miniz.c file from the GitHub repository.
  2. Include the file in your project directory.
  3. In your C source file, add the following lines:
#define MINIZ_HEADER_FILE_ONLY
#include "miniz.c"

// Your code using Miniz functions goes here
  1. Compile your project, ensuring that miniz.c is included in the build process.

You can now use Miniz functions for compression, decompression, and ZIP file operations in your project.

Competitor Comparisons

5,575

A massively spiffy yet delicately unobtrusive compression library.

Pros of zlib

  • More mature and widely used, with extensive testing and optimization
  • Offers a broader range of compression algorithms and options
  • Better documentation and community support

Cons of zlib

  • Larger codebase, potentially more complex to integrate
  • May have higher memory usage for certain operations
  • Slower compilation times due to its size and complexity

Code Comparison

zlib:

z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
deflateInit(&strm, level);

miniz:

tdefl_compressor comp;
tdefl_init(&comp, NULL, NULL, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY));

Key Differences

  • miniz is designed to be a lightweight, single-file implementation
  • zlib offers more features and flexibility but with increased complexity
  • miniz may be easier to integrate into small projects or embedded systems
  • zlib is more suitable for large-scale applications requiring advanced compression options

Performance Considerations

  • zlib generally offers better compression ratios due to its advanced algorithms
  • miniz may have faster compression/decompression speeds in some scenarios
  • zlib's memory usage can be more efficiently managed in long-running applications
10,226

Extremely Fast Compression algorithm

Pros of lz4

  • Extremely fast compression and decompression speeds
  • Supports streaming compression and decompression
  • Widely used in various applications and systems

Cons of lz4

  • Lower compression ratio compared to other algorithms
  • Limited support for older systems and platforms

Code Comparison

lz4:

char* compressed_data = (char*)malloc(LZ4_compressBound(input_size));
int compressed_size = LZ4_compress_default(input_data, compressed_data, input_size, LZ4_compressBound(input_size));

miniz:

unsigned char *pComp_data = (unsigned char *)malloc(compressBound(input_size));
mz_ulong compressed_size = compressBound(input_size);
compress(pComp_data, &compressed_size, (const unsigned char *)input_data, input_size);

Key Differences

  • lz4 focuses on speed, while miniz offers a balance between speed and compression ratio
  • miniz provides a more comprehensive set of compression utilities, including ZIP file support
  • lz4 has a simpler API, making it easier to integrate into projects
  • miniz is a single-file library, which can be more convenient for some developers

Use Cases

  • lz4: Real-time compression scenarios, high-performance storage systems
  • miniz: General-purpose compression, projects requiring ZIP file manipulation
23,176

Zstandard - Fast real-time compression algorithm

Pros of Zstd

  • Higher compression ratios and faster decompression speeds
  • Supports dictionary compression for improved efficiency with small files
  • More actively maintained with frequent updates and optimizations

Cons of Zstd

  • Larger library size and more complex implementation
  • Slightly slower compression speed for some use cases
  • Steeper learning curve due to more advanced features

Code Comparison

Miniz (simple compression):

mz_ulong src_len = (mz_ulong)strlen(src_buf);
mz_ulong cmp_len = compressBound(src_len);
compress(cmp_buf, &cmp_len, (const unsigned char *)src_buf, src_len);

Zstd (simple compression):

size_t cmp_len = ZSTD_compressBound(src_len);
ZSTD_compress(cmp_buf, cmp_len, src_buf, src_len, 1);

Both libraries offer straightforward compression APIs, but Zstd provides more advanced options for fine-tuning compression parameters and performance.

Miniz is a lightweight, single-file zlib replacement focused on simplicity and ease of integration. It's ideal for projects requiring basic compression functionality with minimal overhead.

Zstd, developed by Facebook, offers state-of-the-art compression algorithms with a focus on high performance and advanced features. It's better suited for projects that prioritize compression efficiency and can handle a more complex library.

6,094

A fast compressor/decompressor

Pros of Snappy

  • Higher compression speed, optimized for performance
  • Better integration with Google's ecosystem and other Google projects
  • More active development and maintenance

Cons of Snappy

  • Lower compression ratio compared to Miniz
  • Limited platform support, primarily focused on x86 architectures
  • Larger codebase, potentially more complex to integrate

Code Comparison

Miniz (compression):

mz_uint8 *pDst_buf = (mz_uint8 *)malloc(src_len * 2);
mz_ulong dst_len = src_len * 2;
int status = mz_compress(pDst_buf, &dst_len, pSrc_buf, src_len);

Snappy (compression):

std::string output;
snappy::Compress(input_data.data(), input_data.size(), &output);

Summary

Snappy focuses on speed and integration with Google's ecosystem, making it ideal for projects prioritizing performance. Miniz offers better compression ratios and broader platform support, suitable for projects where file size reduction is crucial. Snappy's codebase is larger and more complex, while Miniz provides a simpler implementation. The choice between the two depends on specific project requirements, balancing compression speed, ratio, and ease of integration.

13,454

Brotli compression format

Pros of Brotli

  • Higher compression ratios, especially for web content
  • Faster decompression speed
  • Wider adoption and support in modern web browsers

Cons of Brotli

  • Slower compression speed compared to Miniz
  • Larger library size and more complex implementation
  • Limited to static compression scenarios (not suitable for real-time compression)

Code Comparison

Miniz (simple compression):

mz_ulong src_len = (mz_ulong)strlen(src_buf);
mz_ulong compressed_size = compressBound(src_len);
int status = compress(compressed_buf, &compressed_size, (const unsigned char *)src_buf, src_len);

Brotli (simple compression):

size_t encoded_size = BrotliEncoderMaxCompressedSize(input_size);
uint8_t* encoded_buffer = malloc(encoded_size);
BROTLI_BOOL result = BrotliEncoderCompress(quality, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE,
                                           input_size, input_buffer, &encoded_size, encoded_buffer);

Both libraries offer compression functionality, but Brotli focuses on higher compression ratios at the cost of speed, while Miniz prioritizes simplicity and faster compression. Miniz is more suitable for real-time compression scenarios, while Brotli excels in static content compression, particularly for web assets.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Miniz

Miniz is a lossless, high performance data compression library in a single source file that implements the zlib (RFC 1950) and Deflate (RFC 1951) compressed data format specification standards. It supports the most commonly used functions exported by the zlib library, but is a completely independent implementation so zlib's licensing requirements do not apply. Miniz also contains simple to use functions for writing .PNG format image files and reading/writing/appending .ZIP format archives. Miniz's compression speed has been tuned to be comparable to zlib's, and it also has a specialized real-time compressor function designed to compare well against fastlz/minilzo.

Usage

Releases are available at the releases page as a pair of miniz.c/miniz.h files which can be simply added to a project. To create this file pair the different source and header files are amalgamated during build. Alternatively use as cmake or meson module (or build system of your choice).

Features

  • MIT licensed
  • A portable, single source and header file library written in plain C. Tested with GCC, clang and Visual Studio.
  • Easily tuned and trimmed down by defines
  • A drop-in replacement for zlib's most used API's (tested in several open source projects that use zlib, such as libpng and libzip).
  • 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. See the miniz performance comparison page for example timings.
  • Not a block based compressor: miniz.c fully supports stream based processing using a coroutine-style implementation. The zlib-style API functions can be called a single byte at a time if that's all you've got.
  • Easy to use. The low-level compressor (tdefl) and decompressor (tinfl) have simple state structs which can be saved/restored as needed with simple memcpy's. The low-level codec API's don't use the heap in any way.
  • Entire inflater (including optional zlib header parsing and Adler-32 checking) is implemented in a single function as a coroutine, which is separately available in a small (~550 line) source file: miniz_tinfl.c
  • A fairly complete (but totally optional) set of .ZIP archive manipulation and extraction API's. The archive functionality is intended to solve common problems encountered in embedded, mobile, or game development situations. (The archive API's are purposely just powerful enough to write an entire archiver given a bit of additional higher-level logic.)

Building miniz - Using vcpkg

You can download and install miniz using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install miniz

The miniz port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.

Known Problems

  • No support for encrypted archives. Not sure how useful this stuff is in practice.
  • Minimal documentation. The assumption is that the user is already familiar with the basic zlib API. I need to write an API wiki - for now I've tried to place key comments before each enum/API, and I've included 6 examples that demonstrate how to use the module's major features.

Special Thanks

Thanks to Alex Evans for the PNG writer function. Also, thanks to Paul Holden and Thorsten Scheuermann for feedback and testing, Matt Pritchard for all his encouragement, and Sean Barrett's various public domain libraries for inspiration (and encouraging me to write miniz.c in C, which was much more enjoyable and less painful than I thought it would be considering I've been programming in C++ for so long).

Thanks to Bruce Dawson for reporting a problem with the level_and_flags archive API parameter (which is fixed in v1.12) and general feedback, and Janez Zemva for indirectly encouraging me into writing more examples.

Patents

I was recently asked if miniz avoids patent issues. miniz purposely uses the same core algorithms as the ones used by zlib. The compressor uses vanilla hash chaining as described here. Also see the gzip FAQ. In my opinion, if miniz falls prey to a patent attack then zlib/gzip are likely to be at serious risk too.