Top Related Projects
Zstandard - Fast real-time compression algorithm
Extremely Fast Compression algorithm
A massively spiffy yet delicately unobtrusive compression library.
A fast compressor/decompressor
Zopfli Compression Algorithm is a compression library programmed in C to perform very good, but slow, deflate or zlib compression.
Optimized Go Compression Packages
Quick Overview
Brotli is a general-purpose lossless compression algorithm developed by Google. It is designed to achieve higher compression ratios than gzip and deflate while maintaining comparable decompression speeds. Brotli is particularly effective for compressing web content, such as HTML, CSS, and JavaScript files.
Pros
- Higher compression ratios compared to gzip and deflate
- Fast decompression speeds, comparable to other popular algorithms
- Widely supported by modern web browsers and servers
- Open-source and actively maintained by Google
Cons
- Slower compression speeds compared to gzip, especially at higher compression levels
- Limited support in older systems and software
- May require more memory during compression and decompression
- Not as widely adopted as gzip for general-purpose compression
Code Examples
- Compressing a string:
import brotli
text = "Hello, world! This is a test string for Brotli compression."
compressed = brotli.compress(text.encode('utf-8'))
print(f"Original size: {len(text)} bytes")
print(f"Compressed size: {len(compressed)} bytes")
- Decompressing a Brotli-compressed string:
import brotli
decompressed = brotli.decompress(compressed).decode('utf-8')
print(f"Decompressed text: {decompressed}")
- Compressing a file:
import brotli
with open('input.txt', 'rb') as input_file, open('output.br', 'wb') as output_file:
data = input_file.read()
compressed = brotli.compress(data)
output_file.write(compressed)
Getting Started
To use Brotli in your Python project, first install it using pip:
pip install brotli
Then, import the library in your Python script:
import brotli
# Compress data
compressed = brotli.compress(b"Your data here")
# Decompress data
decompressed = brotli.decompress(compressed)
For other programming languages, check the Brotli GitHub repository for language-specific bindings and installation instructions.
Competitor Comparisons
Zstandard - Fast real-time compression algorithm
Pros of Zstd
- Generally faster compression and decompression speeds
- Better compression ratios at higher levels
- More flexible with adjustable dictionary sizes
Cons of Zstd
- Slightly larger compressed file sizes at lower compression levels
- Less widespread adoption in some ecosystems
Code Comparison
Brotli:
size_t BrotliEncoderCompress(int quality, int lgwin, BrotliEncoderMode mode,
size_t input_size, const uint8_t* input_buffer,
size_t* encoded_size, uint8_t* encoded_buffer);
Zstd:
size_t ZSTD_compress(void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
int compressionLevel);
Both libraries offer simple compression functions, but Zstd's API is slightly more straightforward. Brotli provides more fine-grained control over compression parameters in its main function.
Zstd offers a wider range of compression levels (1-22) compared to Brotli (0-11), allowing for more flexibility in balancing compression ratio and speed.
Both projects are actively maintained and have strong community support. Zstd has seen rapid adoption in various applications due to its performance advantages, while Brotli has become a standard in web compression.
Extremely Fast Compression algorithm
Pros of lz4
- Extremely fast compression and decompression speeds
- Low memory usage during compression/decompression
- Suitable for real-time applications and scenarios requiring quick processing
Cons of lz4
- Lower compression ratio compared to Brotli
- Less effective for compressing text-based data
- Not as widely supported in web browsers for content encoding
Code Comparison
lz4
char* compressed = LZ4_compress_default(src, dst, srcSize, dstCapacity);
int decompressedSize = LZ4_decompress_safe(compressed, decompressed, compressedSize, maxDecompressedSize);
Brotli
size_t encoded_size = BrotliEncoderMaxCompressedSize(input_size);
uint8_t* encoded = malloc(encoded_size);
BrotliEncoderCompress(quality, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, input_size, input, &encoded_size, encoded);
Summary
lz4 excels in speed and efficiency, making it ideal for scenarios where rapid compression/decompression is crucial. However, Brotli generally achieves better compression ratios, especially for text-based data, and has broader support in web environments. The choice between the two depends on the specific use case, with lz4 being preferable for real-time applications and Brotli for optimizing file sizes and web content delivery.
A massively spiffy yet delicately unobtrusive compression library.
Pros of zlib
- Widely adopted and supported across many platforms and programming languages
- Faster compression and decompression speeds for small to medium-sized files
- Smaller memory footprint during compression and decompression operations
Cons of zlib
- Generally achieves lower compression ratios compared to Brotli
- Less effective for compressing larger files or datasets
- Older algorithm with fewer modern optimizations
Code Comparison
zlib compression example:
z_stream strm;
deflateInit(&strm, Z_DEFAULT_COMPRESSION);
deflate(&strm, Z_FINISH);
deflateEnd(&strm);
Brotli compression example:
BrotliEncoderState* state = BrotliEncoderCreateInstance(NULL, NULL, NULL);
BrotliEncoderCompressStream(state, BROTLI_OPERATION_FINISH, &available_in, &next_in, &available_out, &next_out, &total_out);
BrotliEncoderDestroyInstance(state);
Both libraries offer simple APIs for compression and decompression, but Brotli's API is more modern and provides finer control over compression parameters. zlib's API is more straightforward and requires less setup, which can be beneficial for simpler use cases.
A fast compressor/decompressor
Pros of Snappy
- Faster compression and decompression speeds
- Lower memory usage during compression
- Simpler implementation, easier to integrate into projects
Cons of Snappy
- Lower compression ratio compared to Brotli
- Less suitable for compressing smaller files
- Not as widely supported in web browsers and servers
Code Comparison
Snappy:
char* output = new char[snappy::MaxCompressedLength(input_length)];
size_t output_length;
snappy::RawCompress(input, input_length, output, &output_length);
Brotli:
size_t output_length = BrotliEncoderMaxCompressedSize(input_length);
uint8_t* output = new uint8_t[output_length];
BROTLI_BOOL result = BrotliEncoderCompress(
BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE,
input_length, input, &output_length, output);
Snappy focuses on speed and simplicity, making it ideal for scenarios where compression and decompression need to be performed quickly, such as in-memory data compression or real-time data transfer. Brotli, on the other hand, offers better compression ratios at the cost of slower compression speeds, making it more suitable for static content compression and scenarios where file size reduction is prioritized over speed.
Zopfli Compression Algorithm is a compression library programmed in C to perform very good, but slow, deflate or zlib compression.
Pros of Zopfli
- Achieves higher compression ratios for static content
- Compatible with existing DEFLATE decoders
- Useful for compressing rarely-changed assets
Cons of Zopfli
- Significantly slower compression speed
- Not suitable for real-time compression scenarios
- Limited to DEFLATE-based formats (gzip, PNG)
Code Comparison
Zopfli:
size_t c_size = 0;
unsigned char* compressed = NULL;
ZopfliCompress(&options, ZopfliFormat::ZOPFLI_FORMAT_GZIP,
input, input_size, &compressed, &c_size);
Brotli:
size_t encoded_size = BrotliEncoderMaxCompressedSize(input_size);
uint8_t* encoded = malloc(encoded_size);
BrotliEncoderCompress(quality, BROTLI_DEFAULT_WINDOW,
BROTLI_DEFAULT_MODE, input_size, input,
&encoded_size, encoded);
Zopfli focuses on achieving maximum compression ratios for static content, making it ideal for compressing assets that don't change frequently. It's compatible with existing DEFLATE decoders, ensuring broad compatibility. However, Zopfli's compression speed is much slower compared to Brotli, making it unsuitable for real-time compression scenarios.
Brotli, on the other hand, offers a better balance between compression ratio and speed. It introduces a new compression format, which requires specific decoder support but achieves better compression ratios than gzip in most cases while maintaining reasonable compression speeds.
Optimized Go Compression Packages
Pros of compress
- Supports multiple compression algorithms (Zstandard, LZ4, Snappy) in addition to gzip and deflate
- Generally faster compression and decompression speeds for most use cases
- More actively maintained with frequent updates and improvements
Cons of compress
- Slightly larger compressed file sizes compared to Brotli in some scenarios
- Less widespread adoption and browser support compared to Brotli
- May require additional configuration to achieve optimal compression ratios
Code Comparison
compress:
compressed := bytes.Buffer{}
w := zstd.NewWriter(&compressed)
w.Write(data)
w.Close()
Brotli:
compressed := bytes.Buffer{}
w := brotli.NewWriter(&compressed)
w.Write(data)
w.Close()
Both libraries offer similar ease of use for basic compression tasks. The main difference lies in the specific compression algorithms and their performance characteristics.
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
Introduction
Brotli is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding and 2nd order context modeling, with a compression ratio comparable to the best currently available general-purpose compression methods. It is similar in speed with deflate but offers more dense compression.
The specification of the Brotli Compressed Data Format is defined in RFC 7932.
Brotli is open-sourced under the MIT License, see the LICENSE file.
Please note: brotli is a "stream" format; it does not contain meta-information, like checksums or uncompresssed data length. It is possible to modify "raw" ranges of the compressed stream and the decoder will not notice that.
Build instructions
Vcpkg
You can download and install brotli using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install brotli
The brotli 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.
Bazel
See Bazel
CMake
The basic commands to build and install brotli are:
$ mkdir out && cd out
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=./installed ..
$ cmake --build . --config Release --target install
You can use other CMake configuration.
Python
To install the latest release of the Python module, run the following:
$ pip install brotli
To install the tip-of-the-tree version, run:
$ pip install --upgrade git+https://github.com/google/brotli
See the Python readme for more details on installing from source, development, and testing.
Contributing
We glad to answer/library related questions in brotli mailing list.
Regular issues / feature requests should be reported in issue tracker.
For reporting vulnerability please read SECURITY.
For contributing changes please read CONTRIBUTING.
Benchmarks
- Squash Compression Benchmark / Unstable Squash Compression Benchmark
- Large Text Compression Benchmark
- Lzturbo Benchmark
Related projects
Disclaimer: Brotli authors take no responsibility for the third party projects mentioned in this section.
Independent decoder implementation by Mark Adler, based entirely on format specification.
JavaScript port of brotli decoder. Could be used directly via npm install brotli
Hand ported decoder / encoder in haxe by Dominik Homberger. Output source code: JavaScript, PHP, Python, Java and C#
7Zip plugin
Dart native bindings
Dart compression framework with fast FFI-based Brotli implementation with ready-to-use prebuilt binaries for Win/Linux/Mac
Top Related Projects
Zstandard - Fast real-time compression algorithm
Extremely Fast Compression algorithm
A massively spiffy yet delicately unobtrusive compression library.
A fast compressor/decompressor
Zopfli Compression Algorithm is a compression library programmed in C to perform very good, but slow, deflate or zlib compression.
Optimized Go Compression Packages
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