📄 algorithm.doc
字号:
1. AlgorithmThe deflation algorithm used by zip and gzip is a variation of LZ77(Lempel-Ziv 1977, see reference below). It finds duplicated strings inthe input data. The second occurrence of a string is replaced by apointer to the previous string, in the form of a pair (distance,length). Distances are limited to 32K bytes, and lengths are limitedto 258 bytes. When a string does not occur anywhere in the previous32K bytes, it is emitted as a sequence of literal bytes. (In thisdescription, 'string' must be taken as an arbitrary sequence of bytes,and is not restricted to printable characters.)Literals or match lengths are compressed with one Huffman tree, andmatch distances are compressed with another tree. The trees are storedin a compact form at the start of each block. The blocks can have anysize (except that the compressed data for one block must fit inavailable memory). A block is terminated when zip determines that itwould be useful to start another block with fresh trees. (This issomewhat similar to compress.)Duplicated strings are found using a hash table. All input strings oflength 3 are inserted in the hash table. A hash index is computed forthe next 3 bytes. If the hash chain for this index is not empty, allstrings in the chain are compared with the current input string, andthe longest match is selected.The hash chains are searched starting with the most recent strings, tofavor small distances and thus take advantage of the Huffman encoding.The hash chains are singly linked. There are no deletions from thehash chains, the algorithm simply discards matches that are too old.To avoid a worst-case situation, very long hash chains are arbitrarilytruncated at a certain length, determined by a runtime option (zip -1to -9). So zip does not always find the longest possible match butgenerally finds a match which is long enough.zip also defers the selection of matches with a lazy evaluationmechanism. After a match of length N has been found, zip searches for alonger match at the next input byte. If a longer match is found, theprevious match is truncated to a length of one (thus producing a singleliteral byte) and the longer match is emitted afterwards. Otherwise,the original match is kept, and the next match search is attempted onlyN steps later.The lazy match evaluation is also subject to a runtime parameter. Ifthe current match is long enough, zip reduces the search for a longermatch, thus speeding up the whole process. If compression ratio is moreimportant than speed, zip attempts a complete second search even ifthe first match is already long enough.The lazy match evaluation is no performed for the fastest compressionmodes (speed options -1 to -3). For these fast modes, new stringsare inserted in the hash table only when no match was found, orwhen the match is not too long. This degrades the compression ratiobut saves time since there are both fewer insertions and fewer searches.2. gzip file formatThe pkzip format imposes a lot of overhead in various headers, whichare useful for an archiver but not necessary when only one file iscompressed. gzip uses a much simpler structure. Numbers are in littleendian format, and bit 0 is the least significant bit.A gzip file is a sequence of compressed members. Each member has thefollowing structure:2 bytes magic header 0x1f, 0x8b (\037 \213) 1 byte compression method (0..7 reserved, 8 = deflate)1 byte flags bit 0 set: file probably ascii text bit 1 set: continuation of multi-part gzip file bit 2 set: extra field present bit 3 set: original file name present bit 4 set: file comment present bit 5 set: file is encrypted bit 6,7: reserved4 bytes file modification time in Unix format1 byte extra flags (depend on compression method)1 byte operating system on which compression took place2 bytes optional part number (second part=1)2 bytes optional extra field length? bytes optional extra field? bytes optional original file name, zero terminated? bytes optional file comment, zero terminated12 bytes optional encryption header? bytes compressed data4 bytes crc324 bytes uncompressed input size modulo 2^32The format was designed to allow single pass compression without anybackwards seek, and without a priori knowledge of the uncompressedinput size or the available size on the output media. If input doesnot come from a regular disk file, the file modification time is setto the time at which compression started.The time stamp is useful mainly when one gzip file is transferred overa network. In this case it would not help to keep ownershipattributes. In the local case, the ownership attributes are preservedby gzip when compressing/decompressing the file. A time stamp of zerois ignored.Bit 0 in the flags is only an optional indication, which can be set bya small lookahead in the input data. In case of doubt, the flag iscleared indicating binary data. For systems which have differentfile formats for ascii text and binary data, the decompressor canuse the flag to choose the appropriate format.The extra field, if present, must consist of one or more subfields,each with the following format: subfield id : 2 bytes subfield size : 2 bytes (little-endian format) subfield dataThe subfield id can consist of two letters with some mnemonic value.Please send any such id to jloup@chorus.fr. Ids with a zero secondbyte are reserved for future use. The following ids are defined: Ap (0x41, 0x70) : Apollo file type informationThe subfield size is the size of the subfield data and does notinclude the id and the size itself. The field 'extra field length' isthe total size of the extra field, including subfield ids and sizes.It must be possible to detect the end of the compressed data with anycompression format, regardless of the actual size of the compresseddata. If the compressed data cannot fit in one file (in particular fordiskettes), each part starts with a header as described above, butonly the last part has the crc32 and uncompressed size. A decompressormay prompt for additional data for multipart compressed files. It isdesirable but not mandatory that multiple parts be extractableindependently so that partial data can be recovered if one of theparts is damaged. This is possible only if no compression state iskept from one part to the other. The compression-type dependent flagscan indicate this.If the file being compressed is on a file system with case insensitivenames, the original name field must be forced to lower case. There isno original file name if the data was compressed from standard input.Compression is always performed, even if the compressed file isslightly larger than the original. The worst case expansion isa few bytes for the gzip file header, plus 5 bytes every 32K block,or an expansion ratio of 0.015% for large files. Note that the actualnumber of used disk blocks almost never increases.The encryption is that of zip 1.9. For the encryption check, thelast byte of the decoded encryption header must be zero. The timestamp of an encrypted file might be set to zero to avoid giving a clueabout the construction of the random header.Jean-loup Gaillyjloup@chorus.frReferences:[LZ77] Ziv J., Lempel A., "A Universal Algorithm for Sequential DataCompression", IEEE Transactions on Information Theory", Vol. 23, No. 3,pp. 337-343.APPNOTE.TXT documentation file in PKZIP 1.93a. It is available byftp in ftp.cso.uiuc.edu:/pc/exec-pc/pkz193a.exe [128.174.5.59]Use "unzip pkz193a.exe APPNOTE.TXT" to extract (note: unzip, not gunzip).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -