algorith.txt 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. Zip's deflation algorithm is a variation of LZ77 (Lempel-Ziv 1977, see
  2. reference below). It finds duplicated strings in the input data. The
  3. second occurrence of a string is replaced by a pointer to the previous
  4. string, in the form of a pair (distance, length). Distances are
  5. limited to 32K bytes, and lengths are limited to 258 bytes. When a
  6. string does not occur anywhere in the previous 32K bytes, it is
  7. emitted as a sequence of literal bytes. (In this description,
  8. 'string' must be taken as an arbitrary sequence of bytes, and is not
  9. restricted to printable characters.)
  10. Literals or match lengths are compressed with one Huffman tree, and
  11. match distances are compressed with another tree. The trees are stored
  12. in a compact form at the start of each block. The blocks can have any
  13. size (except that the compressed data for one block must fit in
  14. available memory). A block is terminated when zip determines that it
  15. would be useful to start another block with fresh trees. (This is
  16. somewhat similar to compress.)
  17. Duplicated strings are found using a hash table. All input strings of
  18. length 3 are inserted in the hash table. A hash index is computed for
  19. the next 3 bytes. If the hash chain for this index is not empty, all
  20. strings in the chain are compared with the current input string, and
  21. the longest match is selected.
  22. The hash chains are searched starting with the most recent strings, to
  23. favor small distances and thus take advantage of the Huffman encoding.
  24. The hash chains are singly linked. There are no deletions from the
  25. hash chains, the algorithm simply discards matches that are too old.
  26. To avoid a worst-case situation, very long hash chains are arbitrarily
  27. truncated at a certain length, determined by a runtime option (zip -1
  28. to -9). So zip does not always find the longest possible match but
  29. generally finds a match which is long enough.
  30. zip also defers the selection of matches with a lazy evaluation
  31. mechanism. After a match of length N has been found, zip searches for a
  32. longer match at the next input byte. If a longer match is found, the
  33. previous match is truncated to a length of one (thus producing a single
  34. literal byte) and the longer match is emitted afterwards. Otherwise,
  35. the original match is kept, and the next match search is attempted only
  36. N steps later.
  37. The lazy match evaluation is also subject to a runtime parameter. If
  38. the current match is long enough, zip reduces the search for a longer
  39. match, thus speeding up the whole process. If compression ratio is more
  40. important than speed, zip attempts a complete second search even if
  41. the first match is already long enough.
  42. The lazy match evaluation is not performed for the fastest compression
  43. modes (speed options -1 to -3). For these fast modes, new strings
  44. are inserted in the hash table only when no match was found, or
  45. when the match is not too long. This degrades the compression ratio
  46. but saves time since there are both fewer insertions and fewer searches.
  47. Jean-loup Gailly
  48. jloup@chorus.fr
  49. References:
  50. [LZ77] Ziv J., Lempel A., "A Universal Algorithm for Sequential Data
  51. Compression", IEEE Transactions on Information Theory", Vol. 23, No. 3,
  52. pp. 337-343.
  53. APPNOTE.TXT documentation file in PKZIP 1.93a. It is available by
  54. ftp in ftp.cso.uiuc.edu:/pc/exec-pc/pkz193a.exe [128.174.5.59]
  55. 'Deflate' Compressed Data Format Specification:
  56. ftp://ftp.uu.net/pub/archiving/zip/doc/deflate-1.1.doc