File Formats & Media

Lossless Compression

Definition

Rather than deciding what can be thrown away, a lossless coder looks for redundancy it can describe more briefly — repeated byte sequences, long runs of the same value, symbols that occur far more often than others. DEFLATE, the algorithm inside PNG, ZIP and gzip, combines LZ77 back-references with Huffman coding to do both at once. What you get back depends entirely on the input: text, markup, source code and spreadsheets commonly compress two to four times, flat-colour graphics far more, and photographs barely at all, because photographic noise is close to random and there is no pattern to exploit. The result is verifiable — take a checksum before compressing and after decompressing and the two match.

Why It Matters

Anything that has to survive byte for byte needs lossless treatment, and quite a lot does: a signed PDF, a CSV feeding a script, source code, an archive, a legal scan. Lossless is also repeatable in a way lossy never is — compress and decompress a hundred times and nothing changes, where each round of a lossy file degrades it a little further. The cost is that you cannot choose how small the file gets; the data decides. A photograph stored as PNG is often five to ten times the size of a visually identical JPEG, which is exactly the trade-off you accept for a master copy and exactly the one you should not accept for a web page.

How It Works

DEFLATE slides a window over the data — 32 KB in the standard implementation — and replaces anything it has seen before with a short back-reference, then entropy-codes the output so common symbols get short codes. Brotli does the same in a more modern way and ships with a built-in dictionary of common web strings, which is why it typically beats gzip by 15 to 20 percent on HTML, CSS and JavaScript. On the web this usually happens in transit rather than in the file: a browser sends Accept-Encoding listing what it can handle, and the server responds with Content-Encoding gzip or br and the compressed body. Already-compressed formats gain nothing from a second pass — a JPEG, a PNG, an MP4 or a ZIP is usually skipped, because re-compressing it costs CPU and can make it very slightly larger.

Real-World Example

A handbook is published as a small static site on 99helpers at docs-handbook.99helpers.site. The HTML, CSS and JavaScript come to 1.1 MB on disk and about 260 KB over the wire once the edge compresses them, which is most of a second saved on a mobile connection. The images on the same site transfer at their full size, because JPEG and WebP files are already compressed and there is nothing further to squeeze out of them.

Common Mistakes

  • Zipping a folder of JPEGs or MP4s to make it smaller — the archive comes out within a percent or two of the original, since those formats are already compressed
  • Storing photographs as PNG for the website because lossless sounds safer — lossless means faithful, not small, and the visitor pays the difference
  • Assuming compression is happening — check the response for a Content-Encoding header rather than taking it on trust
  • Compressing the same data twice in the pipeline — it burns CPU at both ends and the second pass finds nothing left to exploit

Related Terms

Put a file online in seconds

Drop in a document, an image, a page or a whole static website and share the link — free, with no build step and no server to set up.

Host a file free →