' 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
|