Data Compression With Zlib


Version Compatibility: Visual Basic 6

More information:
This module requires the ZLib DLL available at:

The following module contains two public functions:

Compress(DataOrText,Optional Key)
DataOrText: Either a byte array (1D) or a string
Key: Original size (required to uncompress) If not provided, the key will be embedded in the compressed data itself

Returns compressed byte array or string, respectively

Uncompress(DataOrText,Optional Byval Key)
Key: Original size. If not provided, Uncompress() will look for it in the compressed data.

Returns uncompressed byte array or string, respectively

About Keys...
In order to successfully uncompress compressed data, the underlying ZLib functions require the original size (of the uncompressed data.) To make things easier, these functions will automatically include this value in the compressed data so that you, the programmer, won't need to keep track of this information yourself. However, if you like, you can provide a Long variable to the Compress() function. This will tell the function to return it directly to you instead of adding it to the compressed data. But you must provide this value to the Uncompress() function or else it won't work.

Here's an example function:

Public Sub Example()
   Dim sTmp As String
   Dim lKey As Long
   sTmp = Compress("This is a test.")
   Debug.Print Len(sTmp), Len(Uncompress(sTmp))
   sTmp = Compress("This is a test.", lKey)
   Debug.Print Len(sTmp), Len(Uncompress(sTmp, lKey))

   sTmp = vbNullString
End Sub
Immediate window example:
call example
 25            15 
 21            15 
Anyway, I hope this is clear. If not, just follow the commented code =)

Instructions: Copy the declarations and code below and paste directly into your VB project.