先声明,原来的基础代码来自这里
http://www.vbcodelibrary.co.uk/modules.php?op=modload&name=Forums&file=viewtopic&topic=1836&forum=7
我只是在压缩的字节组中加入了文件的长度存储。免去了要另外记住文件长度的麻烦。
这个类的优点在于不使用系统外的 Dll 文件。对网络程序中通讯数据压缩很有用。
呵呵,我写东西就是要用到它~~
缺点也显而易见,
1,用 WinRAR 等软件无法打开压缩过的文件,
因为只有数据段,并没有索引段,我只加了个原始文件长度上去。
2,没有对大文件进行分段读取。
因为我的通讯数据都很小,没有必要分段压缩。
3,没有数据校验。
当您能将上面列出的问题解决了,你的程序就可以拿来买咯,弄到好就成了 WinZIP 了...
以下是源码:
Imports System.Runtime.InteropServices '使用该语句后可以使用 DllImport 来引用 Dll 内的函数Public Class cls_zLib <DllImport("zlib.dll")> Public Shared Function compress(ByVal dest As Byte(), ByRef destLen As Integer, ByVal src As Byte(), ByVal srcLen As Integer) As Integer '引用压缩函数 End Function <DllImport("zlib.dll")> Public Shared Function uncompress(ByVal dest As Byte(), ByRef destLen As Integer, ByVal src As Byte(), ByVal srcLen As Integer) As Integer '引用解压函数 End Function '压缩/解压错误信息 Public Enum enumzLibResult Z_NEED_DICT = 2 Z_STREAM_END = 1 Z_OK = 0 Z_ERRNO = -1 Z_STREAM_ERROR = -2 Z_DATA_ERROR = -3 Z_MEM_ERROR = -4 Z_BUF_ERROR = -5 Z_VERSION_ERROR = -6 Exception_Raised = -9 End Enum '压缩/解压结果 Public Structure struczLibResult Public intResult As enumzLibResult Public byteResult As Byte() Public excResult As Exception End Structure '压缩 Public Function ZIP(ByVal bySource As Byte()) As struczLibResult Dim strucReturn As New struczLibResult 'zLib result struct Dim intOrigSize As Integer 'Holds the bysource original uncompressed length Dim intCompSize As Integer 'Holds destination buffer size Try intOrigSize = bySource.Length 'Get orininal bySource size 'MsgBox(intOrigSize) Dim BLen() As Byte = BitConverter.GetBytes(intOrigSize) Dim n As Integer ReDim strucReturn.byteResult(Convert.ToInt32(intOrigSize + (intOrigSize * 0.01) + 12)) 'Resize destination buffer intCompSize = strucReturn.byteResult.Length 'Get buffer size '**Compress Byte Array** strucReturn.intResult = compress(strucReturn.byteResult, intCompSize, bySource, bySource.Length) ReDim Preserve strucReturn.byteResult(intCompSize + 4) 'Get rid of unused bytes 'MsgBox(intCompSize) For n = 1 To 4 strucReturn.byteResult(intCompSize + n) = BLen(n - 1) Next Return strucReturn 'Return completed structure Catch ex As Exception strucReturn.intResult = enumzLibResult.Exception_Raised 'Tell the caller that an exception occured strucReturn.excResult = ex 'Send back error Return strucReturn 'Return completed structure Finally '**Clean Up** Erase strucReturn.byteResult 'Erase all items in the array strucReturn = Nothing 'Mark struc for collection End Try End Function '解压 Public Function unZIP(ByVal bySource As Byte()) As struczLibResult Dim intOrigSize As Integer = bySource.Length - 4 Dim n As Integer Dim BLen(3) As Byte For n = 0 To 3 BLen(n) = bySource(intOrigSize + n) Next intOrigSize = BitConverter.ToInt32(BLen, 0) MsgBox(intOrigSize) Dim strucReturn As New struczLibResult 'zLib result struct Try ReDim strucReturn.byteResult(intOrigSize - 1) 'Resize buffer to support the original size '**Decompress Byte Array** strucReturn.intResult = uncompress(strucReturn.byteResult, intOrigSize, bySource, bySource.Length * 2) Return strucReturn 'Return completed structure Catch ex As Exception strucReturn.intResult = enumzLibResult.Exception_Raised 'Tell the caller that an exception occured strucReturn.excResult = ex 'Send back error Return strucReturn 'Return completed structure Finally '**Clean Up** Erase strucReturn.byteResult 'Erase all items in the array strucReturn = Nothing 'Mark struc for collection End Try End FunctionEnd Class