Compresses data using the ZLIB deflate algorithm.
Public Declare Function ZLIB_Deflate Lib "diCryptoSys.dll"
(ByRef abOutput As Byte, ByVal nOutputLen As Long,
ByRef abInput As Byte, ByVal nInputLen As Long) As Long
nRet = ZLIB_Deflate(abOutput(0), nOutputLen, abInput(0), nInputLen)
Byte array to be filled with
compressed data.Long specifying the size of the output buffer
in bytes, or zero to query the required output length without writing to the output buffer.Byte array containing the input data
to be compressed.Long specifying the number of bytes
in the input buffer.
long _stdcall ZLIB_Deflate(unsigned char *output, long out_len, const unsigned char *input, long in_len);
Long: the number of bytes successfully copied into the
output buffer or the required size in bytes. If an error occurs,
it returns the negative value of a non-zero error code.
Call the ZLIB_Deflate function with a zero output length to find out the
required size of the output buffer (set abOutput to zero in this case).
VB6/VBA users: Note the '(0)' in abOutput(0) and abInput(0).
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)
ReDim abCompressed(nCompLen - 1)
' Now compress plaintext
Call ZLIB_Deflate(abCompressed(0), nCompLen, abPlain(0), nUncompLen)
Debug.Print "Compressed " & nUncompLen & " bytes to " & nCompLen
' DECOMPRESSSION (inflation)
' Uncompress the compressed data
' Note: we must know the uncompressed length here
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