Compresses data using the ZLIB deflate algorithm.
Public Declare Function ZLIB_Deflate Lib "diCryptoSys.dll"
(ByRef lpOutput As Byte, ByVal nOutBytes As Long,
ByRef lpInput As Byte, ByVal nInputLen As Long) As Long
nRet = ZLIB_Deflate(lpOutput(0), nOutBytes, abInput(0), nInputLen)
' Note the "(0)" after the byte array parameters
long __stdcall ZLIB_Deflate(unsigned char *lpOutput, long nOutBytes, const unsigned char *lpInput, long nBytes);
The number of bytes successfully copied into the output buffer or the required size in bytes. If an error occurs, it returns a negative error code.
Public Function zlibDeflate
(lpInput() As Byte) As Byte()
To determine the required size of the output buffer, call the function with nOutBytes set to zero (or lpOutput set to NULL).
Dim strPlain As String Dim strBack As String Dim abPlain() As Byte Dim abCompressed() As Byte Dim nCompLen As Long Dim nUncompLen As Long Dim nRet As Long ' COMPRESSSION (deflation) ' Set the plaintext message strPlain = "hello, hello, hello. This is a 'hello world' message " & _ "for the world, repeat, for the world." ' Convert to an array of bytes abPlain = StrConv(strPlain, vbFromUnicode) nUncompLen = UBound(abPlain) + 1 ' Find required compressed length by calling with zero length value nCompLen = ZLIB_Deflate(0, 0, abPlain(0), nUncompLen) Debug.Print "COMPR-LEN=" & nCompLen ReDim abCompressed(nCompLen - 1) ' Now compress plaintext Call ZLIB_Deflate(abCompressed(0), nCompLen, abPlain(0), nUncompLen) ' Display compressed data in hex Debug.Print "COMPR-DATA=" & cnvHexStrFromBytes(abCompressed) ' DECOMPRESSSION (inflation) ' Uncompress the compressed data ' New in [v5.3]: we can find the uncompressed length from compressed data nUncompLen = ZLIB_Inflate(0, 0, abCompressed(0), nCompLen) Debug.Print "Uncompressed length = " & nUncompLen Debug.Assert (nUncompLen > 0) ReDim abPlain(nUncompLen - 1) nRet = ZLIB_Inflate(abPlain(0), nUncompLen, abCompressed(0), nCompLen) ' Convert back to a string strBack = StrConv(abPlain, vbUnicode) Debug.Print strBack
COMPR-LEN=68 COMPR-DATA=789CCB48CDC9C9D751C840A2F4144232328B15802851411D2CA2509E5F9493A2AE909B5A5C9C989EAA90965FA45092910A11D651284A2D484D2CD14115D6030086D11F4E Uncompressed length = 90 hello, hello, hello. This is a 'hello world' message for the world, repeat, for the world.
Dim strPlain As String
Dim lpToCompress() As Byte
Dim lpCompressed() As Byte
Dim lpUncompressed() As Byte
strPlain = "hello, hello, hello. This is a 'hello world' message " & _
"for the world, repeat, for the world."
lpToCompress = StrConv(strPlain, vbFromUnicode)
lpCompressed = zlibDeflate(lpToCompress)
Debug.Print "OK= " & "789CCB48CDC9C9D751C840A2F4144232328B15802851411D2CA2509E5F9493A2AE909B5A5C9C989EAA90965FA45092910A11D651284A2D484D2CD14115D6030086D11F4E"
Debug.Print "COMPRESSED=" & cnvHexStrFromBytes(lpCompressed)
lpUncompressed = zlibInflate(lpCompressed)
Debug.Print "'" & StrConv(lpUncompressed, vbUnicode); "'"