Skip to main content

zlib

  • CMF
    • 0-3 CM Compression method
      • 8 -> deflate
    • 4-7 CINFO Compression info
      • CM=8 LZ77 window size, CINFO=7 -> 32K window size
  • FLG
    • 0-4 - FCHECK (check bits for CMF and FLG)
    • 5 - FDICT (preset dictionary)
    • 6-7 - FLEVEL (compression level)
      • 0 - fastest
      • 1 - fast
      • 2 - default
      • 3 - maximum compression, slowest algorithm

file /tmp/data
# /tmp/data: zlib compressed data
printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" | cat - /tmp/data | gzip -dc > /tmp/out
cat /tmp/out

# pigz 并行压缩
brew install pigz
pigz -z test
pigz -d -z test.zz

# openssl zlib -d -in /tmp/data

# qpdf
zlib-flate -uncompress < FILE

Header

LZLIBGZIPNotes
178 011F 8BNo Compression/low
278 5E1F 8BFast Compression
378 5E1F 8B
478 5E1F 8B
578 5E1F 8B
678 9C1F 8BDefault Compression
778 DA1F 8BBest Compression
878 DA1F 8B
978 DA1F 8B
  • CMF -Compression Method and flags
bits 0 to 3  CM     Compression method
bits 4 to 7 CINFO Compression info
  • CM = 8
    • Deflate
    • window size up to 32K
    • used by gzip and PNG
    • CINFO - base-2 logarithm of the LZ77 window size, minus eight
      • CINFO=7 indicates a 32K window size
      • 因此大多前缀都是 78
  • CM = 15
    • reserved
bits 0 to 4  FCHECK  (check bits for CMF and FLG)
bit 5 FDICT (preset dictionary)
bits 6 to 7 FLEVEL (compression level)
 +---+---+
|CMF|FLG| (2 bytes - Defines the compression mode - More details below)
+---+---+
+---+---+---+---+
| DICTID | (4 bytes. Present only when FLG.FDICT is set.) - Mostly not set
+---+---+---+---+
+=====================+
|...compressed data...| (variable size of data)
+=====================+
+---+---+---+---+
| ADLER32 | (4 bytes of checksum)
+---+---+---+---+