Compression
gzip, deflate and zlib are built into cljgo — no library to install, and they work the same in the REPL and inside a compiled 6 MB binary.
Start small: shrink a string, get it back
Section titled “Start small: shrink a string, get it back”(require '[cljg.compress :as z])
(def packed (z/gzip "hello, hello, hello, hello"))
(z/gunzip packed {:as :string})Output:
hello, hello, hello, hellogzip returns compressed bytes; gunzip reverses it. {:as :string}
says “give me text back, not bytes”.
See it actually working
Section titled “See it actually working”Repetitive data compresses dramatically. Watch the byte counts:
(def report (apply str (repeat 200 "quarterly numbers ")))
(count report)(count (z/gzip report))Output:
3600673600 characters in, 67 bytes out — because gzip notices the repetition. This is why compressed HTTP responses and log archives are small.
The other codecs
Section titled “The other codecs”Same shape, different algorithm — deflate/inflate and
zlib-compress/zlib-decompress:
(= report (z/inflate (z/deflate report) {:as :string}))Output:
trueWhich do I pick?
Section titled “Which do I pick?”- gzip — files and HTTP; the one you usually want
- deflate — the raw algorithm, smallest framing
- zlib — when another system says “zlib format”
All three round-trip anything you put in. For big inputs there are
streaming versions (gunzip-stream, …) that decompress as you read —
they appear again in the chapter on streams.
Questions or feedback on this page? Comment below with your GitHub account — comments are public and live in the project's GitHub Discussions.