Xceed Zip Compression Library Documentation
Compress method example for VB
Example topics > Compress method example for VB

This example shows the use of the Compress and Uncompress methods for VB 6 users. Extra code is necessary in this example because it also shows you when and where to output the compressed code to a file, further process it by another function, or assign it to a 3rd party component property for file transfer. This is necessary because Variants are not correctly converted by VB into other types. The technique for converting the data ourselves into a format usable by commands such as VB's Get and Put is to use Byte arrays.

To use the example, place an XceedCompression control on a form and run the following code:

Visual Basic Copy Code

' The byte array which will contain the data to save or transfer:
Dim TempByteArray() As Byte  

' We'll put some text to compress here:
Dim OriginalData As Variant 

' Xceed Compression will place the compressed data here:
Dim CompressedData As Variant 

' To uncompress the data we'll put it in this variable:
Dim ToUncompress As Variant 

' Xceed Compression will place the uncompressed data here:
Dim UncompressedData As Variant 

' The result codes will be placed in this variable
Dim ResultCode As xcdCompressionError 

OriginalData = "Text or data to compress" 

Call XceedCompression1.License( "your license key" )

ResultCode = XceedCompression1.Compress(OriginalData, CompressedData, True)

' Make sure it worked successfully, display message
If ResultCode <> xerSuccess Then
   MsgBox "Compression unsuccessful. Error # " & Str(ResultCode) & " occurred. " & _
          "Description: " &  XceedCompression1.GetErrorDescription(ResultCode) 
Else 
   MsgBox "Compression successful." 
End If  

' Now the CompressedData variable is a Byte Array Variant, but it still cannot
' be assigned properly to other properties or used by the Get and Put method until
' we convert it to a real byte array as follows: 

ReDim TempByteArray(1 To UBound(CompressedData)) As Byte
TempByteArray = CompressedData 

' Ok, now you can do whatever you want with the compressed data located in the
' " TempByteArray" byte array variable (not a Variant!) 

' [Save, process or transfer your data here!] 

' Ok, now its time to decompress your saved data, so place it in the TempByteArray byte array variable. 

' [Load your saved or transferred compressed data here!] 

' Lets convert it to a Variant Byte Array before uncompressing it 

ToUncompress = TempByteArray 

' Now let's uncompress the data 

ResultCode = XceedCompression1.Uncompress(ToUncompress, UncompressedData, True) 

' UncompressedData variable should now contain same data as the OriginalData variable
' Check to see if it worked properly, display message 

If ResultCode <> xerSuccess Then
   MsgBox "Decompression unsuccessful. Error # " & Str(ResultCode) & " occurred. " & _
          "Description: " & XceedCompression1.GetErrorDescription(ResultCode) 
Else 
   MsgBox "Decompression successful." 
End If