gzip

概览

 

  gzipGNU zip的缩写,它是一个GNU自由软件的文件压缩程序,也经常用来表示gzip这种文件格式。软件的作者是Jean-loup Gailly和Mark Adler。1992年10月31日第一次公开发布,版本号是0.1。

文件格式

gzip的基础是DEFLATE,DEFLATE是LZ77与哈夫曼编码的一个组合体。DEFLATE最初是作为LZW以及其它受专利保护的数据压缩算法的替代版本而设计的,当时那些专利限制了compress以及其它一些流行的归档工具的应用。

文件格式说明:

  • 10字节的头,包含幻数、版本号以及时间戳
  • 可选的扩展头,如原文件名
  • 文件体,包括DEFLATE压缩的数据
  • 8字节的尾注,包括CRC-32校验和以及未压缩的原始数据长度

尽管这种文件格式允许多个这样的数据拼接在一起,在解压时也把它们当作拼接在一起的数据,但是通常gzip仅仅用来压缩单个文件。多个文件的压缩归档通常是首先将这些文件合并成一个tar文件,然后使用gzip进行压缩,最后生成的.tar.gz或者.tgz文件,这就是所谓的“tar压缩包”或者“tarball”。

注意不要将gzip和ZIP压缩格式混淆。ZIP也使用DEFLATE算法,而且可移植性更好,并且不需要一个外部的归档工具就可以包容多个文件。但是,由于ZIP对每个文件进行单独压缩而没有利用文件间的冗余信息(固实压缩),所以ZIP的压缩率要稍逊于tar压缩包。

zlib是DEFLATE算法的实现库,它的API同时支持 gzip文件格式以及一个简化的数据流格式。zlib数据流格式、DEFLATE以及gzip文件格式均已被标准化成了,分别是RFC 1950、RFC 1951以及RFC 1952。

gzip命令的常用选项

  • -c,--stdout 将解压缩的内容输出到标准输出,原文件保持不变
  • -d,--decompress 解压缩
  • -f,--force 强制覆盖旧文件
  • -l,--list 列出压缩包内储存的原始文件的信息(如,解压后的名字、压缩率等)
  • -n,--no-name 压缩时不保存原始文件的文件名和时间戳,解压缩时不恢复原始文件的文件名和时间戳(此时,解出来的文件,其文件名为压缩包的文件名)
  • -N,--name 压缩时保存原始文件的文件名和时间戳,解压缩时恢复原始文件的文件名和时间戳
  • -q,--quiet 抑制所有警告信息
  • -r,--recursive 递归
  • -t,--test 测试压缩文件完整性
  • -v,--verbose 冗余模式(即显示每一步的执行内容)
  • -1、-2、...、-9 压缩率依次增大,速度依次减慢,默认为-6

其它应用

HTTP/1.1协议允许客户端可以选择要求从服务器下载压缩内容,这个标准本身定义了两种压缩方法:“gzip”(内容用gzip数据流进行封装)以及“deflate”(内容是原始格式、没有数据头的DEFLATE数据流)。许多HTTP客户端库以及绝大多数当今的浏览器都支持这两种格式。

二十世纪九十年代末期以来,一个基于数据块排序算法的文件压缩工具bzip2作为gzip的替代者逐渐得到流行,它可以生成相当小的压缩文件,尤其是对于源代码以及其它的结构化文本来说更是这样,但是这样做的代价是最高达4倍内存与处理器时间消耗。bzip2压缩的tar包传统上叫作.tar.bz2

AdvanceCOMP也有一个DEFLATE实现,它产生的gzip兼容文件比gzip本身的压缩率更高。

gzip压缩文件对应的解压程序是gunzip

参见

  • 归档格式列表
  • 归档工具列表
  • 归档工具比较
  • Unix程序列表
  • .tar.gz.gpg
  • 自由文件格式

外部链接

  • gzip 文件格式规范
  • gzip 主页 以及 http://www.gzip.org/
  • Zlib 主页
  • GZIP 相关的资源、教程及源码
  • gzip 恢复工具箱
  • The Online Marketing Benefits of gzip

本文来源:http://zh.wikipedia.org/wiki/Gzip

From Wikipedia, the free encyclopedia

Gzip is any of several software applications used for file compression and decompression. The term usually refers to the GNU Project's implementation, "gzip" standing for GNU zip. It is based on the DEFLATE algorithm, which is a combination of Lempel-Ziv (LZ77) and Huffman coding. The program was created by Jean-Loup Gailly and Mark Adler as a free software replacement for the compress program used in early Unix systems, and intended for use by the Project. Version 0.1 was first publicly released on October 30, 1992, and version 1.0 followed in February 1993.

OpenBSD's version of gzip is actually the compress program, to which support for the gzip format was added in OpenBSD 3.4. The "g" in this specific version stands for gratis.[2]

 

FreeBSD, DragonFlyBSD, and NetBSD use a BSD-licensed implementation instead of the GNU version; it is actually a command-line interface for zlib intended to be compatible with the GNU implementation's options.[3] These implementations originally come from NetBSD, and supports decompression of bzip2 and Unix pack(1) format.

 

Other uses

 

The "Content-Encoding"/"Accept-Encoding" and "Transfer-Encoding"/"TE" headers in HTTP/1.1 allow clients to optionally receive compressed HTTP responses and (less commonly) to send compressed requests. The specification for HTTP/1.1 (RFC 2616) specifies three compression methods: "gzip" (RFC 1952; the content wrapped in a gzip stream), "deflate" (RFC 1950; the content wrapped in a zlib-formatted stream), and "compress" (explained in RFC 2616 section 3.5 as 'The encoding format produced by the common UNIX file compression program "compress". This format is an adaptive Lempel-Ziv-Welch coding (LZW).'). Many client libraries, browsers, and server platforms (including Apache and Microsoft IIS) support gzip. Many agents also support deflate, although several important players incorrectly implement deflate support using the format specified by RFC 1951 instead of the correct format specified by RFC 1950 (which encapsulates RFC 1951). Notably, Internet Explorer versions 6, 7, and 8 report deflate support but do not actually accept RFC 1950 format, making actual use of deflate highly unusual. Many clients accept both RFC 1951 and RFC 1950-formatted data for the "deflate" compressed method, but a server has no way to detect whether a client will correctly handle RFC 1950 format.

 

Since the late 1990s, bzip2, a file compression utility based on a block-sorting algorithm, has gained some popularity as a gzip replacement. It produces considerably smaller files (especially for source code and other structured text), but at the cost of memory and processing time (up to a factor of 4). bzip2-compressed archive files are conventionally named either .tar.bz2 or simply .tbz.

 

AdvanceCOMP and 7-Zip can produce gzip-compatible files, using an internal DEFLATE implementation with better compression ratios than gzip itself—at the cost of more processor time compared to the reference implementation.

File format

Gzip is based on the DEFLATE algorithm, which is a combination of LZ77 and Huffman coding. DEFLATE was intended as a replacement for LZW and other patent-encumbered data compression algorithms, which, at the time, limited the usability of compress and other popular archivers.

 

"Gzip" is often also used to refer to the gzip file format, which is:

  • a 10-byte header, containing a magic number, a version number and a time stamp
  • optional extra headers, such as the original file name,
  • a body, containing a DEFLATE-compressed payload
  • an 8-byte footer, containing a CRC-32 checksum and the length of the original uncompressed data

Although its file format also allows for multiple such streams to be concatenated (zipped files are simply decompressed concatenated as if they were originally one file), gzip is normally used to compress just single files.[4] Compressed archives are typically created by assembling collections of files into a single tar archive, and then compressing that archive with gzip. The final .tar.gz or .tgz file is usually called a "tarball".[5]

 

Gzip is not to be confused with the ZIP archive format, which also uses DEFLATE. The ZIP format can hold collections of files without an external archiver, but is less compact than compressed tarballs holding the same data, because it compresses files individually and cannot take advantage of redundancy between files (solid compression).

 

Zlib is an abstraction of the DEFLATE algorithm in library form which includes support both for the gzip file format and a lightweight stream format in its API. The zlib stream format, DEFLATE, and the gzip file format were standardized respectively as RFC 1950, RFC 1951, and RFC 1952.

 

The corresponding program for uncompressing gzipped files is gunzip. Both commands call the same binary; gunzip has the same effect as gzip -d.

gunzip and zcat

The gzip utility on UNIX systems has some alternative names.

 

When gzip is invoked as gunzip, it decompresses the data (a file or stdin). gunzip is equivalent to gzip -d.

 

When gzip is invoked as zcat, it also decompresses the data, but behaves similarly to cat. It decompresses individual files and concatenates them to standard output. zcat is equivalent to gzip -d -c. [6]

Examples

  • To compress a file with gzip, pass the filename as an argument:
gzip file.txt

The command will then replace the original file with a new, usually smaller file called file.txt.gz. To keep the original file file.txt, it is necessary to use the -c option and redirect the output to a new file.

  • To uncompress, use gunzip:
gunzip file.txt.gz
  • Generally, multiple files can be compressed by combining tar with gzip:
tar czf files.tar.gz *.txt

See also

  • List of archive formats
  • List of file archivers
  • Comparison of file archivers
  • List of Unix programs
  • Free file format
  • Info-ZIP's funzip can gunzip gzip-ped data

References

  • RFC 1952 - GZIP file format specification version 4.3
  1. ^ Meyering, Jim (2010-01-20). "gzip-1.4 released [stable/security]". info-gnu mailing list. http://lists.gnu.org/archive/html/info-gnu/2010-01/msg00016.html. Retrieved 2010-03-21. 
  2. ^ "OpenBSD gzip(1) manual page". OpenBSD. http://www.openbsd.org/cgi-bin/man.cgi?query=gzip#HISTORY. Retrieved 2007-07-23. 
  3. ^ http://man.freebsd.org/gzip
  4. ^ "Can gzip compress several files into a single archive?". http://www.gzip.org/#faq16. Retrieved 2010-01-27. 
  5. ^ "tarball, The Jargon File, version 4.4.7". http://www.catb.org/jargon/html/T/tarball.html. Retrieved 2010-01-27. 
  6. ^ [1] zcat manual page in FreeBSD 7.0

External links

  • GNU Gzip home page
  • original gzip Home Page
  • pigz - Parallel gzip (and Windows version)

Come from: http://en.wikipedia.org/wiki/Gzip


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。