CryptoSys API examples VB6 to VB.NET

ZLIB_Deflate

Compresses data using the ZLIB deflate algorithm.

VB6/VBA

Debug.Print "Testing ZLIB_Deflate ..."
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

Output

Testing ZLIB_Deflate ...
Compressed 90 bytes to 68
hello, hello, hello. This is a 'hello world' message for the world, repeat, for the world.

VB.NET

Console.WriteLine("Testing ZLIB_Deflate ...")
Dim strPlain As String
Dim strBack As String
Dim abPlain() As Byte
Dim abCompressed() As Byte
''Dim nCompLen As Integer
Dim nUncompLen As Integer
''Dim nRet As Integer

' 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 = System.Text.Encoding.Default.GetBytes(strPlain)
nUncompLen = abPlain.Length
' Find required compressed length by calling with zero length value
' [VB.NET] This step not required in .NET
' Now compress plaintext
abCompressed = Zlib.Deflate(abPlain)
Console.WriteLine("Compressed " & abPlain.Length & " bytes to " & abCompressed.Length)

' DECOMPRESSSION (inflation)

' Uncompress the compressed data
' Note: we must know the uncompressed length here
''ReDim abPlain(nUncompLen - 1)
abPlain = Zlib.Inflate(abCompressed, nUncompLen)

' Convert back to a string
strBack = System.Text.Encoding.Default.GetString(abPlain)
Console.WriteLine(strBack)

[Contents]


Copyright © 2009 D.I. Management Services Pty Limited ABN 78 083 210 584, Australia. All rights reserved.
<www.di-mgt.com.au> <www.cryptosys.net>

[HOME]

Copyright © 2009-20 D.I. Management Services Pty Ltd. All rights reserved.