---
title: "Identify and peel"
description: "Which format made these bytes. Ranked by magic numbers and headers and checksums and every layer peeled off in one call"
canonical_url: "https://compressions.agntn.dev/guide/identify"
---
# Identify and peel

> Which format made these bytes. Ranked by magic numbers and headers and checksums and every layer peeled off in one call

## Try it, don't guess it

`identify` doesn't pattern match and hope. It tries every container whose start fits, decompresses for real, and ranks what worked:

```ts
import { identify } from "@agntn/compressions";

identify(gz);
// [{ format: "deflate", container: "gzip", confidence: 100, confirmed: true,
//    reasons: ["starts with its magic number", "checksum matches",
//              "decodes to the last byte", "decompresses to readable text"], bytes, details }]
```

Each candidate carries the decompressed `bytes`, the `details` and the reasons behind its score. The confidence ranks, it isn't a probability.

## What counts

<table>
<thead>
  <tr>
    <th>
      Evidence
    </th>
    
    <th>
      Points
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      starts with its magic number
    </td>
    
    <td>
      50
    </td>
  </tr>
  
  <tr>
    <td>
      header fields are valid (zlib, .lzma)
    </td>
    
    <td>
      25
    </td>
  </tr>
  
  <tr>
    <td>
      checksum matches
    </td>
    
    <td>
      30
    </td>
  </tr>
  
  <tr>
    <td>
      decodes to the last byte
    </td>
    
    <td>
      10
    </td>
  </tr>
  
  <tr>
    <td>
      bytes follow the stream
    </td>
    
    <td>
      minus 15
    </td>
  </tr>
  
  <tr>
    <td>
      decompresses to readable text
    </td>
    
    <td>
      10
    </td>
  </tr>
  
  <tr>
    <td>
      comes out longer than it went in
    </td>
    
    <td>
      5
    </td>
  </tr>
</tbody>
</table>

A container with a magic number is tried only when the bytes start with it. zlib and .lzma have no real magic, so their headers get checked instead: zlib's two bytes carry their own check, .lzma's properties and sizes have to make sense. Raw deflate and brotli have neither. They get tried on everything, and they only count when they decode every single byte and give something back, since random bytes decode as deflate more often than you'd think. Filling the whole limit counts too, as an unconfirmed guess.

`confirmed` marks a candidate backed by more than luck: a magic number, or a checked header with a checksum or readable text behind it.

## When nothing fits

`identify` returns an empty list. That's an answer, not a failure. Plain text isn't compressed. And when the bytes start like an archive this package doesn't open, `archiveOf` names it:

```ts
archiveOf(zipBytes);  // "a ZIP archive"
```

ZIP, 7-Zip, RAR, tar, cabinet files, lzip and PNG are on that list. A ZIP entry's data is usually raw deflate, so once you've cut it out, `decompress("deflate", entry)` reads it.

## Peel

Puzzles love nesting. `peel` takes off one layer after another while each is confirmed:

```ts
import { peel } from "@agntn/compressions";

peel(compress("bzip2", compress("lzma", compress("deflate", text, { container: "gzip" }), { container: "xz" })));
// bzip2 · bzip2, 90, confirmed
// lzma  · xz,    90, confirmed
// deflate · gzip, 100, confirmed
```

The list runs outermost first, and the last layer's `bytes` is what's at the bottom. An unconfirmed best guess ends the list, marked `confirmed: false`, so you see it without the next layer building on it. A layer cut at the limit ends it too. `depth` caps the layers, 10 by default.

The tool does the same with `peel: true`, and hands the innermost bytes back in base64. On the CLI, `compressions identify --peel file` lists the layers on stderr and writes the bottom to stdout.

## Too big to finish

Sometimes the stream is fine. It's just huge. A gzip that inflates past `limit` is still a gzip, so `identify` keeps it and cuts at the line:

```ts
identify(hugeGzip, { limit: 4096 });
// [{ format: "deflate", container: "gzip", confidence: 65, confirmed: true, limited: true,
//    reasons: ["starts with its magic number", "output passes the limit of 4096 bytes, cut there",
//              "decompresses to readable text", "comes out longer than it went in"], bytes, details }]
```

`bytes` holds the first 4096 bytes. Where did the checksum points go? The checksum sits at the end, and nobody read the end. A magic number still confirms the stream. A zlib header alone doesn't. Want those bytes for real? `decompress` with `partial: true` hands them over.

`peel` stops at a cut layer, because a prefix of a stream isn't a stream. And an empty list keeps meaning one thing: nothing fit, not something too big.


## Sitemap

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