VBA解压缩ZIP文件07——length和distance扩展

2020-07-28 14:28:50 浏览数 (1)

前面介绍压缩过程的时候,讲到过length和distance,因为数字范围有点大,ZIP中为了节省记录的信息,将其划分为29个区间,在从Huffman树中得到了length和distance之后,还需要到划分的区间进行一次扩展:

代码语言:javascript复制
'扩展LengthCode、DistanceCode
Private Function ExtraValue(Value As Long, ValueArr() As CodeInterval, cpByte() As Byte, ByRef bitIndex As Long) As Long
    Dim bValue As Long
    If ValueArr(Value).Bits Then
        bValue = GetBits(cpByte, bitIndex, ValueArr(Value).Bits)
        bitIndex = bitIndex   ValueArr(Value).Bits
        Value = ValueArr(Value).Start   bValue
    
    Else
        Value = ValueArr(Value).Start
    End If
    
    ExtraValue = Value
End Function

2个区间的初始过程:

代码语言:javascript复制
'分区间的数据
Private Type CodeInterval
    Bits As Long
    Start As Long
    End As Long
End Type
代码语言:javascript复制
Private Function InitLengthCode()
    LengthCode(0).Bits = 0: LengthCode(0).Start = 3: LengthCode(0).End = 3
    LengthCode(1).Bits = 0: LengthCode(1).Start = 4: LengthCode(1).End = 4
    LengthCode(2).Bits = 0: LengthCode(2).Start = 5: LengthCode(2).End = 5
    LengthCode(3).Bits = 0: LengthCode(3).Start = 6: LengthCode(3).End = 6
    LengthCode(4).Bits = 0: LengthCode(4).Start = 7: LengthCode(4).End = 7
    LengthCode(5).Bits = 0: LengthCode(5).Start = 8: LengthCode(5).End = 8
    LengthCode(6).Bits = 0: LengthCode(6).Start = 9: LengthCode(6).End = 9
    LengthCode(7).Bits = 0: LengthCode(7).Start = 10: LengthCode(7).End = 10
    LengthCode(8).Bits = 1: LengthCode(8).Start = 11: LengthCode(8).End = 12
    LengthCode(9).Bits = 1: LengthCode(9).Start = 13: LengthCode(9).End = 14
    LengthCode(10).Bits = 1: LengthCode(10).Start = 15: LengthCode(10).End = 16
    LengthCode(11).Bits = 1: LengthCode(11).Start = 17: LengthCode(11).End = 18
    LengthCode(12).Bits = 2: LengthCode(12).Start = 19: LengthCode(12).End = 22
    LengthCode(13).Bits = 2: LengthCode(13).Start = 23: LengthCode(13).End = 26
    LengthCode(14).Bits = 2: LengthCode(14).Start = 27: LengthCode(14).End = 30
    LengthCode(15).Bits = 2: LengthCode(15).Start = 31: LengthCode(15).End = 34
    LengthCode(16).Bits = 3: LengthCode(16).Start = 35: LengthCode(16).End = 42
    LengthCode(17).Bits = 3: LengthCode(17).Start = 43: LengthCode(17).End = 50
    LengthCode(18).Bits = 3: LengthCode(18).Start = 51: LengthCode(18).End = 58
    LengthCode(19).Bits = 3: LengthCode(19).Start = 59: LengthCode(19).End = 66
    LengthCode(20).Bits = 4: LengthCode(20).Start = 67: LengthCode(20).End = 82
    LengthCode(21).Bits = 4: LengthCode(21).Start = 83: LengthCode(21).End = 98
    LengthCode(22).Bits = 4: LengthCode(22).Start = 99: LengthCode(22).End = 114
    LengthCode(23).Bits = 4: LengthCode(23).Start = 115: LengthCode(23).End = 130
    LengthCode(24).Bits = 5: LengthCode(24).Start = 131: LengthCode(24).End = 162
    LengthCode(25).Bits = 5: LengthCode(25).Start = 163: LengthCode(25).End = 194
    LengthCode(26).Bits = 5: LengthCode(26).Start = 195: LengthCode(26).End = 226
    LengthCode(27).Bits = 5: LengthCode(27).Start = 227: LengthCode(27).End = 257
    LengthCode(28).Bits = 0: LengthCode(28).Start = 258: LengthCode(28).End = 258
End Function
代码语言:javascript复制
Private Function InitDistanceCode()
    DistanceCode(0).Bits = 0: DistanceCode(0).Start = 1: DistanceCode(0).End = 1 '
    DistanceCode(1).Bits = 0: DistanceCode(1).Start = 2: DistanceCode(1).End = 2
    DistanceCode(2).Bits = 0: DistanceCode(2).Start = 3: DistanceCode(2).End = 3
    DistanceCode(3).Bits = 0: DistanceCode(3).Start = 4: DistanceCode(3).End = 4
    DistanceCode(4).Bits = 1: DistanceCode(4).Start = 5: DistanceCode(4).End = 6
    DistanceCode(5).Bits = 1: DistanceCode(5).Start = 7: DistanceCode(5).End = 8
    DistanceCode(6).Bits = 2: DistanceCode(6).Start = 9: DistanceCode(6).End = 12
    DistanceCode(7).Bits = 2: DistanceCode(7).Start = 13: DistanceCode(7).End = 16
    DistanceCode(8).Bits = 3: DistanceCode(8).Start = 17: DistanceCode(8).End = 24
    DistanceCode(9).Bits = 3: DistanceCode(9).Start = 25: DistanceCode(9).End = 32
    DistanceCode(10).Bits = 4: DistanceCode(10).Start = 33: DistanceCode(10).End = 48
    DistanceCode(11).Bits = 4: DistanceCode(11).Start = 49: DistanceCode(11).End = 64
    DistanceCode(12).Bits = 5: DistanceCode(12).Start = 65: DistanceCode(12).End = 96
    DistanceCode(13).Bits = 5: DistanceCode(13).Start = 97: DistanceCode(13).End = 128
    DistanceCode(14).Bits = 6: DistanceCode(14).Start = 129: DistanceCode(14).End = 192
    DistanceCode(15).Bits = 6: DistanceCode(15).Start = 193: DistanceCode(15).End = 256
    DistanceCode(16).Bits = 7: DistanceCode(16).Start = 257: DistanceCode(16).End = 384
    DistanceCode(17).Bits = 7: DistanceCode(17).Start = 385: DistanceCode(17).End = 512
    DistanceCode(18).Bits = 8: DistanceCode(18).Start = 513: DistanceCode(18).End = 768
    DistanceCode(19).Bits = 8: DistanceCode(19).Start = 769: DistanceCode(19).End = 1024
    DistanceCode(20).Bits = 9: DistanceCode(20).Start = 1025: DistanceCode(20).End = 1536
    DistanceCode(21).Bits = 9: DistanceCode(21).Start = 1537: DistanceCode(21).End = 2048
    DistanceCode(22).Bits = 10: DistanceCode(22).Start = 2049: DistanceCode(22).End = 3072
    DistanceCode(23).Bits = 10: DistanceCode(23).Start = 3073: DistanceCode(23).End = 4096
    DistanceCode(24).Bits = 11: DistanceCode(24).Start = 4097: DistanceCode(24).End = 6144
    DistanceCode(25).Bits = 11: DistanceCode(25).Start = 6145: DistanceCode(25).End = 8192
    DistanceCode(26).Bits = 12: DistanceCode(26).Start = 8193: DistanceCode(26).End = 12288
    DistanceCode(27).Bits = 12: DistanceCode(27).Start = 12289: DistanceCode(27).End = 16384
    DistanceCode(28).Bits = 13: DistanceCode(28).Start = 16385: DistanceCode(28).End = 24576
    DistanceCode(29).Bits = 13: DistanceCode(29).Start = 24577: DistanceCode(29).End = 32768
End Function

这个区间的扩展函数静态Huffman和动态Huffman是一致的。

0 人点赞