zopfli
Zopfli Compression Algorithm is a compression library programmed in C to perform very good, but slow, deflate or zlib compression.
Top Related Projects
Brotli compression format
A massively spiffy yet delicately unobtrusive compression library.
Zstandard - Fast real-time compression algorithm
Extremely Fast Compression algorithm
miniz: Single C source file zlib-replacement library, originally from code.google.com/p/miniz
Quick Overview
Zopfli is a compression algorithm and library developed by Google that produces smaller compressed output than zlib at the cost of slower compression speed. It is compatible with existing decompression tools and can be used as a drop-in replacement for zlib or gzip compression in scenarios where file size is more important than compression speed.
Pros
- Produces smaller compressed files compared to zlib
- Compatible with existing decompression tools (gzip, zip)
- Ideal for scenarios where file size is critical (e.g., embedded systems, web assets)
- Open-source and actively maintained by Google
Cons
- Significantly slower compression speed compared to zlib
- Not suitable for real-time compression scenarios
- Limited to DEFLATE, gzip, and zlib formats
- May require more memory during compression
Code Examples
- Compressing a string using Zopfli:
#include "zopfli.h"
const char* input = "Hello, Zopfli!";
size_t input_size = strlen(input);
unsigned char* output = NULL;
size_t output_size = 0;
ZopfliOptions options;
ZopfliInitOptions(&options);
ZopfliCompress(&options, ZOPFLI_FORMAT_GZIP,
(unsigned char*)input, input_size,
&output, &output_size);
// Use the compressed data in 'output'
// ...
free(output);
- Compressing a file using Zopfli:
#include "zopfli.h"
#include <stdio.h>
FILE* input_file = fopen("input.txt", "rb");
FILE* output_file = fopen("output.gz", "wb");
unsigned char buffer[8192];
size_t bytes_read;
ZopfliOptions options;
ZopfliInitOptions(&options);
ZopfliStream zs;
ZopfliStreamInitGzip(&zs, &options);
while ((bytes_read = fread(buffer, 1, sizeof(buffer), input_file)) > 0) {
ZopfliStreamCompressChunk(&zs, buffer, bytes_read, 0);
fwrite(zs.output, 1, zs.outsize, output_file);
zs.outsize = 0;
}
ZopfliStreamCompressChunk(&zs, NULL, 0, 1);
fwrite(zs.output, 1, zs.outsize, output_file);
ZopfliStreamDestroy(&zs);
fclose(input_file);
fclose(output_file);
- Using Zopfli to optimize PNG images:
#include "zopflipng/zopflipng_lib.h"
const char* input_png = "input.png";
const char* output_png = "output.png";
ZopfliPNGOptions options;
CZopfliPNGSetDefaults(&options);
bool success = CZopfliPNGOptimize(input_png, output_png, &options);
if (success) {
printf("PNG optimization successful\n");
} else {
printf("PNG optimization failed\n");
}
Getting Started
To use Zopfli in your project:
-
Clone the repository:
git clone https://github.com/google/zopfli.git
-
Build the library:
cd zopfli make
-
Include the necessary headers in your C/C++ project:
#include "zopfli.h"
-
Link against the compiled library when building your project:
gcc -o your_program your_program.c -lzopfli
-
Use the Zopfli functions in your code as shown in the examples above.
Competitor Comparisons
Brotli compression format
Pros of Brotli
- Better compression ratio, especially for small files
- Faster decompression speed
- Wider browser support and adoption in web standards
Cons of Brotli
- Slower compression speed, especially at higher compression levels
- More complex implementation, requiring more development resources
Code Comparison
Zopfli (C):
size_t ZopfliCompress(const ZopfliOptions* options,
ZopfliFormat output_type,
const unsigned char* in,
size_t insize,
unsigned char** out,
size_t* outsize);
Brotli (C):
BROTLI_BOOL BrotliEncoderCompress(
int quality, int lgwin, BrotliEncoderMode mode, size_t input_size,
const uint8_t input_buffer[BROTLI_ARRAY_PARAM(input_size)],
size_t* encoded_size,
uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(*encoded_size)]);
Both libraries offer compression functionality, but Brotli provides more fine-grained control over compression parameters. Zopfli focuses on achieving the smallest possible output size, while Brotli balances compression ratio with speed and offers different modes for various use cases.
Brotli has gained more traction in web development due to its superior compression and decompression performance, making it a preferred choice for modern web applications. However, Zopfli remains useful for offline compression tasks where compression time is less critical.
A massively spiffy yet delicately unobtrusive compression library.
Pros of zlib
- Faster compression and decompression speeds
- Widely adopted and supported across many platforms and programming languages
- Smaller memory footprint during compression/decompression operations
Cons of zlib
- Generally achieves lower compression ratios compared to Zopfli
- Less optimized for scenarios where compression time is not a critical factor
Code Comparison
zlib (compression example):
z_stream strm;
deflateInit(&strm, Z_DEFAULT_COMPRESSION);
deflate(&strm, Z_FINISH);
deflateEnd(&strm);
Zopfli (compression example):
ZopfliOptions options;
ZopfliInitOptions(&options);
unsigned char* out = 0;
size_t outsize = 0;
ZopfliCompress(&options, ZopfliFormat, in, insize, &out, &outsize);
Both libraries offer compression functionality, but Zopfli focuses on achieving higher compression ratios at the cost of slower compression speed. zlib provides a more balanced approach with faster compression and decompression, making it suitable for real-time applications. Zopfli is often used for static content where compression time is less critical. zlib's widespread adoption and cross-platform support make it a popular choice for many developers, while Zopfli is typically used in specific scenarios where maximum compression is desired.
Zstandard - Fast real-time compression algorithm
Pros of Zstd
- Significantly faster compression and decompression speeds
- Better compression ratios at higher levels
- Supports dictionary compression for improved efficiency
Cons of Zstd
- Slightly larger compressed file sizes at lower compression levels
- More complex implementation, potentially harder to integrate
Code Comparison
Zopfli (C):
size_t c_size = 0;
unsigned char* compressed = compress(input, input_size, &c_size);
Zstd (C):
size_t c_size = ZSTD_compressBound(input_size);
void* compressed = malloc(c_size);
c_size = ZSTD_compress(compressed, c_size, input, input_size, 1);
Both libraries offer compression functionality, but Zstd provides more options and control over the compression process. Zstd's API is more extensive, allowing for advanced features like dictionary compression and adjustable compression levels.
Zopfli focuses on producing smaller compressed files at the cost of slower compression speed, making it suitable for scenarios where compression time is not critical. Zstd, on the other hand, prioritizes speed while maintaining good compression ratios, making it more versatile for real-time applications and scenarios where both speed and compression efficiency are important.
Extremely Fast Compression algorithm
Pros of lz4
- Extremely fast compression and decompression speeds
- Suitable for real-time applications and scenarios requiring low latency
- Widely adopted and supported across various platforms and programming languages
Cons of lz4
- Lower compression ratio compared to Zopfli
- Not optimized for maximum compression, prioritizing speed instead
Code Comparison
lz4:
char* compressed = LZ4_compress_default(src, dst, srcSize, dstCapacity);
int decompressedSize = LZ4_decompress_safe(compressed, decompressed, compressedSize, maxDecompressedSize);
Zopfli:
unsigned char* out = 0;
size_t outsize = 0;
ZopfliCompress(&options, ZopfliFormat, in, insize, &out, &outsize);
Key Differences
- lz4 focuses on speed and efficiency, making it ideal for scenarios where quick compression/decompression is crucial
- Zopfli prioritizes achieving the highest possible compression ratio, sacrificing speed for better compression
- lz4 is more versatile and widely used in various applications, while Zopfli is primarily used for static content where compression time is not a concern
- lz4 offers both compression and decompression functionality, whereas Zopfli is mainly used for compression, relying on existing zlib-compatible decompressors
miniz: Single C source file zlib-replacement library, originally from code.google.com/p/miniz
Pros of miniz
- Faster compression and decompression speeds
- Simpler API and easier integration into existing projects
- Supports both zlib and deflate compression formats
Cons of miniz
- Generally achieves lower compression ratios compared to Zopfli
- Less optimized for producing the smallest possible output size
- May not be suitable for scenarios where file size is the top priority
Code comparison
miniz:
mz_uint32 cmp_status;
uLong cmp_len = compressBound(src_len);
cmp_status = compress(pDst, &cmp_len, pSrc, src_len);
Zopfli:
ZopfliOptions options;
ZopfliInitOptions(&options);
ZopfliCompress(&options, ZopfliFormat::ZOPFLI_FORMAT_DEFLATE,
src, src_len, &out, &outsize);
Summary
miniz is a lightweight, fast, and easy-to-use compression library that supports both zlib and deflate formats. It offers better speed and simpler integration but may not achieve the same level of compression as Zopfli. Zopfli, on the other hand, focuses on producing the smallest possible output size at the cost of slower compression speed. The choice between the two depends on whether speed or file size is the primary concern for the specific use case.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Zopfli Compression Algorithm is a compression library programmed in C to perform very good, but slow, deflate or zlib compression.
The basic function to compress data is ZopfliCompress in zopfli.h. Use the ZopfliOptions object to set parameters that affect the speed and compression. Use the ZopfliInitOptions function to place the default values in the ZopfliOptions first.
ZopfliCompress supports deflate, gzip and zlib output format with a parameter. To support only one individual format, you can instead use ZopfliDeflate in deflate.h, ZopfliZlibCompress in zlib_container.h or ZopfliGzipCompress in gzip_container.h.
ZopfliDeflate creates a valid deflate stream in memory, see: http://www.ietf.org/rfc/rfc1951.txt ZopfliZlibCompress creates a valid zlib stream in memory, see: http://www.ietf.org/rfc/rfc1950.txt ZopfliGzipCompress creates a valid gzip stream in memory, see: http://www.ietf.org/rfc/rfc1952.txt
This library can only compress, not decompress. Existing zlib or deflate libraries can decompress the data.
zopfli_bin.c is separate from the library and contains an example program to create very well compressed gzip files. Currently the makefile builds this program with the library statically linked in.
The source code of Zopfli is under src/zopfli. Build instructions:
To build zopfli, compile all .c source files under src/zopfli to a single binary with C, and link to the standard C math library, e.g.: gcc src/zopfli/*.c -O2 -W -Wall -Wextra -Wno-unused-function -ansi -pedantic -lm -o zopfli
A makefile is provided as well, but only for linux. Use "make" to build the binary, "make libzopfli" to build it as a shared library. For other platforms, please use the build instructions above instead.
Zopfli Compression Algorithm was created by Lode Vandevenne and Jyrki Alakuijala, based on an algorithm by Jyrki Alakuijala.
Top Related Projects
Brotli compression format
A massively spiffy yet delicately unobtrusive compression library.
Zstandard - Fast real-time compression algorithm
Extremely Fast Compression algorithm
miniz: Single C source file zlib-replacement library, originally from code.google.com/p/miniz
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot