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:
Private Declare Function Compress Lib "zlib.dll" Alias "compress" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long Private Declare Function Uncompress Lib "zlib.dll" Alias "uncompress" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long Private Sub Form_Load() Dim Str As String, CompStr As String Dim InData() As Byte, OutData() As Byte, DecompData() As Byte, OriginalSize As Long Str = "Testing... blah blah blah ..... " & String(40, "A") & String(55, "B") InData = StrConv(Str, vbFromUnicode) OriginalSize = UBound(InData) + 1 If CompressData(InData, OutData) Then ' save the data or something, you have to save the OriginalSize also ' you need it when decompressing... End If Debug.Print "Original: " & UBound(InData) + 1 & " Bytes", "Compressed: " & UBound(OutData) + 1 & " Bytes" Debug.Print "Compression rate: " & Format((1 - (CDbl(UBound(OutData)) / UBound(InData))) * 100#, "##0.00") Erase InData ' read the data in OutData (in our case right now is actually the IN data) ReDim DecompData(OriginalSize - 1) Uncompress DecompData(0), OriginalSize, OutData(0), UBound(OutData) + 1 Debug.Print "Decompressed string:" Debug.Print StrConv(DecompData, vbUnicode) End Sub Public Function CompressData(InData() As Byte, OutData() As Byte) As Boolean Dim BufferSize As Long BufferSize = (UBound(InData) + 1) * 1.01 + 12 ReDim OutData(BufferSize) CompressData = Compress(OutData(0), BufferSize, InData(0), UBound(InData) + 1) = 0 ReDim Preserve OutData(BufferSize - 1) End Function




Reply With Quote