分享
 
 
 

一个优化后的压缩算法(下)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

'类中压缩与解压算法

Private Sub Compress()

Dim lngTemp As Long, intCount As Integer

Dim intBufferLocation As Integer

Dim intMaxLen As Integer

Dim intNext As Integer

Dim intPrev As Integer

Dim intMatchPos As Integer

Dim intMatchLen As Integer

Dim intInputFile As Integer

Dim intOutputFile As Integer

Dim aintWindowNext(mcintWindowSize + 1 + mcintWindowSize) As Integer

Dim aintWindowPrev(mcintWindowSize + 1) As Integer

Dim intByteCodeWritten As Long

Dim intBitCount As Integer

Dim abytWindow(mcintWindowSize + mcintMaxMatchLen) As Byte

Dim udtFileH As FileHeader

Dim strOutTmpFile As String

Dim lngBytesRead As Long

Dim lngFileLength As Long

Dim lngCurWritten As Long

Dim lngInBufLen As Long, abytInputBuffer() As Byte, abytOutputBuffer() As Byte

Dim lngOutBufLen As Long, lngInPos As Long, lngOutPos As Long

Dim intErrNo As Integer

On Error GoTo PROC_ERR

m_bEnableProcss = True

If Len(Dir(m_strInputFileName)) = 0 Or Len(m_strInputFileName) = 0 Then intErrNo = 1: GoTo PROC_ERR

If Len(m_strOutputFileName) = 0 Then m_strOutputFileName = m_strInputFileName

strOutTmpFile = m_strOutputFileName & ".tmp"

If Len(Dir(strOutTmpFile)) > 0 Then Kill strOutTmpFile

If FileLen(m_strInputFileName) < 100 Then intErrNo = 2: GoTo PROC_ERR

intInputFile = FreeFile

Open m_strInputFileName For Binary Access Read As intInputFile

Get intInputFile, , udtFileH

Seek #intInputFile, 1

If udtFileH.HeaderTag = mcstrSignature Then intErrNo = 3: GoTo PROC_ERR

intOutputFile = FreeFile

Open strOutTmpFile For Binary As intOutputFile

For intCount = 0 To mcintWindowSize

aintWindowPrev(intCount) = mcintNull

abytWindow(intCount) = &H20

Next

CopyMemory aintWindowNext(0), aintWindowPrev(0), (mcintWindowSize + 1) * 2

CopyMemory aintWindowNext(mcintWindowSize + 1), aintWindowPrev(0), mcintWindowSize * 2

CopyMemory abytWindow(mcintWindowSize + 1), abytWindow(0), mcintMaxMatchLen - 1

intByteCodeWritten = 1

lngFileLength = LOF(intInputFile)

lngInBufLen = &HA000&

lngOutBufLen = &HA000&

If lngInBufLen > lngFileLength Then lngInBufLen = lngFileLength

ReDim abytInputBuffer(lngInBufLen - 1)

ReDim abytOutputBuffer(lngOutBufLen + 17)

With udtFileH

.HeaderSize = Len(udtFileH)

lngCurWritten = .HeaderSize + 1

.HeaderTag = mcstrSignature

.FileLength = lngFileLength

.Version = App.Revision

.Flag = 0

End With

intMaxLen = mcintMaxMatchLen

lngBytesRead = mcintMaxMatchLen

lngInPos = mcintMaxMatchLen

intBitCount = 1

Put intOutputFile, , udtFileH

Get intInputFile, , abytInputBuffer

CopyMemory abytWindow(0), abytInputBuffer(0), mcintMaxMatchLen

CopyMemory abytWindow(mcintWindowSize), abytInputBuffer(0), mcintMaxMatchLen

Do While intMaxLen

intMatchPos = 0

intMatchLen = 0

intPrev = aintWindowNext(((&H100& * abytWindow(intBufferLocation + 1) + abytWindow(intBufferLocation)) And &HFFF) + mcintWindowSize + 1)

intCount = 0

Do Until intCount > mintCompressLevel Or intPrev = mcintNull

intNext = 0

Do While (abytWindow(intPrev + intNext) = abytWindow(intBufferLocation + intNext)) And intNext < mcintMaxMatchLen

intNext = intNext + 1

Loop

If intNext > intMatchLen Then

intMatchLen = intNext

intMatchPos = intPrev

If intNext = mcintMaxMatchLen Then

aintWindowNext(aintWindowPrev(intPrev)) = aintWindowNext(intPrev)

aintWindowPrev(aintWindowNext(intPrev)) = aintWindowPrev(intPrev)

aintWindowNext(intPrev) = mcintNull

aintWindowPrev(intPrev) = mcintNull

Exit Do

End If

End If

intPrev = aintWindowNext(intPrev)

intCount = intCount + 1

Loop

If intBitCount And &H100 Then

lngOutPos = intByteCodeWritten

If intByteCodeWritten > lngOutBufLen Then

Put intOutputFile, lngCurWritten, abytOutputBuffer

DoEvents

If m_bEnableProcss = False Then intErrNo = 254: GoTo PROC_ERR

lngCurWritten = lngCurWritten + intByteCodeWritten

lngOutPos = 0

End If

intByteCodeWritten = lngOutPos + 1

intBitCount = 1

abytOutputBuffer(lngOutPos) = 0

End If

If intMatchLen < mcintMinMatchLen Then

intMatchLen = 1

abytOutputBuffer(intByteCodeWritten) = abytWindow(intBufferLocation)

abytOutputBuffer(lngOutPos) = abytOutputBuffer(lngOutPos) Or intBitCount

End If

If intMatchLen > 1 Then

If intMatchLen > intMaxLen Then intMatchLen = intMaxLen

abytOutputBuffer(intByteCodeWritten) = intMatchPos And &HFF

intByteCodeWritten = intByteCodeWritten + 1

abytOutputBuffer(intByteCodeWritten) = (((intMatchPos \ 16) And &HF0) Or intMatchLen - mcintMinMatchLen) And &HFF

End If

intByteCodeWritten = intByteCodeWritten + 1

intBitCount = intBitCount * 2

Do While intMatchLen

intPrev = intBufferLocation + mcintMaxMatchLen

intNext = intPrev And &HFFF

If aintWindowPrev(intNext) <> mcintNull Then

aintWindowNext(aintWindowPrev(intNext)) = aintWindowNext(intNext)

aintWindowPrev(aintWindowNext(intNext)) = aintWindowPrev(intNext)

aintWindowNext(intNext) = mcintNull

aintWindowPrev(intNext) = mcintNull

End If

If lngInPos < lngInBufLen Then

abytWindow(intNext) = abytInputBuffer(lngInPos)

If intPrev >= mcintWindowSize Then abytWindow(intPrev) = abytInputBuffer(lngInPos)

lngBytesRead = lngBytesRead + 1

lngInPos = lngInPos + 1

If lngInPos >= lngInBufLen Then

If lngFileLength > lngBytesRead Then

If lngInBufLen > lngFileLength - lngBytesRead Then

lngInBufLen = lngFileLength - lngBytesRead

ReDim abytInputBuffer(lngInBufLen - 1)

End If

Get intInputFile, , abytInputBuffer

lngInPos = 0

RaiseEvent FileProgress(lngBytesRead / lngFileLength)

DoEvents

If m_bEnableProcss = False Then intErrNo = 254: GoTo PROC_ERR

End If

End If

End If

intPrev = ((&H100& * abytWindow(intBufferLocation + 1) + abytWindow(intBufferLocation)) And &HFFF) + mcintWindowSize + 1

intNext = aintWindowNext(intPrev)

aintWindowPrev(intBufferLocation) = intPrev

aintWindowNext(intBufferLocation) = intNext

aintWindowNext(intPrev) = intBufferLocation

If intNext <> mcintNull Then aintWindowPrev(intNext) = intBufferLocation

intBufferLocation = (intBufferLocation + 1) And &HFFF

intMatchLen = intMatchLen - 1

Loop

If lngInPos >= lngInBufLen Then intMaxLen = intMaxLen - 1

Loop

If intByteCodeWritten > 0 Then

ReDim Preserve abytOutputBuffer(intByteCodeWritten - 1)

Put intOutputFile, lngCurWritten, abytOutputBuffer

End If

Close intInputFile

Close intOutputFile

If Len(Dir(m_strOutputFileName)) > 0 Then Kill m_strOutputFileName

Name strOutTmpFile As m_strOutputFileName

RaiseEvent FileProgress(1)

Exit Sub

PROC_ERR:

Close intOutputFile

Close intInputFile

If Len(Dir(strOutTmpFile)) > 0 And Len(strOutTmpFile) > 0 Then Kill strOutTmpFile

If intErrNo = 0 Then intErrNo = 255

RaiseEvent ProcssError(LastError(intErrNo))

End Sub

Private Sub Decompress()

Dim intTemp As Integer

Dim intBufferLocation As Integer

Dim intLength As Integer

Dim bytHiByte As Integer

Dim bytLoByte As Integer

Dim intWindowPosition As Integer

Dim lngFlags As Long

Dim intInputFile As Integer

Dim intOutputFile As Integer

Dim abytWindow(mcintWindowSize + mcintMaxMatchLen) As Byte

Dim strOutTmpFile As String

Dim lngBytesRead As Long

Dim lngBytesWritten As Long

Dim lngFileLength As Long

Dim lngOriginalFileLen As Long

Dim lngInBufLen As Long, abytInBuf() As Byte, abytOutBuf() As Byte

Dim lngOutBufLen As Long, lngInPos As Long, lngOutPos As Long

Dim udtFileH As FileHeader

Dim intErrNo As Integer

On Error GoTo PROC_ERR

m_bEnableProcss = True

If Len(Dir(m_strInputFileName)) = 0 Or Len(m_strInputFileName) = 0 Then intErrNo = 4: GoTo PROC_ERR

If Len(m_strOutputFileName) = 0 Then m_strOutputFileName = m_strInputFileName

strOutTmpFile = m_strOutputFileName & ".tmp"

If Len(Dir(strOutTmpFile)) > 0 Then Kill strOutTmpFile

intInputFile = FreeFile

Open m_strInputFileName For Binary Access Read As intInputFile

lngFileLength = LOF(intInputFile)

Get intInputFile, , udtFileH

If udtFileH.HeaderTag = mcstrSignature And udtFileH.Version <= App.Revision Then

Seek #intInputFile, udtFileH.HeaderSize + 1

intOutputFile = FreeFile

Open strOutTmpFile For Binary As intOutputFile

lngOriginalFileLen = udtFileH.FileLength

lngFileLength = lngFileLength - udtFileH.HeaderSize

lngInBufLen = &H20000

lngOutBufLen = &H20000

If lngInBufLen > lngFileLength Then lngInBufLen = lngFileLength

ReDim abytInBuf(lngInBufLen - 1)

ReDim abytOutBuf(lngOutBufLen - 1)

Get intInputFile, , abytInBuf

Do While lngBytesWritten < lngOriginalFileLen

lngFlags = lngFlags \ 2

If (lngFlags And &H100) = 0 Then

lngFlags = &HFF00& Or abytInBuf(lngInPos)

lngBytesRead = lngBytesRead + 1

lngInPos = lngInPos + 1

If lngInPos >= lngInBufLen Then

If lngFileLength > lngBytesRead Then

If lngInBufLen > lngFileLength - lngBytesRead Then

lngInBufLen = lngFileLength - lngBytesRead

ReDim abytInBuf(lngInBufLen - 1)

End If

Get intInputFile, , abytInBuf

DoEvents

If m_bEnableProcss = False Then intErrNo = 254: GoTo PROC_ERR

lngInPos = 0

End If

End If

End If

If (lngFlags And 1) Then

abytWindow(intWindowPosition) = abytInBuf(lngInPos)

abytOutBuf(lngOutPos) = abytInBuf(lngInPos)

lngBytesRead = lngBytesRead + 1

lngInPos = lngInPos + 1

lngBytesWritten = lngBytesWritten + 1

lngOutPos = lngOutPos + 1

intWindowPosition = (intWindowPosition + 1) And &HFFF

If lngInPos >= lngInBufLen Then

If lngFileLength > lngBytesRead Then

If lngInBufLen > lngFileLength - lngBytesRead Then

lngInBufLen = lngFileLength - lngBytesRead

ReDim abytInBuf(lngInBufLen - 1)

End If

Get intInputFile, , abytInBuf

DoEvents

If m_bEnableProcss = False Then intErrNo = 254: GoTo PROC_ERR

lngInPos = 0

End If

End If

If lngOutPos >= lngOutBufLen Then

Put intOutputFile, , abytOutBuf

lngOutPos = 0

RaiseEvent FileProgress(lngBytesWritten / lngOriginalFileLen)

DoEvents

If m_bEnableProcss = False Then intErrNo = 254: GoTo PROC_ERR

End If

Else

bytHiByte = abytInBuf(lngInPos)

lngBytesRead = lngBytesRead + 1

lngInPos = lngInPos + 1

If lngInPos >= lngInBufLen Then

If lngFileLength > lngBytesRead Then

If lngInBufLen > lngFileLength - lngBytesRead Then

lngInBufLen = lngFileLength - lngBytesRead

ReDim abytInBuf(lngInBufLen - 1)

End If

Get intInputFile, , abytInBuf

DoEvents

If m_bEnableProcss = False Then intErrNo = 254: GoTo PROC_ERR

lngInPos = 0

End If

End If

bytLoByte = abytInBuf(lngInPos)

intBufferLocation = ((bytLoByte And &HF0) * 16 + bytHiByte) And &HFFF

intLength = (bytLoByte And &HF) + mcintMinMatchLen

lngBytesRead = lngBytesRead + 1

lngInPos = lngInPos + 1

If lngInPos >= lngInBufLen Then

If lngFileLength > lngBytesRead Then

If lngInBufLen > lngFileLength - lngBytesRead Then

lngInBufLen = lngFileLength - lngBytesRead

ReDim abytInBuf(lngInBufLen - 1)

End If

Get intInputFile, , abytInBuf

DoEvents

If m_bEnableProcss = False Then intErrNo = 254: GoTo PROC_ERR

lngInPos = 0

End If

End If

intTemp = intBufferLocation + intLength

Do While intBufferLocation < intTemp

abytOutBuf(lngOutPos) = abytWindow((intBufferLocation) And &HFFF)

abytWindow(intWindowPosition) = abytOutBuf(lngOutPos)

intBufferLocation = intBufferLocation + 1

lngBytesWritten = lngBytesWritten + 1

intWindowPosition = (intWindowPosition + 1) And &HFFF

lngOutPos = lngOutPos + 1

If lngOutPos >= lngOutBufLen Then

Put intOutputFile, , abytOutBuf

lngOutPos = 0

RaiseEvent FileProgress(lngBytesWritten / lngOriginalFileLen)

DoEvents

If m_bEnableProcss = False Then intErrNo = 254: GoTo PROC_ERR

End If

Loop

End If

Loop

If lngOutPos > 0 Then

ReDim Preserve abytOutBuf(lngOutPos - 1)

Put intOutputFile, , abytOutBuf

End If

Close intOutputFile

Else

intErrNo = 5

GoTo PROC_ERR

End If

Close intInputFile

If Len(Dir(m_strOutputFileName)) > 0 Then Kill m_strOutputFileName

Name strOutTmpFile As m_strOutputFileName

RaiseEvent FileProgress(1)

Exit Sub

PROC_ERR:

Close intOutputFile

Close intInputFile

If Len(Dir(strOutTmpFile)) > 0 And Len(strOutTmpFile) > 0 Then Kill strOutTmpFile

If intErrNo = 0 Then intErrNo = 255

RaiseEvent ProcssError(LastError(intErrNo))

End Sub

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有