GZIP File Format Specification Version 4.3: Status of This Memo
GZIP File Format Specification Version 4.3: Status of This Memo
GZIP File Format Specification Version 4.3: Status of This Memo
P. Deutsch
Aladdin Enterprises
May 1996
IESG Note:
The IESG takes no position on the validity of any Intellectual Property Rights statements contained in this
document.
Notices
<ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html>.
Abstract
This specification defines a lossless compressed data format that is compatible with the widely used GZIP
utility. The format includes a cyclic redundancy check value for detecting data corruption. The format
presently uses the DEFLATE method of compression but can be easily extended to use other compression
methods. The format can be implemented readily in a manner not covered by patents.
Deutsch
Informational
[Page 1]
RFC 1952
May 1996
Contents
1
3
4
5
6
7
8
Introduction . . . . . . . . . . . . . . . . . .
1.1
Purpose . . . . . . . . . . . . . . . . . .
1.2
Intended audience . . . . . . . . . . . . .
1.3
Scope . . . . . . . . . . . . . . . . . . . .
1.4
Compliance . . . . . . . . . . . . . . . .
1.5
Definitions of terms and conventions used
1.6
Changes from previous versions . . . . . .
Detailed specification . . . . . . . . . . . . .
2.1
Overall conventions . . . . . . . . . . . .
2.2
File format . . . . . . . . . . . . . . . . .
2.3
Member format . . . . . . . . . . . . . .
2.3.1 Member header and trailer . . . . .
Extra field . . . . . . . . . . . . . .
Compliance . . . . . . . . . . . . .
References . . . . . . . . . . . . . . . . . . .
Security Considerations . . . . . . . . . . . .
Acknowledgements . . . . . . . . . . . . . .
Authors Address . . . . . . . . . . . . . . .
Appendix: Jean-Loup Gaillys gzip utility . .
Appendix: Sample CRC Code . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 2
. 2
. 2
. 2
. 3
. 3
. 3
. 3
. 3
. 4
. 4
. 5
. 7
. 8
. 8
. 8
. 9
. 9
. 9
. 10
1 Introduction
1.1
Purpose
The purpose of this specification is to define a lossless compressed data format that:
Is independent of CPU type, operating system, file system, and character set, and hence can be used
for interchange;
Can compress or decompress a data stream (as opposed to a randomly accessible file) to produce another data stream, using only an a priori bounded amount of intermediate storage, and hence can be
used in data communications or similar structures such as Unix filters;
Compresses data with efficiency comparable to the best currently available general-purpose compression methods, and in particular considerably better than the compress program;
Can be implemented readily in a manner not covered by patents, and hence can be practiced freely;
Is compatible with the file format produced by the current widely used gzip utility, in that conforming
decompressors will be able to read data produced by the existing gzip compressor.
The data format defined by this specification does not attempt to:
Deutsch
Informational
[Page 2]
RFC 1952
May 1996
1.2
Intended audience
This specification is intended for use by implementors of software to compress data into gzip format and/or
decompress data from gzip format.
The text of the specification assumes a basic background in programming at the level of bits and other primitive data representations.
1.3
Scope
The specification specifies a compression method and a file format (the latter assuming only that a file can
store a sequence of arbitrary bytes). It does not specify any particular interface to a file system or anything
about character sets or encodings (except for file names and comments, which are optional).
1.4
Compliance
Unless otherwise indicated below, a compliant decompressor must be able to accept and decompress any file
that conforms to all the specifications presented here; a compliant compressor must produce files that conform
to all the specifications presented here. The material in the appendices is not part of the specification per se
and is not relevant to compliance.
1.5
byte: 8 bits stored or transmitted as a unit (same as an octet). (For this specification, a byte is exactly 8 bits,
even on machines which store a character on a number of bits different from 8.) See below for the numbering
of bits within a byte.
1.6
There have been no technical changes to the gzip format since version 4.1 of this specification. In version
4.2, some terminology was changed, and the sample CRC code was rewritten for clarity and to eliminate the
requirement for the caller to do pre- and post-conditioning. Version 4.3 is a conversion of the specification
to RFC style.
Deutsch
Informational
[Page 3]
RFC 1952
2
2.1
May 1996
Detailed specification
Overall conventions
|
|
|
+ more significant byte = 2 x 256
+ less significant byte = 8
Deutsch
Informational
[Page 4]
RFC 1952
2.2
May 1996
File format
A gzip file consists of a series of members (compressed data sets). The format of each member is specified in the following section. The members simply appear one after another in the file, with no additional
information before, between, or after them.
2.3
Member format
Deutsch
Informational
[Page 5]
RFC 1952
May 1996
0
1
2
3
4
5
6
7
FTEXT
FHCRC
FEXTRA
FNAME
FCOMMENT
reserved
reserved
reserved
If FTEXT is set, the file is probably ASCII text. This is an optional indication, which the compressor
may set by checking a small amount of the input data to see whether any non-ASCII characters are
present. In case of doubt, FTEXT is cleared, indicating binary data. For systems which have different
file formats for ascii text and binary data, the decompressor can use FTEXT to choose the appropriate
format. We deliberately do not specify the algorithm used to set this bit, since a compressor always
has the option of leaving it cleared and a decompressor always has the option of ignoring it and letting
some other program handle issues of data conversion.
If FHCRC is set, a CRC16 for the gzip header is present, immediately before the compressed data.
The CRC16 consists of the two least significant bytes of the CRC32 for all bytes of the gzip header up
to and not including the CRC16. [The FHCRC bit was never set by versions of gzip up to 1.2.4, even
though it was documented with a different meaning in gzip 1.2.4.]
If FEXTRA is set, optional extra fields are present, as described in a following section.
If FNAME is set, an original file name is present, terminated by a zero byte. The name must consist of
ISO 8859-1 (LATIN-1) characters; on operating systems using EBCDIC or any other character set for
file names, the name must be translated to the ISO LATIN-1 character set. This is the original name of
the file being compressed, with any directory components removed, and, if the file being compressed
is on a file system with case insensitive names, forced to lower case. There is no original file name if
the data was compressed from a source other than a named file; for example, if the source was stdin
on a Unix system, there is no file name.
Deutsch
Informational
[Page 6]
RFC 1952
May 1996
If FCOMMENT is set, a zero-terminated file comment is present. This comment is not interpreted; it
is only intended for human consumption. The comment must consist of ISO 8859-1 (LATIN-1) characters. Line breaks should be denoted by a single line feed character (10 decimal).
Reserved FLG bits must be zero.
MTIME (Modification TIME)
This gives the most recent modification time of the original file being compressed. The time is in Unix
format, i.e., seconds since 00:00:00 GMT, Jan. 1, 1970. (Note that this may cause problems for MSDOS and other systems that use local rather than Universal time.) If the compressed data did not come
from a file, MTIME is set to the time at which compression started. MTIME = 0 means no time stamp
is available.
XFL (eXtra FLags)
These flags are available for use by specific compression methods. The deflate method (CM = 8)
sets these flags as follows:
XFL = 2 - compressor used maximum compression,
slowest algorithm
XFL = 4 - compressor used fastest algorithm
OS (Operating System)
This identifies the type of file system on which compression took place. This may be useful in determining end-of-line convention for text files. The currently defined values are as follows:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
255
Informational
[Page 7]
RFC 1952
May 1996
V.42. (See http://www.iso.ch for ordering ISO documents. See gopher://info.itu.ch for an online version of ITU-T V.42.)
ISIZE (Input SIZE)
This contains the size of the original (uncompressed) input data modulo 232.
Extra field If the FLG.FEXTRA bit is set, an extra field is present in the header, with total length XLEN
bytes. It consists of a series of subfields, each of the form:
+---+---+---+---+==================================+
|SI1|SI2| LEN |... LEN bytes of subfield data ...|
+---+---+---+---+==================================+
SI1 and SI2 provide a subfield ID, typically two ASCII letters with some mnemonic value. Jean-Loup Gailly
<gzip@prep.ai.mit.edu> is maintaining a registry of subfield IDs; please send him any subfield ID you wish
to use. Subfield IDs with SI2 = 0 are reserved for future use. The following IDs are currently defined:
SI1
---------0x41 (A)
SI2
---------0x70 (P)
Data
---Apollo file type information
LEN gives the length of the subfield data, excluding the 4 initial bytes.
Compliance A compliant compressor must produce files with correct ID1, ID2, CM, CRC32, and ISIZE,
but may set all the other fields in the fixed-length part of the header to default values (255 for OS, 0 for all
others). The compressor must set all reserved bits to zero.
A compliant decompressor must check ID1, ID2, and CM, and provide an error indication if any of these
have incorrect values. It must examine FEXTRA/XLEN, FNAME, FCOMMENT and FHCRC at least so
it can skip over the optional fields if they are present. It need not examine any other part of the header or
trailer; in particular, a decompressor may ignore FTEXT and OS and always produce binary output, and
still be compliant. A compliant decompressor must give an error indication if any reserved bit is non-zero,
since such a bit could indicate the presence of a new field that would cause subsequent data to be interpreted
incorrectly.
3 References
[1] Information Processing - 8-bit single-byte coded graphic character sets - Part 1: Latin alphabet No.1
(ISO 8859-1:1987). The ISO 8859-1 (Latin-1) character set is a superset of 7-bit ASCII. Files defining this
character set are available as iso 8859-1.* in ftp://ftp.uu.net/graphics/png/documents/
[2] ISO 3309
Deutsch
Informational
[Page 8]
RFC 1952
May 1996
Security Considerations
Any data compression method involves the reduction of redundancy in the data. Consequently, any corruption of the data is likely to have severe effects and be difficult to correct. Uncompressed text, on the other
hand, will probably still be readable despite the presence of some corrupted bytes.
It is recommended that systems using this data format provide some means of validating the integrity of the
compressed data, such as by setting and checking the CRC-32 check value.
Acknowledgements
Trademarks cited in this document are the property of their respective owners.
Jean-Loup Gailly designed the gzip format and wrote, with Mark Adler, the related software described in this
specification. Glenn Randers-Pehrson converted this document to RFC and HTML format.
6 Authors Address
L. Peter Deutsch
Aladdin Enterprises
203 Santa Margarita Ave.
Menlo Park, CA 94025
Phone: (415) 322-0103 (AM only)
FAX:
(415) 322-1734
EMail: <ghost@aladdin.com>
Questions about the technical content of this specification can be sent by email to:
Deutsch
Informational
[Page 9]
RFC 1952
May 1996
The most widely used implementation of gzip compression, and the original documentation on which this
specification is based, were created by Jean-Loup Gailly <gzip@prep.ai.mit.edu>. Since this implementation is a de facto standard, we mention some more of its features here. Again, the material in this section is
not part of the specification per se, and implementations need not follow it to be compliant.
When compressing or decompressing a file, gzip preserves the protection, ownership, and modification time
attributes on the local file system, since there is no provision for representing protection attributes in the
gzip file format itself. Since the file format includes a modification time, the gzip decompressor provides a
command line switch that assigns the modification time from the file, rather than the local modification time
of the compressed input, to the decompressed output.
The following sample code represents a practical implementation of the CRC (Cyclic Redundancy Check).
(See also ISO 3309 and ITU-T V.42 for a formal specification.)
The sample code is in the ANSI C programming language. Non C users may find it easier to read with these
hints:
&
>>
!
++
0xNNN
Deutsch
Informational
[Page 10]
RFC 1952
May 1996
g
/*
Update a running crc with the bytes buf[0..len-1] and return
the updated crc. The crc should be initialized to zero. Pre- and
post-conditioning (ones complement) is performed within this
function so it shouldnt be done by the caller. Usage example:
unsigned long crc = 0L;
while (read buffer(buffer, length) != EOF) f
crc = update crc(crc, buffer, length);
g
if (crc != original crc) error();
*/
unsigned long update crc(unsigned long crc,
unsigned char *buf, int len)
f
unsigned long c = crc 0xffffffffL;
int n;
if (!crc table computed)
make crc table();
for (n = 0; n < len; n++) f
c = crc table[(c buf[n]) & 0xff] (c >> 8);
g
return c 0xffffffffL;
Deutsch
Informational
[Page 11]
RFC 1952
May 1996
Deutsch
Informational
[Page 12]