tar, gzip, bzip2, xz and Android backups
tar (tape archive) puts files one after the other, each after a header that describes it; it doesn't compress. gzip, bzip2 and xz compress one stream of bytes, so a .tar.gz (.tgz), .tar.bz2 (.tbz) or .tar.xz (.txz) is a tar inside one of them. This page describes them as the Tar tool reads them, and Android's backup format, which is a tar too.
- iOS: a sysdiagnose is a
.tar.gz of logs, crash reports and system state; some app bundles and SDKs ship tarballs.
- Android:
adb backup writes an .ab file (a header, then a zlib-compressed tar); bug reports and OTA tools use tar and gzip; .gz assets appear in APKs.
Contents
- tar: blocks and headers
- The header fields
- Entry types
- v7, ustar, GNU and pax
- gzip
- bzip2
- xz and .lzma
- Android backups
- What to look for
- What the tool shows, and its limits
- The TarArchive library
- The compression and AndroidBackup libraries
1tar: blocks and headers
A tar archive is a sequence of 512-byte blocks. Each entry is a header block, then its data padded with zeros to a whole number of blocks. Two blocks of zeros end the archive; tar programs write in records of 20 blocks (10 KB), so more zeros usually follow. There is no index: to list an archive, a reader walks from header to header, skipping each entry's data.
2The header fields
Numbers are octal ASCII digits, ended by a space or a 0 byte (GNU tar stores numbers too large for the field in binary: the first byte has its high bit set).
| Offset | Size | Field |
| 0 | 100 | name (the path) |
| 100 · 108 · 116 | 8 each | mode (permission bits), owner id, group id |
| 124 · 136 | 12 each | size of the data, modification time (seconds since 1970) |
| 148 | 8 | checksum: the sum of the header's bytes, counting this field as spaces |
| 156 | 1 | type flag (below) |
| 157 | 100 | link name (the target of a link) |
| 257 · 263 | 6 · 2 | ustar: magic ustar\0 and version 00 (GNU tar: ustar and \0) |
| 265 · 297 | 32 each | ustar: owner and group names |
| 329 · 337 | 8 each | ustar: device major and minor numbers |
| 345 | 155 | ustar: a prefix for the name (the path is prefix + "/" + name) |
3Entry types
| Flag | Type |
| 0 (or \0) | regular file |
| 1 | hard link: the same file as an earlier entry (no data) |
| 2 | symbolic link |
| 3 · 4 | character and block devices |
| 5 · 6 · 7 | folder, FIFO (named pipe), contiguous file |
| x · g | pax extended header for the next entry, and global header for all that follow |
| L · K | GNU: the long name, or long link name, of the next entry |
| S · D · M · V | GNU: sparse file, folder dump, multi-volume continuation, volume label |
5gzip
A gzip file (RFC 1952) is one or more members, each:
| Bytes | Field |
| 2 | ID 1F 8B |
| 1 | method: 8 = DEFLATE |
| 1 | flags: 1 text, 2 header CRC, 4 extra field, 8 file name, 16 comment |
| 4 · 1 · 1 | modification time (little-endian), extra flags (2 = slowest, best; 4 = fastest), operating system (0 FAT, 3 Unix, 7 Macintosh, 11 NTFS, 255 unknown) |
| optional | extra field (a length, then subfields of a 2-letter id and a length: BC in BGZF, RA in dictzip), the original file name and a comment (Latin-1, 0-terminated), a CRC-16 of the header |
| … | the DEFLATE data |
| 4 · 4 | CRC-32 of the uncompressed data, and its length modulo 232 |
Members decompress to the concatenation of their data (cat a.gz b.gz is a valid gzip). Programs that write in blocks may pad the end with zeros, which gunzip ignores.
6bzip2
A bzip2 file is BZh and a level from 1 to 9, the block size in units of 100,000 bytes, then blocks that are compressed independently, then an end marker and a CRC of the whole stream. Blocks aren't byte-aligned: each starts with the 48-bit magic 31 41 59 26 53 59 (the digits of π) and a CRC-32 of its data, wherever the previous block's bits ended; the end marker is 17 72 45 38 50 90 (√π), and only the stream's end is padded to a byte.
A block undoes four steps: Huffman coding with 2 to 6 tables, switched every 50 symbols by a list of selectors; a move-to-front coding whose runs of zeros are written in base 2 with two symbols, RUNA and RUNB; the Burrows-Wheeler transform, a sort of all the rotations of the block that brings bytes with similar contexts together (the block stores the position of the original row); and a run-length coding where four equal bytes are followed by a count of more. bzip2's CRC is CRC-32 computed most significant bit first, unlike gzip's. Parallel compressors (pbzip2, lbzip2) write several streams one after the other.
7xz and .lzma
LZMA (from 7-Zip) is Lempel-Ziv compression — literals, and copies of earlier data at a distance up to the dictionary size — where every bit is coded by a range coder with probabilities that adapt to the last operations, the position and the previous byte. LZMA2 cuts it into chunks of up to 2 MB of output, each compressed or stored as is (so incompressible data doesn't grow), that can reset the state or the dictionary.
| Part | Contents |
| stream header (12 bytes) | magic FD 37 7A 58 5A 00, flags naming the check (none, CRC-32, CRC-64, SHA-256), a CRC-32 |
| blocks | a header (its size, the filter chain with each filter's properties, optionally the compressed and uncompressed sizes, a CRC-32), the data, zero padding to 4 bytes, the check of the uncompressed data |
| index | each block's sizes, so that a reader can seek, and a CRC-32 |
| stream footer (12 bytes) | a CRC-32, the index size, the flags again, YZ; zero stream padding may follow, then another stream |
The filter chain ends with LZMA2. Before it, BCJ filters (x86, ARM, ARM-Thumb, ARM64, PowerPC, SPARC, IA-64, RISC-V) turn the relative targets of call and branch instructions into absolute addresses, so that calls to the same function look alike and compress better; Delta does the same for audio and image samples. xz -T writes several blocks with their sizes, so they can be decompressed in parallel.
The older .lzma format (LZMA Utils, the 7-Zip SDK) is a 13-byte header — the properties lc/lp/pb in one byte, the dictionary size, the uncompressed size or all FF when unknown — then LZMA data, with no magic number and no check.
8Android backups
adb backup (deprecated since Android 12) and the bu command write a file that starts with lines of text — ANDROID BACKUP, the format version, 1 or 0 for compression, and the encryption, none or AES-256 — then the payload: a tar, zlib-compressed when compression is 1. An encrypted backup has five more lines (the salts for the user password and the master key checksum, the PBKDF2 round count, an IV, and the master key encrypted with the password), and the payload is AES-256-CBC encrypted with that master key.
The tar holds apps/<package>/: _manifest (the package, version and signatures), a/ the APK when included, f/ files, db/ databases, sp/ shared preferences, r/ and c/ other app folders, ef/ external files. Apps opt out with android:allowBackup="false" in their manifest; a backup of an app that allows it is a way to read its private data without root.
9What to look for
- Paths that leave the extraction folder: absolute paths,
.. components (“zip slip”), symbolic links that point outside, and files written through such a link (a link app/x → /etc followed by app/x/passwd).
- Permissions: set-user-ID and set-group-ID files (they run with their owner's rights when extracted as root with permissions kept), device files, world-writable files.
- Duplicate paths: a later entry silently replaces an earlier one, so what a listing shows first isn't what you get.
- Data after the end of the tar or gzip stream, which tools ignore; and in gzip, a name or comment that reveals where the file came from.
10What the tool shows, and its limits
The layers (backup → gzip → tar), the archive's size, dates and owners, the problems and findings, a bar of the file's bytes, the files as a tree with buttons to open each in the other tools, a search inside them, every header block, the gzip members and the backup header. Folders download as .zip. It reads everything into memory, so archives of several gigabytes may not fit. Gzip, bzip2, xz and .lzma are decompressed in the page (at roughly 25–35 MB per second), up to 256 MB of data. Not supported: zstd, lz4 and compress (.Z), bzip2's “randomised” blocks (written only by bzip2 0.9.0, 1998), the IA-64 and RISC-V xz filters, GNU multi-volume archives, the data layout of sparse files (they are shown with their stored size), and decrypting an encrypted backup.
11The TarArchive library
lib/compression/tar.js is a JavaScript module with no user interface and no dependencies.
import { TarArchive } from "../lib/compression/tar.js";
| Member | What it is |
| TarArchive.parse(bytes) | Reads an archive from a Uint8Array; throws when it isn't one. Damage is reported in problems. |
| TarArchive.isTar(bytes) | A ustar magic, or a first header with a valid checksum. |
| tar.entries | [{ name, dir, type, typeName, size, time, mode, modeText, uid, gid, uname, gname, owner, linkName, deviceMajor, deviceMinor, headerOffset, headerBlocks, dataOffset, format, attributes, checksum, problems }]. name is the full path, from a pax path, a GNU long name or prefix + name; attributes holds the pax records that applied. |
| tar.data(entry) | The entry's bytes, a view into the archive. |
| tar.headers | Every header block: [{ offset, kind, entry, checksum }], kind "entry", "pax", "pax global", "GNU long name", "GNU long link name" or "damaged". |
| tar.format · tar.end · tar.trailing | The formats seen ("ustar + pax"…); { offset, blocks } of the end marker; { offset, length, zeros } after it. |
| tar.findings | [{ severity, title, detail, entry }]: the security points above. |
| tar.problems | Checksums, truncation, malformed numbers or pax records. |
const tar = TarArchive.parse(new Uint8Array(await file.arrayBuffer()));
for (const entry of tar.entries) {
console.log(entry.modeText, entry.owner, entry.size, entry.name);
}
const config = tar.entries.find(entry => entry.name.endsWith("config.json"));
const text = new TextDecoder().decode(tar.data(config));
12The compression and AndroidBackup libraries
These modules in lib/compression/ have no user interface and decompress synchronously: gzip.js and android-backup.js use inflate.js (DEFLATE) and crc32.js; bzip2.js stands alone; xz.js uses lzma.js, crc64.js and lib/crypto/sha256.js. Each takes a limit on the decompressed size (256 MB by default) against decompression bombs, reports damage in problems, and has a decompress(bytes) that returns just the data or throws.
import { GzipFile } from "../lib/compression/gzip.js";
import { Bzip2File } from "../lib/compression/bzip2.js";
import { XzFile } from "../lib/compression/xz.js";
import { LzmaFile, Lzma2 } from "../lib/compression/lzma.js";
import { AndroidBackup } from "../lib/compression/android-backup.js";
| Member | What it is |
| GzipFile.parse(bytes, { limit }) | Reads every member; limit caps the decompressed size (256 MB) against decompression bombs. |
| GzipFile.decompress(bytes) | Just the data; throws when a member is damaged. |
| gzip.members | [{ offset, length, headerLength, flags, method, modified, extraFlags, os, osName, extra, name, comment, headerCrc, dataOffset, dataLength, crc, size, error }]; crc and size are { stored, computed, valid }. |
| gzip.data · gzip.trailing · gzip.problems | The joined data (null when damaged); bytes after the last member; what is wrong. |
| Bzip2File.parse(bytes, { limit }) | streams [{ offset, length, level, blockSize, blocks, crc, error }]; blocks [{ offset, bit, length, size, crc, tables, selectors, symbols }], where bit is the bit of the byte at offset where the block starts; data, trailing, problems. Bzip2File.isBzip2(bytes). |
| XzFile.parse(bytes, { limit }) | streams [{ offset, length, check, checkName, headerCrc, blocks, index, footer, padding, error }]; blocks [{ offset, headerSize, filters, declaredCompressedSize, declaredUncompressedSize, dataOffset, compressedSize, uncompressedSize, check, chunks, error }], filters [{ id, name, properties, description }], check { name, stored, computed, valid } in hexadecimal; data, trailing, problems. XzFile.isXz(bytes). |
| LzmaFile.parse(bytes, { limit }) | A .lzma file: { properties: { lc, lp, pb }, dictionarySize, declaredSize, endMarker, consumed, data, trailing, problems }. LzmaFile.isLzma(bytes) checks that the header looks like what encoders write. |
| Lzma2.decode(bytes, { offset, end, limit }) | Raw LZMA2 data to its end marker: { data, consumed, chunks }. |
| Crc64.of(bytes) | The CRC-64 of xz, as 16 hexadecimal digits (lib/compression/crc64.js). |
| AndroidBackup.parse(bytes) | { version, compressed, encryption, key, headerLength, payload, problems }; payload is the tar, ready for TarArchive.parse, or null when encrypted. |
| Crc32.of(bytes, previous) | CRC-32, optionally continued (lib/compression/crc32.js). |
| Inflate.decompress(bytes, options) | { data, consumed }: the inflated data and how many input bytes it took (lib/compression/inflate.js). |
const gzip = GzipFile.parse(bytes);
if (gzip.data && TarArchive.isTar(gzip.data)) {
const tar = TarArchive.parse(gzip.data);
}
const xz = XzFile.parse(xzBytes);
const filters = xz.streams[0].blocks[0].filters.map(filter => filter.name); // ["x86", "LZMA2"]
const backup = AndroidBackup.parse(abBytes);
const apps = backup.payload ? TarArchive.parse(backup.payload) : null;