This package provides complete Brotli compression/decompression support for Delphi (Windows 32/64-bit) and Free Pascal (Windows 32/64-bit and Linux 64-bit).
It consists of two units:
libbrotli.pas– Low-level direct bindings to the official Brotli C library (libbrotlicommon,libbrotlienc,libbrotlidec).brotli.pas– High-level stream classes (TBrotliCompressionStream,TBrotliDecompressionStream) and helper functions for easy integration into Delphi/FPC applications.
The package includes pre-compiled static libraries and object files:
- Windows (Delphi/FPC):
.objfiles linked directly via{$L ...} - Linux (FPC):
.astatic libraries - All necessary C sources from the official Brotli release are compiled and included.
Tested platforms:
- Delphi (10.4 Sydney and later) – Windows 32/64-bit
- Lazarus/FPC – Windows 32/64-bit and Linux 64-bit
Delphi on Linux/macOS should work in principle (via static linking), but has not been tested (no FMX Linux environment available).
This unit provides direct Pascal declarations for the entire Brotli C API.
- Full access to encoder and decoder functions (
BrotliEncoderCompress,BrotliDecoderDecompressStream, etc.) - All types, constants, enums, and structures from
brotli/decode.handbrotli/encode.h - Custom memory manager support via callbacks
- Version helpers (
BrotliDecoderVersion,BrotliEncoderVersion,BrotliVersionDecode)
var
Input, Output: PByte;
InSize, OutSize: size_t;
Success: Longint;
begin
InSize := ...;
GetMem(Input, InSize);
// fill Input...
OutSize := BrotliEncoderMaxCompressedSize(InSize);
GetMem(Output, OutSize);
Success := BrotliEncoderCompress(
11, // quality (0-11)
22, // lgwin (10-24)
BROTLI_MODE_GENERIC,
InSize, Input,
@OutSize, Output);
if Success = BROTLI_TRUE then
// Output contains compressed data, OutSize is actual size
else
// compression failed
end;Most users will prefer the higher-level streams in brotli.pas.
This unit provides easy-to-use stream classes similar to TZCompressionStream/TDecompressionStream from ZLib.
Write-only stream that compresses data written to it and outputs to an underlying stream.
constructor Create(Dest: TStream;
Quality: Integer = 11;
LgWin: Integer = 22;
Mode: BrotliEncoderMode = BROTLI_MODE_GENERIC;
AOptions: TBrotliStreamOptions = []);- Quality: 0–11 (higher = better compression, slower). Default 11.
- LgWin: Log2 of window size, 10–24. Default 22.
- Mode:
BROTLI_MODE_GENERIC(default),BROTLI_MODE_TEXT, orBROTLI_MODE_FONT. - AOptions:
brLeaveOpen– do not free the destination stream in destructor.
Write– compresses dataFlush– flushes internal buffers (calls Brotli FLUSH operation)Finish– completes compression (calls Brotli FINISH). Called automatically in destructor.
var
Source, Compressed: TFileStream;
begin
Source := TFileStream.Create('input.txt', fmOpenRead);
Compressed := TFileStream.Create('input.br', fmCreate);
try
with TBrotliCompressionStream.Create(Compressed, 11, 22, BROTLI_MODE_TEXT) do
try
CopyFrom(Source, 0);
Finish; // important before closing
finally
Free;
end;
finally
Source.Free;
end;
end;Read-only stream that decompresses data from an underlying stream.
constructor Create(Source: TStream; AOptions: TBrotliStreamOptions = []);- Supports large window (
BROTLI_PARAM_LARGE_WINDOW) automatically enabled. brLeaveOpen– do not free source stream in destructor.
var
Compressed, Output: TFileStream;
begin
Compressed := TFileStream.Create('input.br', fmOpenRead);
Output := TFileStream.Create('output.txt', fmCreate);
try
with TBrotliDecompressionStream.Create(Compressed) do
try
Output.CopyFrom(Self, 0);
finally
Free;
end;
finally
Output.Free;
end;
end;function GetMaxCompressedSize(const Count: size_t): size_t;
// Returns worst-case compressed size
function BrotliCompressBuf(InBuf: PByte; InBytesCount: size_t;
out OutBuf: PByte; out OutBytesCount: size_t): size_t;
// One-shot compression (quality 11, lgwin 22, generic mode)
// Allocates and returns compressed buffer, raises exception on failure
function BrotliDecompress(const compressedBuffer; compressedBuffSize: size_t;
var DecBuffer: PByte; var decSize: Psize_t): Longint;
// One-shot decompression of entire buffer
// Allocates DecBuffer, returns BROTLI_TRUE/BROTLI_FALSE
function BrotliDecoderVersionString: string;
function BrotliEncoderVersionString: string;
// Human-readable version strings, e.g., "1.0.9"EBrotliError– base classEBrotliCompressionError– compression failuresEBrotliDecompressionError– decompression failures (includes error message from decoder when possible)
- Add both units (
libbrotli.pasandbrotli.pas) to your project. - No external DLLs required – everything is statically linked.
- For FPC on Linux: the
.alibraries are automatically linked via{$linklib ...}directives. - For Delphi on Windows: object files are linked via
{$L ...}.
No additional setup needed.
The Brotli library itself is under the MIT License.
The Pascal bindings and wrapper code are provided under the same permissive terms – feel free to use in any project (commercial or open-source).
Enjoy fast and efficient compression with Brotli in your Delphi and Lazarus applications!