Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the Zlib compression library to compress and decompress byte arrays
Keywordscompress, uncompress, decompress, zlib
CategoriesUtilities
 
Go to the Zlib for Windows 95 Web site and download zlib114dll.zip. Expand the archive and pull out zlib.dll. You should also probably download zlib114.zip so you can read the file zlib.h to figure out how things work.

Add Declare statements for the Zlib routines you want to use. This example uses the compress and uncompress routines.

The cmdCompress_Click event handler reads the file to compress into a byte array. It allocates an array to hold the compressed results and calls the library's compress function.

The cmdUncompress_Click event handler reads the compressed file into a byte array. It allocates an array to hold the uncompressed results and calls the library's uncompress function. Note that the program must somehow know how large the uncompressed file will be.

 
' Either place the library in the System directory
' or specify the full path to zlib.dll here.
Private Declare Function compress Lib "zlib.dll" (dest As _
    Any, destLen As Any, src As Any, ByVal srcLen As Long) _
    As Long
Private Declare Function uncompress Lib "zlib.dll" (dest As _
    Any, destLen As Any, src As Any, ByVal srcLen As Long) _
    As Long

Private Const Z_OK = 0
Private Const Z_DATA_ERROR = -3
Private Const Z_MEM_ERROR = -4
Private Const Z_BUF_ERROR = -5

Private Sub cmdCompress_Click()
Dim file_name As String
Dim fnum As Integer
Dim uncompressed_size As Long
Dim uncompressed_bytes() As Byte
Dim compressed_size As Long
Dim compressed_bytes() As Byte

    ' **************************************
    ' Load the uncompressed file into a byte array.
    file_name = txtUncompressed.Text
    uncompressed_size = FileLen(file_name)
    ReDim uncompressed_bytes(1 To uncompressed_size)

    fnum = FreeFile
    Open file_name For Binary Access Read As #fnum
    Get #fnum, , uncompressed_bytes()
    Close #fnum
    lblUncompressedSize.Caption = uncompressed_size & " " & _
        "bytes"

    ' **************************************
    ' Compress.

    ' Allocate the smallest allowed compression
    ' buffer (1% larger than the uncompressed data
    ' plus 12 bytes).
    compressed_size = 1.01 * uncompressed_size + 12
    ReDim compressed_bytes(1 To compressed_size)

    ' Compress the bytes.
    Select Case compress( _
            compressed_bytes(1), compressed_size, _
            uncompressed_bytes(1), uncompressed_size)
        Case Z_MEM_ERROR
            MsgBox "Insufficient memory", vbExclamation, _
                "Compression Error"
            Exit Sub
        Case Z_BUF_ERROR
            MsgBox "Buffer too small", vbExclamation, _
                "Compression Error"
            Exit Sub
        ' Else Z_OK.
    End Select

    ' Shrink the compressed buffer to fit.
    ReDim Preserve compressed_bytes(1 To compressed_size)

    ' **************************************
    ' Save the results into the output file.

    ' Remove the existing file.
    On Error Resume Next
    Kill txtCompressed.Text
    On Error GoTo 0

    ' Write the file.
    Open txtCompressed.Text For Binary Access Write As #fnum
    Put #fnum, , compressed_bytes()
    Close #fnum
    lblCompressedSize.Caption = compressed_size & " bytes"

    MsgBox "Done. Compressed " & uncompressed_size & _
        " --> " & compressed_size & " (" & _
        Format$(compressed_size / uncompressed_size * 100, _
            "0.00") & "%)"
    cmdUncompress.Enabled = True
End Sub

Private Sub cmdUncompress_Click()
Dim file_name As String
Dim fnum As Integer
Dim compressed_size As Long
Dim compressed_bytes() As Byte
Dim uncompressed_size As Long
Dim uncompressed_bytes() As Byte

    ' **************************************
    ' Load the file into a byte array.
    file_name = txtCompressed.Text
    compressed_size = FileLen(file_name)
    ReDim compressed_bytes(1 To compressed_size)

    fnum = FreeFile
    Open file_name For Binary Access Read As #fnum
    Get #fnum, , compressed_bytes()
    Close #fnum
    lblCompressedSize.Caption = compressed_size & " bytes"

    ' **************************************
    ' Uncompress.

    ' Allocate room for the uncompressed file.
    ' Note that this routine needs to know
    ' the original file's uncompressed size.
    uncompressed_size = Val(lblUncompressedSize.Caption)
    ReDim uncompressed_bytes(1 To uncompressed_size)

    ' Decompress the bytes.
    Select Case uncompress( _
            uncompressed_bytes(1), uncompressed_size, _
            compressed_bytes(1), compressed_size)
        Case Z_MEM_ERROR
            MsgBox "Insufficient memory", vbExclamation, _
                "Compression Error"
            Exit Sub
        Case Z_BUF_ERROR
            MsgBox "Buffer too small", vbExclamation, _
                "Compression Error"
            Exit Sub
        Case Z_DATA_ERROR
            MsgBox "Input file corrupted", vbExclamation, _
                "Compression Error"
            Exit Sub
        ' Else Z_OK.
    End Select

    ' **************************************
    ' Save the results into the output file.

    ' Remove the existing file.
    file_name = txtUncompressed.Text
    On Error Resume Next
    Kill file_name
    On Error GoTo 0

    ' Write the file.
    Open file_name For Binary Access Write As #fnum
    Put #fnum, , uncompressed_bytes()
    Close #fnum
    lblUncompressedSize.Caption = uncompressed_size & " " & _
        "bytes"

    MsgBox "Done. Uncompressed " & compressed_size & _
        " --> " & uncompressed_size & " (" & _
        Format$(uncompressed_size / compressed_size, _
            "0.00") & "x)"
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated