Compression
This page documents the Brotli and ZLib compression support in Capy.
Code snippets assume using namespace boost::capy; is in effect.
|
Overview
Capy provides wrappers for two compression libraries:
| Library | Use Case |
|---|---|
Brotli |
High compression ratio, good for web content |
ZLib |
Wide compatibility, DEFLATE/gzip/zlib formats |
Both are accessed through service objects installed in a datastore.
Brotli
Brotli is a modern compression algorithm offering excellent compression ratios, especially for text content.
Setup
#include <boost/capy/brotli.hpp>
#include <boost/capy/datastore.hpp>
datastore ctx;
// Install encoder and decoder services
auto& encoder = brotli::install_encode_service(ctx);
auto& decoder = brotli::install_decode_service(ctx);
Encoding
std::vector<char> input = get_data();
std::vector<char> output;
// Compress
brotli::encode_result result = encoder.encode(
input.data(), input.size(),
output
);
if (result.ec)
handle_error(result.ec);
ZLib
ZLib implements the DEFLATE algorithm used in gzip, zlib, and raw formats.
Setup
#include <boost/capy/zlib.hpp>
#include <boost/capy/datastore.hpp>
datastore ctx;
// Install deflate (compress) and inflate (decompress) services
auto& deflate_svc = zlib::install_deflate_service(ctx);
auto& inflate_svc = zlib::install_inflate_service(ctx);
Compression (Deflate)
std::vector<char> input = get_data();
std::vector<char> output;
// Compress with default settings
zlib::deflate_result result = deflate_svc.deflate(
input.data(), input.size(),
output
);
if (result.ec)
handle_error(result.ec);
Decompression (Inflate)
std::vector<char> compressed = get_compressed();
std::vector<char> output;
// Decompress
zlib::inflate_result result = inflate_svc.inflate(
compressed.data(), compressed.size(),
output
);
if (result.ec)
handle_error(result.ec);
Compression Options
// Compression level (0-9, higher = better compression, slower)
zlib::compression_level level = zlib::compression_level::best;
// Compression strategy
zlib::compression_strategy strategy = zlib::compression_strategy::filtered;
// Flush mode
zlib::flush flush = zlib::flush::sync_flush;
Error Handling
Both libraries use error codes from their respective error categories:
if (result.ec == brotli::error::invalid_input)
{
// Brotli-specific error
}
if (result.ec == zlib::error::data_error)
{
// ZLib-specific error
}
// Generic error handling
if (result.ec)
{
std::cerr << "Compression failed: " << result.ec.message() << "\n";
}
Streaming
For large data that doesn’t fit in memory, use streaming APIs:
zlib::stream stream;
// Process in chunks
while (has_more_input())
{
auto chunk = get_next_chunk();
auto result = stream.deflate_chunk(
chunk.data(), chunk.size(),
output_buffer,
zlib::flush::no_flush
);
// Handle partial output...
}
// Finish the stream
auto result = stream.deflate_finish(output_buffer);
When to Use Each
Use Brotli when:
-
Compression ratio is important
-
Content is text-heavy (HTML, CSS, JS)
-
Decompression speed is acceptable
Use ZLib when:
-
Compatibility is important (gzip is universal)
-
Fast decompression is needed
-
Memory usage must be minimal
Summary
| Library | Header | Purpose |
|---|---|---|
Brotli |
|
High-ratio compression |
ZLib |
|
DEFLATE/gzip/zlib compression |
Next Steps
-
Containers — Service container (
datastore) -
Introduction — Return to overview