ArticleslgStudy

computer science

Deflate

Deflate is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Deflate rather than just read about it. In short: In computing, Deflate (stylized as DEFLATE) is a lossless data compression algorithm that uses a combination of LZ77 and Huffman coding. It was designed by Phil Katz, for version 2 of his PKZIP archiving tool.

Key takeaways

  • Deflate belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Deflate to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Deflate from memory before moving on to harder problems.

Reference excerpt

In computing, Deflate (stylized as DEFLATE) is a lossless data compression algorithm that uses a combination of LZ77 and Huffman coding. It was designed by Phil Katz, for version 2 of his PKZIP archiving tool. Deflate was later specified in Request for Comments (RFC) 1951 (1996). Katz also designed the original algorithm used to construct Deflate streams. This algorithm received software patent U.S. patent 5,051,745, assigned to PKWare, Inc. As stated in the RFC document, an algorithm producing Deflate files was widely thought to be implementable in a manner not covered by patents. This led to its widespread use; for example, in the zlib data format, gzip file format, Portable Network Graphics (PNG) image file, and the ZIP file format for which Katz originally designed it. The patent has since expired.

Block structure Deflate compression takes any sequence of bytes and outputs a sequence of blocks (commonly called Deflate stream). Deflate decompression takes a sequence of blocks and outputs the original sequence of bytes. Endianness is little-endian. Bit 0 is the least significant bit in a byte. Each block has a 3-bit header with two fields:

BFINAL (first bit): 1 if this is the last block in the sequence, else 0. BTYPE (next two bits): Block type 00: No compression (sometimes called stored). Any bits up to the next byte boundary are ignored. The rest of the block consists of 16-bit LEN, 16-bit NLEN (one's complement of LEN), and LEN bytes of uncompressed data, i.e. up to 65,535 (216 − 1) bytes. Useful for incompressible data (e.g. high-entropy, random, or already compressed), adding minimal overhead (i.e. ~5 bytes per block). 01: A static Huffman compressed block, using a pre-agreed Huffman tree defined in the RFC. 10: A dynamic Huffman compressed block, complete with the Huffman table supplied. 11: Reserved (error). Most compressible data will end up being encoded using method 10, the dynamic Huffman encoding, which produces an optimized Huffman tree customized for each block of data individually. Instructions to generate the necessary Huffman tree immediately follow the block header. The static Huffman option is used for short messages, where the fixed saving gained by omitting the tree outweighs the percentage compression loss due to using a non-optimal (thus, not technically Huffman) code. Compression is achieved through two steps:

Matching and replacing duplicate strings with pointers Replacing symbols with new, weighted symbols based on use frequency

Duplicate string elimination

Within compressed blocks, if a duplicate series of bytes is spotted (a repeated string), then a back-reference is inserted, linking to the prior location of that identical string instead. An encoded match to an earlier string consists of an 8-bit length (3–258 bytes) and a 15-bit distance (1–32,768 bytes) to the start of the duplicate. Relative back-references can be made across any number of blocks, as long as the distance appears within the last 32 KiB of uncompressed data decoded (termed the sliding window). If the distance is less than the length, the duplicate overlaps itself, indicating repetition. For example, a run of 10 identical bytes can be encoded as one byte, followed by a duplicate of length 9, starting with the prior byte. Searching the preceding text for duplicate substrings is the most computationally expensive part of the Deflate algorithm, and the operation which compression level settings affect.

Bit reduction

The second compression stage consists of replacing commonly used symbols with shorter representations and less commonly used symbols with longer representations. The method used is Huffman coding which creates an unprefixed tree of non-overlapping intervals, where the length of each sequence is inversely proportional to the logarithm of the probability of that symbol needing to be encoded. The more likely it is that a symbol has to be encoded, the shorter its bit-sequence will be. A tree is created, containing space for 288 symbols:

0–255: represent the literal bytes/symbols 0–255. 256: end of block – stop processing if last block, otherwise start processing next block. 257–285: combined with extra-bits, a match length of 3–258 bytes. 286, 287: not used, reserved and illegal but still part of the tree. A match length code will always be followed by a distance code. Based on the distance code read, further "extra" bits may be read in order to produce the final distance. The distance tree contains space for 32 symbols:

0–3: distances 1–4 4–5: distances 5–8, 1 extra bit 6–7: distances 9–16, 2 extra bits 8–9: distances 17–32, 3 extra bits ... 26–27: distances 8,193–16,384, 12 extra bits 28–29: distances 16,385–32,768, 13 extra bits 30–31: not used, reserved and illegal but still part of the tree For the match distance symbols 2–29, the number of extra bits can be calculated as ⌊n2⌋−1. The two codes (the 288-symbol length/literal tree and the 32-symbol distance tree) are themselves encoded as canonical Huffman codes by giving the bit length of the code for each symbol. The bit lengths are themselves run-length encoded to produce as compact a representation as possible. As an alternative to including the tree representation, the "static tree" option provides standard fixed Huffman trees. The compressed size using the static trees can be computed using the same statistics (the number of times each symbol appears) as are used to generate the dynamic trees, so it is easy for a compressor to choose whichever is smaller.

Encoder–compressor During the compression stage, it is the encoder that chooses the amount of time spent looking for matching strings. The zlib/gzip reference implementation allows the user to select from a sliding scale of likely resulting compression-level vs. speed of encoding. Options range from 0 (do not attempt compression, just store uncompressed) to 9 representing the maximum capability of the reference implementation in zlib/gzip. Other Deflate encoders have been produced, all of which will also produce a compatible bitstream capable of being decompressed by any existing Deflate decoder. Differing implementations will likely produce variations on the final encoded bit-stream produced. The focus with non-zlib versions of an encoder has normally been to produce a more efficiently compressed and smaller encoded stream.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Deflate

Start with the simplest possible case. Write down what Deflate claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Deflate before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Deflate ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Deflate

In research
Deflate appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Deflate in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Deflate is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data compression, Lossless compression algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Deflate outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Deflate” →

How to study Deflate in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Deflate means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Deflate out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Deflate in simple terms?

In computing, Deflate (stylized as DEFLATE) is a lossless data compression algorithm that uses a combination of LZ77 and Huffman coding. It was designed by Phil Katz, for version 2 of his PKZIP archiving tool.

Why does Deflate matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Deflate?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Deflate.

Tags

  • Data compression
  • Lossless compression algorithms

Keep exploring