If you want to compress data without saving to the drive, for example if you want to compress, and send it right away on a socket over the net. NOTE, you need to send/save the original size of the data, with the compressed data.
VB Code:
  1. Private Declare Function Compress Lib "zlib.dll" Alias "compress" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long
  2. Private Declare Function Uncompress Lib "zlib.dll" Alias "uncompress" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long
  3.  
  4. Private Sub Form_Load()
  5.     Dim Str As String, CompStr As String
  6.     Dim InData() As Byte, OutData() As Byte, DecompData() As Byte, OriginalSize As Long
  7.    
  8.     Str = "Testing... blah blah blah .....   " & String(40, "A") & String(55, "B")
  9.     InData = StrConv(Str, vbFromUnicode)
  10.    
  11.     OriginalSize = UBound(InData) + 1
  12.    
  13.     If CompressData(InData, OutData) Then
  14.         ' save the data or something, you have to save the OriginalSize also
  15.         ' you need it when decompressing...
  16.     End If
  17.    
  18.     Debug.Print "Original: " & UBound(InData) + 1 & " Bytes", "Compressed: " & UBound(OutData) + 1 & " Bytes"
  19.     Debug.Print "Compression rate: " & Format((1 - (CDbl(UBound(OutData)) / UBound(InData))) * 100#, "##0.00")
  20.    
  21.     Erase InData
  22.    
  23.     ' read the data in OutData (in our case right now is actually the IN data)
  24.    
  25.     ReDim DecompData(OriginalSize - 1)
  26.     Uncompress DecompData(0), OriginalSize, OutData(0), UBound(OutData) + 1
  27.    
  28.     Debug.Print "Decompressed string:"
  29.     Debug.Print StrConv(DecompData, vbUnicode)
  30. End Sub
  31.  
  32. Public Function CompressData(InData() As Byte, OutData() As Byte) As Boolean
  33.     Dim BufferSize As Long
  34.     BufferSize = (UBound(InData) + 1) * 1.01 + 12
  35.  
  36.     ReDim OutData(BufferSize)
  37.  
  38.     CompressData = Compress(OutData(0), BufferSize, InData(0), UBound(InData) + 1) = 0
  39.  
  40.     ReDim Preserve OutData(BufferSize - 1)
  41. End Function