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

算法|压缩|优化

 
'类中压缩与解压算法

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

时间: 2024-09-29 11:35:05

一个优化后的压缩算法(下)的相关文章

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

算法|压缩|优化   这是一个在CSDN论坛中讨论过的压缩算法代码. 与WinRAR以最快方式压缩ZIP比较,255M的文件Level=0时 用时24.98秒 大小95.1MLevel=255时 用时30.24秒 大小91.6M WinRAR最快压缩ZIP 用时 25.2秒 大小58.6M标准RAR压缩,我看了一下,实在太慢,也就没试了,估计要几分钟才会有结果. 从速度看,基本持平了,这个算法虽然最大压缩能力有限,但感觉设计得很巧妙,每次都基于动态表,使软件可以做得很小巧,资源占用也很少.非常值

请问 如何实现在一个客户端处理数据后传递给下一个客户端 依次传递 直到任务完成

问题描述 请问 如何实现在一个客户端处理数据后传递给下一个客户端 依次传递 直到任务完成 一组数据 在一个客户端处理完后 再传递到下一个客户端 处理完后再传递给下一个客户端 依次类推 直到任务完结 客户端都是同一个, 这个该如何实现? 例如 有5个组 数据从一号组开始, 1号组处理完 ,就把数据传递给2号组(1号组没处理完时,2号组不会显示要处理的数据),2号处理完 再传递给3号,直到传到5号组结束. 数据是多组的 只有当前数据组处理完 才会处理下组数据. 求帮助啊, 该如何实现??? 解决方案

class-C#中使用Task类,如何调用完成后继续执行下一个Task?是threadpool么?

问题描述 C#中使用Task类,如何调用完成后继续执行下一个Task?是threadpool么? C#中使用Task类,如何调用完成后继续执行下一个Task?是threadpool么? 解决方案 http://www.cnblogs.com/x-xk/archive/2012/12/11/2804563.html

代码分析-做一个圆在按键按下后向右移动,放开后停止。每秒刷新一次。功能不对求教

问题描述 做一个圆在按键按下后向右移动,放开后停止.每秒刷新一次.功能不对求教 代码如下. void CHomeWorkView::OnTimer(UINT_PTR nIDEvent){ if (nIDEvent == 102) { Invalidate(); } CView::OnTimer(nIDEvent);} void CHomeWorkView::OnKeyDown(UINT nChar UINT nRepCnt UINT nFlags){ CDC* pDC=GetDC(); CPoi

java web-选择下拉框的一个选项后跳转时,怎么让跳转后的界面(原来界面刷新)的下拉框中元素是选择的那个

问题描述 选择下拉框的一个选项后跳转时,怎么让跳转后的界面(原来界面刷新)的下拉框中元素是选择的那个 请问一下,选择下拉框的一个选项后跳转时,怎么让跳转后的界面的下拉框中元素是选择的那个,以上是相关的js和jsp代码.然后我找了一下教程,在js中开头和结尾加了俩句,但是好像没用 解决方案 下拉列表的onchange处理函数see()方法中你取得下拉列表选中的值存储到type变量,使用url(window.location.href=...) 传递这个参数type=选中值,迁移到本画面后会刷新本画

android系统-android中当一个Activity启动后什么情况下会被destroy,什么情况下onstop

问题描述 android中当一个Activity启动后什么情况下会被destroy,什么情况下onstop android中当一个Activity启动后什么情况下会被destroy,什么情况下onstop 点击返回键是destroy还是onstop 如果是destroy那么什么情况下onstop 如果是onstop什么情况下destroy 解决方案 切换的时候会onstop,如果内存不足,程序出错,或者用户关闭,会destroy 解决方案二: 楼主你该重新学习下Activity的生命周期

请高手描述下,用.NET开发好一个网站后,发布到公共网络上的具体步骤

问题描述 如题:用.NET开发好一个网站后,发布到公共网络上的具体步骤.到底要怎么做,又应该如何维护,安全性方面要怎么做,谢谢了! 解决方案 解决方案二:这问题太大了,坐等呵,顶解决方案三:都没人回答吗??

当你在浏览器地址栏输入一个URL后回车,将会发生的事情?

这道题目没有所谓的完全的正确答案,这个题目可以让你在任意的一个点深入下去, 只要你对这个点是熟悉的.以下是一个大概流程: 浏览器向DNS服务器查找输入URL对应的IP地址. DNS服务器返回网站的IP地址. 浏览器根据IP地址与目标web服务器在80端口上建立TCP连接 浏览器获取请求页面的html代码. 浏览器在显示窗口内渲染HTML. 窗口关闭时,浏览器终止与服务器的连接. 这其中最有趣的是第1步和第2步(域名解析).我们输入的网址(域名)是IP地址的一个别名, 在一个DNS内,一个域名对应

网络推广站上线优化后的一些反思

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 公司最近上线了一个产品站与网络推广站,因为是新公司,资金较吃紧,订单就成为解决此问题的救命稻草.笔者在优化与推广这两个站时,特别是那个长沙网络推广网站,遇到了技术上,职业道德等方面的诸多问题,一个多月过去了,虽然取得了部分成绩,但仍然不理想,现在反思如下. 一.订单不能押宝于seo 笔者公司核心业务是一款汽车产品的销售与建站推广.公司做了两个