GZ
Gzip Compressed File · application/gzip · .gz
Overview
A gzip file can contain one or more compressed members in sequence. Reading all members produces their original bytes in order. A .tar.gz file uses tar to record file names and directory structure before gzip compresses those archive bytes.
Why the last size field says 5 for a 9-byte result
Compress the ASCII text red followed by a line feed (4 bytes). Separately compress blue followed by a line feed (5 bytes). Join the two compressed byte sequences end to end. A reader that processes both members produces two lines, 9 bytes total.
The example begins with the hex bytes 1F 8B 08: the gzip signature and the DEFLATE method. The last four bytes of the combined file are 05 00 00 00. They encode the uncompressed size of the blue member, modulo 2³². Reading those four bytes as a total for the whole file would miss the red member.
To reproduce it in Python, join gzip.compress(b"red\n") + gzip.compress(b"blue\n") after importing gzip, then pass the result to gzip.decompress. If another reader returns just the first line, check its support for concatenated members. For a directory backup, put the files in a tar archive before compressing it. RFC 1952, sections 2.2 and 2.3.1
Typical use cases
- Single-file compression
- Web transfer compression
- Log archives
- tar.gz bundles
Advantages
- Lossless decoding restores the original bytes
- Members can be concatenated without recompressing their contents
Disadvantages
- Provides no directory tree for a collection of files
- Each member records its own size modulo 2³²
- No encryption
How to open or use it
Decompress a copy with a gzip reader. If the result is a tar archive, list its entries using a tar reader before extracting the files you need.