zip.htm

来自「刚刚看到本站有Visual C++数字图象处理(人民邮电出版社)的电子书」· HTM 代码 · 共 896 行 · 第 1/4 页

HTM
896
字号
where n(j) is the size of set s(j).  n(j) can be 0, in which

case the follower set for s(j) is empty.  each n(j) value is

encoded in 6 bits, followed by n(j) eight bit character values

corresponding to s(j)[0] to s(j)[n(j)-1] respectively.  if

n(j) is 0, then no values for s(j) are stored, and the value

for n(j-1) immediately follows.</pre>

    <pre>immediately after the follower sets, is the compressed data

stream.  the compressed data stream can be interpreted for the

probabilistic decompression as follows:

</pre>

    <pre>let last-character &lt;- 0.

loop until done

    if the follower set s(last-character) is empty then

        read 8 bits from the input stream, and copy this

        value to the output stream.

    otherwise if the follower set s(last-character) is non-empty then

        read 1 bit from the input stream.

        if this bit is not zero then

            read 8 bits from the input stream, and copy this

            value to the output stream.

        otherwise if this bit is zero then

            read b(n(last-character)) bits from the input

            stream, and assign this value to i.

            copy the value of s(last-character)[i] to the

            output stream.</pre>

    <pre>    assign the last value placed on the output stream to

    last-character.

end loop

</pre>

    <pre>b(n(j)) is defined as the minimal number of bits required to

encode the value n(j)-1.

</pre>

    <pre>the decompressed stream from above can then be expanded to

re-create the original file as follows:

</pre>

    <pre>let state &lt;- 0.</pre>

    <pre>loop until done

    read 8 bits from the input stream into c.

    case state of

        0:  if c is not equal to dle (144 decimal) then

                copy c to the output stream.

            otherwise if c is equal to dle then

                let state &lt;- 1.</pre>

    <pre>        1:  if c is non-zero then

                let v &lt;- c.

                let len &lt;- l(v)

                let state &lt;- f(len).

            otherwise if c is zero then

                copy the value 144 (decimal) to the output stream.

                let state &lt;- 0</pre>

    <pre>        2:  let len &lt;- len + c

            let state &lt;- 3.</pre>

    <pre>        3:  move backwards d(v,c) bytes in the output stream

            (if this position is before the start of the output

            stream, then assume that all the data before the

            start of the output stream is filled with zeros).

            copy len+3 bytes from this position to the output stream.

            let state &lt;- 0.

    end case

end loop

</pre>

    <pre>the functions f,l, and d are dependent on the 'compression

factor', 1 through 4, and are defined as follows:</pre>

    <pre>for compression factor 1:

    l(x) equals the lower 7 bits of x.

    f(x) equals 2 if x equals 127 otherwise f(x) equals 3.

    d(x,y) equals the (upper 1 bit of x) * 256 + y + 1.

for compression factor 2:

    l(x) equals the lower 6 bits of x.

    f(x) equals 2 if x equals 63 otherwise f(x) equals 3.

    d(x,y) equals the (upper 2 bits of x) * 256 + y + 1.

for compression factor 3:

    l(x) equals the lower 5 bits of x.

    f(x) equals 2 if x equals 31 otherwise f(x) equals 3.

    d(x,y) equals the (upper 3 bits of x) * 256 + y + 1.

for compression factor 4:

    l(x) equals the lower 4 bits of x.

    f(x) equals 2 if x equals 15 otherwise f(x) equals 3.

    d(x,y) equals the (upper 4 bits of x) * 256 + y + 1.

</pre>

    <pre>imploding - method 6

--------------------</pre>

    <pre>the imploding algorithm is actually a combination of two distinct

algorithms.  the first algorithm compresses repeated byte

sequences using a sliding dictionary.  the second algorithm is

used to compress the encoding of the sliding dictionary output,

using multiple shannon-fano trees.</pre>

    <pre>the imploding algorithm can use a 4k or 8k sliding dictionary

size. the dictionary size used can be determined by bit 1 in the

general purpose flag word; a 0 bit indicates a 4k dictionary

while a 1 bit indicates an 8k dictionary.</pre>

    <pre>the shannon-fano trees are stored at the start of the compressed

file. the number of trees stored is defined by bit 2 in the

general purpose flag word; a 0 bit indicates two trees stored, a

1 bit indicates three trees are stored.  if 3 trees are stored,

the first shannon-fano tree represents the encoding of the

literal characters, the second tree represents the encoding of

the length information, the third represents the encoding of the

distance information.  when 2 shannon-fano trees are stored, the

length tree is stored first, followed by the distance tree.</pre>

    <pre>the literal shannon-fano tree, if present is used to represent

the entire ascii character set, and contains 256 values.  this

tree is used to compress any data not compressed by the sliding

dictionary algorithm.  when this tree is present, the minimum

match length for the sliding dictionary is 3.  if this tree is

not present, the minimum match length is 2.</pre>

    <pre>the length shannon-fano tree is used to compress the length part

of the (length,distance) pairs from the sliding dictionary

output.  the length tree contains 64 values, ranging from the

minimum match length, to 63 plus the minimum match length.</pre>

    <pre>the distance shannon-fano tree is used to compress the distance

part of the (length,distance) pairs from the sliding dictionary

output. the distance tree contains 64 values, ranging from 0 to

63, representing the upper 6 bits of the distance value.  the

distance values themselves will be between 0 and the sliding

dictionary size, either 4k or 8k.</pre>

    <pre>the shannon-fano trees themselves are stored in a compressed

format. the first byte of the tree data represents the number of

bytes of data representing the (compressed) shannon-fano tree

minus 1.  the remaining bytes represent the shannon-fano tree

data encoded as:</pre>

    <pre>    high 4 bits: number of values at this bit length + 1. (1 - 16)

    low  4 bits: bit length needed to represent value + 1. (1 - 16)</pre>

    <pre>the shannon-fano codes can be constructed from the bit lengths

using the following algorithm:</pre>

    <pre>1)  sort the bit lengths in ascending order, while retaining the

    order of the original lengths stored in the file.</pre>

    <pre>2)  generate the shannon-fano trees:</pre>

    <pre>    code &lt;- 0

    codeincrement &lt;- 0

    lastbitlength &lt;- 0

    i &lt;- number of shannon-fano codes - 1   (either 255 or 63)</pre>

    <pre>    loop while i &gt;= 0

        code = code + codeincrement

        if bitlength(i) &lt;&gt; lastbitlength then

            lastbitlength=bitlength(i)

            codeincrement = 1 shifted left (16 - lastbitlength)

        shannoncode(i) = code

        i &lt;- i - 1

    end loop

</pre>

    <pre>3)  reverse the order of all the bits in the above shannoncode()

    vector, so that the most significant bit becomes the least

    significant bit.  for example, the value 0x1234 (hex) would

    become 0x2c48 (hex).</pre>

    <pre>4)  restore the order of shannon-fano codes as originally stored

    within the file.</pre>

    <pre>example:</pre>

    <pre>    this example will show the encoding of a shannon-fano tree

    of size 8.  notice that the actual shannon-fano trees used

    for imploding are either 64 or 256 entries in size.</pre>

    <pre>example:   0x02, 0x42, 0x01, 0x13</pre>

    <pre>    the first byte indicates 3 values in this table.  decoding the

    bytes:

            0x42 = 5 codes of 3 bits long

            0x01 = 1 code  of 2 bits long

            0x13 = 2 codes of 4 bits long</pre>

    <pre>    this would generate the original bit length array of:

    (3, 3, 3, 3, 3, 2, 4, 4)</pre>

    <pre>    there are 8 codes in this table for the values 0 thru 7.  using the

    algorithm to obtain the shannon-fano codes produces:</pre>

    <pre>                                  reversed     order     original

val  sorted   constructed code      value     restored    length

---  ------   -----------------   --------    --------    ------

0:     2      1100000000000000        11       101          3

1:     3      1010000000000000       101       001          3

2:     3      1000000000000000       001       110          3

3:     3      0110000000000000       110       010          3

4:     3      0100000000000000       010       100          3

5:     3      0010000000000000       100        11          2

6:     4      0001000000000000      1000      1000          4

7:     4      0000000000000000      0000      0000          4

</pre>

    <pre>the values in the val, order restored and original length columns

now represent the shannon-fano encoding tree that can be used for

decoding the shannon-fano encoded data.  how to parse the

variable length shannon-fano values from the data stream is beyond the

scope of this document.  (see the references listed at the end of

this document for more information.)  however, traditional decoding

schemes used for huffman variable length decoding, such as the

greenlaw algorithm, can be successfully applied.</pre>

    <pre>the compressed data stream begins immediately after the

compressed shannon-fano data.  the compressed data stream can be

interpreted as follows:</pre>

    <pre>loop until done

    read 1 bit from input stream.</pre>

    <pre>    if this bit is non-zero then       (encoded data is literal data)

        if literal shannon-fano tree is present

            read and decode character using literal shannon-fano tree.

        otherwise

            read 8 bits from input stream.

        copy character to the output stream.

    otherwise                   (encoded data is sliding dictionary match)

        if 8k dictionary size

            read 7 bits for offset distance (lower 7 bits of offset).

        otherwise

            read 6 bits for offset distance (lower 6 bits of offset).</pre>

    <pre>        using the distance shannon-fano tree, read and decode the

          upper 6 bits of the distance value.</pre>

    <pre>        using the length shannon-fano tree, read and decode

          the length value.</pre>

    <pre>        length &lt;- length + minimum match length</pre>

    <pre>        if length = 63 + minimum match length

            read 8 bits from the input stream,

            add this value to length.</pre>

    <pre>        move backwards distance+1 bytes in the output stream, and

        copy length characters from this position to the output

        stream.  (if this position is before the start of the output

        stream, then assume that all the data before the start of

        the output stream is filled with zeros).

end loop</pre>

    <pre>tokenizing - method 7

--------------------</pre>

    <pre>this method is not used by pkzip.</pre>

    <pre>deflating - method 8

-----------------</pre>

    <pre>the deflate algorithm is similar to the implode algorithm using

a sliding dictionary of up to 32k with secondary compression

from huffman/shannon-fano codes.</pre>

    <pre>the compressed data is stored in blocks with a header describing

the block and the huffman codes used in the data block.  the header

format is as follows:</pre>

    <pre>   bit 0: last block bit     this bit is set to 1 if this is the last

                             compressed block in the data.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?