'类中压缩与解压算法
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
新闻热点
疑难解答