Zlib compress for embedded data
Zlib (RFC 1950) wraps DEFLATE-compressed data in a compact 2-byte header and a 4-byte Adler-32 checksum. It is the format used inside PNG images and PDF streams, by Git objects, by Python’s zlib.compress, PHP’s gzcompress and Java’s Deflater. This tool will zlib compress text and give you the bytes as Base64 or hex.
Example
Input (38 bytes):
Hello world! Hello world! Hello world!Zlib, Base64 encoded:
eJzzSM3JyVcozy/KSVFU8MDFAQAGNw1YThe same data at level 9 in hex: 78daf348cdc9c95728cf2fca495154f0c0c5010006370d58. Note the first byte 78: every zlib stream starts with it (for the default 32 KB window). The second byte encodes the compression level hint — 9C for the default and DA for the best compression — which is why zlib data in Base64 usually starts with eJ.
Zlib vs. gzip vs. raw DEFLATE
| Format | Header / trailer | Typical use |
|---|---|---|
| Raw DEFLATE | None | ZIP entries, WebSocket permessage-deflate |
| Zlib | 2 bytes + Adler-32 (6 bytes) | PNG, PDF, Git, application data |
| Gzip | 10 bytes + CRC-32 and size (18 bytes) | .gz files, HTTP compression |
All three share the same compressed core, so compression ratios are nearly identical; zlib is simply smaller in overhead, which matters for short messages. When an HTTP server says Content-Encoding: deflate, it is supposed to mean zlib format, though some older servers sent raw DEFLATE.
When compression makes data bigger
Zlib always adds 6 bytes of header and checksum, and DEFLATE needs repetition to save space. Very short inputs therefore grow:
"Hi" (2 bytes)
zlib: 78 9c f3 c8 04 00 00 fb 00 b2 (10 bytes)
Base64: eJzzyAQAAPsAsg== (16 characters)
The last four bytes of any zlib stream are the Adler-32 checksum of the original data. In the example above, 06370d58 at the end of the level 9 output is the Adler-32 of the "Hello world!" text, which the decompressor recomputes to detect corruption. As a rule of thumb, compress only data of a few hundred bytes or more, or data with obvious repetition such as JSON with repeated keys; for tiny strings, Base64 alone is shorter.
Choosing a level
Level 6 is the zlib default. Level 9 squeezes out a little more at higher CPU cost, and level 1 is fastest. For text under a few kilobytes the differences are small; the status bar shows the exact sizes before and after compression.
Typical uses
- Creating test data for code that calls a zlib or inflate function.
- Shrinking large JSON or text before storing it in a cookie, a database field or a queue message, together with Base64.
- Reproducing the output of PHP’s gzcompress or Python’s zlib.compress to debug an integration.
In code: Python zlib.compress(data, 9), Node.js zlib.deflateSync(buf), PHP gzcompress($s, 9). Base64-encode the result if it has to travel as text.
Decompress with Zlib Decompress, or use Gzip Compress if you need a .gz file. Everything runs in your browser.