vb 操作zlib需要stdcall 的dll,所以dll不对就会出错。
简单版本
Option Explicit
Public Const Z_OK = 0
' Compression levels
Public Const Z_NO_COMPRESSION As Long = 0
Public Const Z_BEST_SPEED As Long = 1
Public Const Z_BEST_COMPRESSION As Long = 9
Public Const Z_DEFAULT_COMPRESSION As Long = (-1)
Public Declare Function compress Lib "zlib.dll" (ByVal dest As Long,ByRef destLen As Long,ByVal Source As Long,ByVal sourceLen As Long) As Long
Public Declare Function compress2 Lib "zlib.dll" (ByVal dest As Long,ByVal sourceLen As Long,ByVal level As Long) As Long
Public Declare Function compressBound Lib "zlib.dll" (ByVal sourceLen As Long) As Long
Public Declare Function uncompress Lib "zlib.dll" (ByVal dest As Long,ByVal sourceLen As Long) As Long
'ZEXTERN int ZEXPORT compress OF((Bytef *dest,uLongf *destLen,
' const Bytef *source,uLong sourceLen));
'压缩
Public Function ZLIB_Compress(bIn() As Byte,bOut() As Byte) As Long
' On Error Resume Next
Dim bSize As Long
Dim lngInSize As Long
Dim lngOutSize As Long
Dim ret As Long
lngInSize = UBound(bIn) + 1
' MsgBox compressBound(lngInSize)
lngOutSize = (lngInSize * 1.01) + 12
'MsgBox lngOutSize
ReDim bOut(lngOutSize - 1) As Byte
ret = compress(VarPtr(bOut(0)),VarPtr(lngOutSize),VarPtr(bIn(0)),lngInSize)
' Debug.Print ret
ReDim Preserve bOut(lngOutSize - 1)
ZLIB_Compress = lngOutSize
End Function
'解压缩数组
Public Function ZLIB_UnCompress(bIn() As Byte,bOut() As Byte) As Long
' On Error Resume Next
Dim bSize As Long
Dim lngInSize As Long
Dim lngOutSize As Long
Dim ret As Long
lngInSize = UBound(bIn) + 1
lngOutSize = (lngInSize * 15.01) + 12
ReDim bOut(lngOutSize - 1) As Byte
' ret = uncompress(0&,VarPtr(bIn(0)),lngInSize)
' Debug.Print ret
ret = uncompress(VarPtr(bOut(0)),lngOutSize,lngInSize)
Debug.Print ret
If ret = 0 Then
ReDim Preserve bOut(lngOutSize - 1)
ZLIB_UnCompress = lngOutSize
Else
ReDim Preserve bOut(0)
End If
End Function