The basics
What is a GZ file?
A GZ file is one file made smaller with gzip. report.csv becomes report.csv.gz, and decompressing it gives back exactly the original, byte for byte. Text, logs, CSV, JSON and source code often shrink to a third of their size or less; photos and videos, which are already compressed, barely shrink.
The important word is one. Unlike ZIP, gzip has no idea of folders or of several files. To compress a folder, Unix tools first bundle it into a single tar file and then gzip that, which is what a .tar.gz or .tgz file is. A .gz that isn't a .tar.gz usually holds a single log, data dump, disk image or document.
You also use gzip every day without seeing a .gz file: web servers compress pages with it on the way to your browser, which unpacks them before showing them. That's the same format, sent as a stream instead of saved as a file.
gzip was released in 1992 as a free replacement for the Unix compress program, whose LZW method was covered by patents at the time. It replaced .Z files almost everywhere.
Anatomy
What's inside a GZ file
A gzip file is a 10-byte header, some optional fields, the compressed data and an 8-byte trailer. Here are the real bytes of sample-text.txt.gz, made with gzip -9 from a file called sample-text.txt. Numbers are little-endian, so 40 92 E0 6A is the number 0x6AE09240.
offset bytes part
0 1F 8B ID1 ID2: the signature
2 08 CM: 8 = Deflate
3 08 FLG: bit 3 = FNAME
4 40 92 E0 6A MTIME: 1793102400
8 02 XFL: 2 = maximum compression
9 03 OS: 3 = Unix
10 73 61 6D 70 6C 65 2D 74 65 78 74 2E 74 78 74 "sample-text.txt"
25 00 end of the name
26 95 56 CB 6E 24 35 14 DD … 1,142 bytes of Deflate data
1168 7A 23 2C 12 CRC-32 = 0x122C237A
1172 B1 0A 00 00 ISIZE = 2,737
1176 end of file- 1
1F 8BThe signature
Every gzip file starts with these two bytes. File type detectors and decompressors use them to recognise gzip data, whatever the file is called.
- 2
08Compression method
8 means Deflate, the only method ever defined. Values 0 to 7 are reserved, so in practice every gzip file starts
1F 8B 08. - 3
08 (FLG)Flags
Five bits say which optional fields follow: FTEXT (bit 0), FHCRC (bit 1), FEXTRA (bit 2), FNAME (bit 3) and FCOMMENT (bit 4). Here only FNAME is set. In
sample-comment.gzthe flag byte is1A: name, comment and header checksum. - 4
40 92 E0 6AModification time
The original file's date as Unix seconds: 1793102400 is 27 October 2026, 12:00 UTC. Zero means no date.
gunzip -Nrestores it;gzip -nleaves it out, which makes the output reproducible. - 5
02 · 03Extra flags and operating system
XFL 2 says the slowest, best compression was used (4 would mean fastest). OS 3 means the file was made on Unix. Neither changes how the data is decoded. Python's
gzipmodule writesFF, meaning unknown. - 6
"sample-text.txt" 00Original file name
Present because FNAME is set, ended by a zero byte.
gunzipnormally ignores it and just drops.gzfrom the name you have;gunzip -Nuses the stored name instead. - 7
95 56 CB 6E …The Deflate stream
1,142 bytes encoding the 2,737-byte text. The first bits of
95say this is the final block and that it uses its own Huffman tables. Deflate has no fixed length, so the decoder finds the end by decoding. - 8
7A 23 2C 12CRC-32
A checksum of the uncompressed data. After decompressing, the tool recomputes it; a mismatch means the file is damaged and
gzip -treports a CRC error. - 9
B1 0A 00 00Original size (ISIZE)
2,737 bytes. It's stored modulo 2^32, so for files of 4 GiB and more the number wraps round. That is why
gzip -lshows wrong sizes for big files.
Under the hood
How gzip compresses: Deflate in a thin wrapper
gzip itself is a thin wrapper. The compression is done by Deflate, the same method ZIP and PNG use. It works in two passes over the data:
- 1Find repeats (LZ77). Whenever a run of bytes already appeared in the last 32 KB, it's replaced by a reference: go back this many bytes and copy that many. Repeats further back than 32 KB can't be used.
- 2Encode compactly (Huffman coding). Common bytes and common match lengths get short bit codes, rare ones long codes. Each block of data can carry its own code tables.
The levels -1 (fastest) to -9 (smallest) only change how hard gzip searches for matches. Any decoder reads them all at the same speed. The default is -6. On our test data -9 was nearly three times slower than -6 for a file 4% smaller; the XZ page has the full comparison with bzip2, xz, Zstandard and ZIP.
| Format | What it adds around Deflate | Where you meet it |
|---|---|---|
| Deflate (RFC 1951) | Nothing: raw compressed data | Inside ZIP entries, inside the other two |
| zlib (RFC 1950) | A 2-byte header (often 78 9C) and an Adler-32 checksum | PNG image data, PDF streams, Content-Encoding: deflate |
| gzip (RFC 1952) | A 10-byte header with name and date, a CRC-32 and the size | .gz files, Content-Encoding: gzip |
| ZIP | Many files, folders and a central directory | .zip, .docx, .xlsx, .epub, .jar |
The format
Optional header fields and multi-member files
| Bit | Name | Adds |
|---|---|---|
| 0 | FTEXT | A hint that the data is probably text. Ignored by decoders. |
| 1 | FHCRC | A 2-byte checksum of the header itself, after the other optional fields. |
| 2 | FEXTRA | Extra fields: a length, then sub-fields with 2-letter IDs. |
| 3 | FNAME | The original file name, ended by a zero byte. |
| 4 | FCOMMENT | A comment, ended by a zero byte. GNU gzip never writes one. |
The specification says the name and comment are Latin-1 (ISO 8859-1). In practice tools write whatever bytes the file name had, usually UTF-8.
A gzip file can also hold several members, each a complete header, Deflate stream and trailer, one after the other. Decompressing gives all members' data joined together. That's why cat a.gz b.gz > both.gz works: it gives a valid file that decompresses to the two originals joined. Log rotation, parallel compressors and streaming systems all produce multi-member files.
Not every reader handles more than one member
GNU gzip, Python's gzip module and Go's compress/gzip read every member by default. Some libraries and hand-written decoders stop after the first and silently return only part of the data. sample-multi-member.gz catches that: it should decompress to 2,970 bytes and 50 lines.
HTTP
gzip on the web: Content-Encoding vs a .gz file
Browsers announce which compressions they accept, for example Accept-Encoding: gzip, deflate, br, zstd. The server can then send an HTML page, stylesheet or JSON response compressed, with Content-Encoding: gzip. The browser decompresses it on arrival, and the page never exists as a .gz file anywhere. Brotli (br) usually compresses web content better, but gzip is the one every client understands.
A .gz file that you download is a different case: it should stay compressed. It's normally served as Content-Type: application/gzip with no Content-Encoding, so the browser saves the bytes as they are.
Why is my downloaded .gz file not compressed?
If a server sends a .gz file with Content-Encoding: gzip, the browser takes that as an instruction to decompress it and saves the result under the .gz name. gunzip then says not in gzip format. Check with file download.gz: if it says text or tar archive, rename it without .gz. The fix on the server is to send .gz files with no Content-Encoding header.
Troubleshooting
Common gzip problems and quirks
gzipdeleted my original file. That's its default:gzip filereplacesfilewithfile.gz, andgunzipdoes the reverse. Use-k(keep) or-c(write to standard output) to keep both.not in gzip format: the file doesn't start with1F 8B. It may be already decompressed (see the note above), a plain tar file, a ZIP, or an HTML error page saved under a.gzname.file name.gztells you what it really is.unexpected end of file: the file was cut short, usually by an interrupted download. Everything up to that point can often be recovered withzcat name.gz > partial, but the end is lost and can't be checked.gzip -lshows the wrong size. The size in the trailer wraps round at 4 GiB, and for a multi-member filegzip -lreports only the last member: it listssample-multi-member.gzas 1,435 bytes, though it decompresses to 2,970. Usezcat name.gz | wc -cfor the real size.decompression OK, trailing garbage ignored(ortrailing zero bytes ignored): there are bytes after the last member that aren't gzip data, often zero padding from a tape or a block device. The data before them is intact.- The file name comes out different.
gunzipnames the output after the.gzfile, not after the name stored inside.gunzip -Nuses the stored name and date. - Can't open one line from the middle. A Deflate stream has to be decoded from the start, so gzip has no random access. Tools like
zgrepandzlessdecompress on the fly, which works but reads the whole file.
How to
How to open, create and convert a GZ file
- Linux:
gunzip file.gz(orgzip -d) to decompress,gzip -k fileto compress and keep the original,zcat,zlessandzgrepto read and search without unpacking. If the file is a.tar.gz, see the .tgz page. - macOS: double-click: Archive Utility decompresses it next to the original. In Terminal the same
gzipandgunzipcommands as Linux are built in. - Windows: File Explorer in current Windows 11 opens
.tar.gzarchives, but a plain single-file.gzis most reliably opened with a free tool such as 7-Zip or PeaZip. In WSL or Git Bash,gunzipworks as on Linux. - Phone: use a file manager or archive app that supports gzip; there's no built-in way that works on every device.
- Programming: Python's
gzipmodule, Node'szlib.gunzipSyncandcreateGunzip, Go'scompress/gzip, Java'sGZIPInputStreamand .NET'sGZipStreamall read and write it. - Convert to another compression: decompress and recompress in one pipe, for example
zcat file.gz | xz -9 > file.xz. To ZIP, decompress first and zip the result.
Security
Is a GZ file safe? Decompression bombs and what's inside
Decompressing a gzip file runs no code from it, and the format has nowhere to hide any. The risks are about size and about the content you get out:
- Decompression bombs. Deflate can shrink highly repetitive data by about 1,000 to 1. In our test, 1 GiB of zero bytes compressed to 1,042,069 bytes with
gzip -9. A 10 MB.gzcan therefore expand to around 10 GB, and nested or multi-member files multiply that. The danger is greatest for servers that decompress uploads or request bodies sent withContent-Encoding: gzip. - Malware inside. A
.gzcan contain any file, including an executable or script with a misleading name. Check what came out before running anything. - Compression and secrets on HTTPS. When a web response compresses secret tokens together with text an attacker controls, the compressed size can leak the secret byte by byte. This is the BREACH attack. Sites defend against it by not putting secrets next to reflected input, or by masking tokens on each request.
If your code decompresses gzip from users
Never trust ISIZE: it can be faked and it wraps at 4 GiB. Decompress as a stream, count the output bytes, and stop at a sensible limit for that upload. Set a limit on the compression ratio too.
Free downloads
Sample GZ files to download
Free sample gzip files for testing decompressors, upload checks and parsers. Each one passes gzip -t and decompresses with Python's gzip module. All are dated 27 October 2026, 12:00 UTC in their headers.
Browsers can't show this kind of file in a page. Download it to open it in the app it belongs to.
What's inside sample-text.txt.gz
A text file compressed by GNU gzip with gzip -9, so it's exactly what you'd get on your own machine. The anatomy above shows all of its bytes except the compressed data.
Signature and method
1F 8B 08: gzip, compressed with Deflate.The stored name
FNAME is set and the header holds
sample-text.txt. Trygunzip -Nafter renaming the file to see the stored name come back.The stored date
MTIME is 1793102400, which is 27 October 2026, 12:00:00 UTC, the original file's modification time.
Maximum compression
XFL is 2 because it was made with
-9. The 2,737-byte text shrinks to 1,176 bytes, 43% of the original.UTF-8 text
The text includes accented letters, Greek, Japanese and an emoji. gzip doesn't care about encodings; if they come out garbled, the viewer is reading the text with the wrong encoding.
The trailer
CRC-32
0x122C237Aand size 2,737. Change any byte in the middle andgzip -treports a CRC error.
All sample GZ files
Text file with name and date
sample-text.txt.gz · 1.1 KB (1,176 bytes)
A UTF-8 text file compressed by GNU gzip 1.12 with
-9. The header stores the original namesample-text.txt(FNAME) and its modification time.- Basic decompression tests
- Checking that a tool reads or ignores the stored name
Multi-member gzip
sample-multi-member.gz · 508 bytes
Two complete gzip members back to back, each with its own header and CRC. Together they decompress to one 2,970-byte, 50-line log; a reader that stops early gets only the first 26 lines.
- Checking a library reads every member
- Testing streaming and concatenation code
gzip with comment and header CRC
sample-comment.gz · 320 bytes
A small JSON file with three optional header fields: FNAME (
sample-data.json), FCOMMENT ("Free sample from 99helpers.com/file-types/gz") and FHCRC, a checksum of the header. Flag byte1A.- Header parsers that must skip optional fields
- Reading the comment with `gzip -lv` or your own code
100 KB gzip file
sample-100kb.gz · 100 KB (102,400 bytes)
Exactly 102,400 bytes, decompressing to a 408,437-byte web server log of fictional requests. The last line is padding, written as a stored (uncompressed) Deflate block to reach the exact size.
- Upload and size-limit tests
- Decoders that must handle stored blocks
Ways to test with them
- Every member read:
sample-multi-member.gzmust decompress to 2,970 bytes ending with a line for 23:00 on 27 October. If it stops at 26 October, the reader ignores later members. - Optional header fields:
sample-comment.gzhas a comment and a header CRC. A parser that gets the field order wrong reads garbage instead of the JSON. - Integrity checks: flip one byte near the middle of a sample and confirm your tool reports an error instead of returning wrong data.
- Upload limits:
sample-100kb.gzis exactly 102,400 bytes, for testing a limit at the boundary.
Free to use for testing, teaching and demos, commercially or not, with no attribution needed. A link back to this page is always appreciated.
Put it online
Have a GZ file? Give it a link.
Drop a .gz file on 99helpers and it gets its own download link, so you can share a compressed log or data dump without attachment limits. Anyone with the link downloads the file exactly as you uploaded it.
Questions
GZ files, frequently asked
What is a .gz file?
A single file compressed with gzip. Decompress it and you get the original file back exactly. If the name ends in .tar.gz, it's a whole folder: a tar archive that was then gzipped.
How do I open a .gz file?
On macOS, double-click it. On Linux, run gunzip file.gz. On Windows, use a free tool such as 7-Zip; current Windows 11 also opens .tar.gz archives in File Explorer.
Is GZ the same as ZIP?
No. Both use Deflate compression, but ZIP is an archive that holds many files and folders, while gzip compresses exactly one file. Unix tools combine tar with gzip to get an archive.
Can a .gz file contain several files?
Not as separate files. A .gz can have several members, but they decompress into one output. For several files you need an archive: .tar.gz or ZIP.
What's the difference between .gz and .gzip?
None. .gz is the standard extension and what gzip itself writes. .gzip is occasionally used, and tools recognise both from the 1F 8B signature.
How much does gzip compress?
It depends on the data. Text, logs and CSV often shrink to 15 to 30% of their size. JPEG, MP4, MP3 and other compressed files barely shrink at all.
Why is the uncompressed size wrong in gzip -l?
The size stored in the file wraps round at 4 GiB, and for files with several members only the last one is reported. Decompress and count instead: zcat file.gz | wc -c.
Is it safe to open a .gz file?
Decompressing is safe in the sense that no code runs. Watch the size, since a small file can expand to an enormous one, and be careful with whatever file comes out of it.
Related formats
Other file types explained
Keep going
Related
Sources
- RFC 1952: GZIP file format specification version 4.3
- RFC 1951: DEFLATE Compressed Data Format Specification
- RFC 6713: The application/zlib and application/gzip media types
- RFC 9110: HTTP Semantics (content codings)
- GNU gzip manual
Last reviewed 27 October 2026.