---
title: "Containers"
description: "gzip and zlib and raw deflate are one compression in three wrappers. Why that's an option and what each wrapper adds"
canonical_url: "https://compressions.agntn.dev/guide/containers"
---
# Containers

> gzip and zlib and raw deflate are one compression in three wrappers. Why that's an option and what each wrapper adds

## One compression, several wrappers

A container is the packaging around a compressed stream: a magic number so tools recognize it, a header with metadata, a checksum at the end. The compression inside doesn't change. gzip and zlib carry the exact same deflate data, they just wrap it differently.

So here a container is an option of its format, never a format of its own:

```ts
compress("deflate", data);                          // raw deflate, RFC 1951
compress("deflate", data, { container: "zlib" });   // RFC 1950
compress("deflate", data, { container: "gzip" });   // RFC 1952
```

The first container is the default. Asking for `gzip` as a format gets you an error that names `deflate` and the container, so nobody has to guess.

## Every container

<table>
<thead>
  <tr>
    <th>
      Format
    </th>
    
    <th>
      Container
    </th>
    
    <th>
      Magic
    </th>
    
    <th>
      Header
    </th>
    
    <th>
      Check
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      deflate
    </td>
    
    <td>
      <code>
        raw
      </code>
    </td>
    
    <td>
      none
    </td>
    
    <td>
      none
    </td>
    
    <td>
      none
    </td>
  </tr>
  
  <tr>
    <td>
      deflate
    </td>
    
    <td>
      <code>
        zlib
      </code>
    </td>
    
    <td>
      none, two header bytes with a check
    </td>
    
    <td>
      method, window, level hint
    </td>
    
    <td>
      Adler-32
    </td>
  </tr>
  
  <tr>
    <td>
      deflate
    </td>
    
    <td>
      <code>
        gzip
      </code>
    </td>
    
    <td>
      <code>
        1f 8b 08
      </code>
    </td>
    
    <td>
      flags, mtime, OS, optional name and comment
    </td>
    
    <td>
      CRC-32 and size
    </td>
  </tr>
  
  <tr>
    <td>
      bzip2
    </td>
    
    <td>
      <code>
        bzip2
      </code>
    </td>
    
    <td>
      <code>
        BZh
      </code>
      
       and the block size digit
    </td>
    
    <td>
      per block magic and CRC
    </td>
    
    <td>
      CRC-32/BZIP2 per block and per stream
    </td>
  </tr>
  
  <tr>
    <td>
      lzma
    </td>
    
    <td>
      <code>
        alone
      </code>
    </td>
    
    <td>
      none, the properties byte is usually <code>
        5d
      </code>
    </td>
    
    <td>
      properties, dictionary size, size
    </td>
    
    <td>
      none
    </td>
  </tr>
  
  <tr>
    <td>
      lzma
    </td>
    
    <td>
      <code>
        xz
      </code>
    </td>
    
    <td>
      <code>
        fd 37 7a 58 5a 00
      </code>
    </td>
    
    <td>
      stream flags, block headers with filters
    </td>
    
    <td>
      CRC-32, CRC-64 or SHA-256 per block, CRC-32 everywhere else
    </td>
  </tr>
  
  <tr>
    <td>
      zstd
    </td>
    
    <td>
      <code>
        zstd
      </code>
    </td>
    
    <td>
      <code>
        28 b5 2f fd
      </code>
    </td>
    
    <td>
      window, dictionary id, content size
    </td>
    
    <td>
      XXH64, low 32 bits
    </td>
  </tr>
  
  <tr>
    <td>
      brotli
    </td>
    
    <td>
      <code>
        brotli
      </code>
    </td>
    
    <td>
      none
    </td>
    
    <td>
      window bits
    </td>
    
    <td>
      none
    </td>
  </tr>
  
  <tr>
    <td>
      lz4
    </td>
    
    <td>
      <code>
        frame
      </code>
    </td>
    
    <td>
      <code>
        04 22 4d 18
      </code>
    </td>
    
    <td>
      flags, block size, content size
    </td>
    
    <td>
      XXH32 of the header, blocks and content
    </td>
  </tr>
  
  <tr>
    <td>
      lz4
    </td>
    
    <td>
      <code>
        legacy
      </code>
    </td>
    
    <td>
      <code>
        02 21 4c 18
      </code>
    </td>
    
    <td>
      none
    </td>
    
    <td>
      none
    </td>
  </tr>
  
  <tr>
    <td>
      lzw
    </td>
    
    <td>
      <code>
        compress
      </code>
    </td>
    
    <td>
      <code>
        1f 9d
      </code>
    </td>
    
    <td>
      maximum code width, block mode
    </td>
    
    <td>
      none
    </td>
  </tr>
</tbody>
</table>

## What each one adds

### gzip

It stores an optional file name and comment, a modification time and the OS. `compress` writes `name` and `mtime` when you pass them and OS 255, unknown, so the bytes don't depend on the machine. `decompress` reads every member, as `gunzip` does.

### zlib

Two header bytes and an Adler-32. The second byte carries a level hint, which `decompress` reports as `fastest`, `fast`, `default` or `best`. A stream that needs a preset dictionary is refused with `UnsupportedError`.

### xz

The elaborate one. Blocks, each with its own filter chain and check, an index of the blocks and a footer that points back at it. This package reads LZMA2 with the delta and x86 filters before it, and every check type it knows how to compute. It writes one block with LZMA2 and the check you pick, CRC-64 by default.

### .lzma

`alone` here. It's what LZMA Utils wrote before xz: thirteen header bytes and the raw range coded stream. No checksum, so a damaged file decodes into garbage instead of an error. `xz --format=lzma` still writes it.

### LZ4 frame

It checks itself three ways: a byte of XXH32 closes the header, and the frame can carry XXH32 after every block and after the content. `compress` writes the content checksum, like the `lz4` command.

### LZ4 legacy

It's the old `lz4 -l` format. Blocks of up to 8 MiB, no checksum, no end mark. Linux still boots kernels packed in it.


## Sitemap

See the full [sitemap](https://compressions.agntn.dev/sitemap.md) for all pages.
