Class: Huffman in Category General VBA/VB6 : Encryption and Compression from Total Visual SourceBook

Class that implements Huffman compression for files in VBA and VB6.

Huffman compression uses character frequencies to compress data. Bit codes are assigned to each character, with shorter bitcodes for more common characters, and longer bitcodes for the less common characters.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CHuffman class.
InputFileName Property Get the name of the input file.
OutputFileName Property Get the name of the output file.
Compress Method Compresses the input file to the output file.
Decompress Method Decompresses the input file to the output file.
BuildTree Private Builds a Huffman tree based on the character frequency of the input data.
CompressByte Private Compresses a single byte.
Encode Private Huffman encodes a byte.
IntToByte Private Perform an unsigned conversion from an integer value to a byte value. This procedure correctly handles any integer value.
For example, lngNumber = -1 assigns -1 to the variable lngNumber. However, lngNumber = IntToLong(-1) assigns 65,535 to lngNumber.
LongToInt Private Perform an unsigned conversion from a long integer to an integer value.
ReadFile Private Read the specified number of bytes from the file. Using this function significantly increases the speed of processing files. The alternative to using a file buffer is reading a byte at a time from the file.
Shlb Private Shift a byte value left by the specified number of bits. Left shifting is a multiplication operation. For the number of bits to shift to the left, raise two to that power, then multiply the result by the original value.
WriteBit Private Write to the output file a bit at a time.
WriteFinish Private Flush any remaining data to the output file.
' Example of CHuffman
'
' To try this example, do the following:
' 1. Create a new form
' 2. Create a command button named 'cmdTest'
' 3. Paste all the code from this example to the new form's module.
' 4. Run the form

' This example assumes that the sample files are located in the folder named by the following constant.
Private Const mcstrSamplePath As String = "C:\TVSBSamp"

Private WithEvents mclsHuffman As CHuffman

Private Sub cmdTest_Click()
  ' Comments: Examples of compressing and decompressing files using Huffman compression algorithms
  '           Huffman compression uses character frequencies to compress data.
  '           Bit codes are assigned to each character, with shorter bitcodes for more common characters, and
  '           longer bitcodes for the less common characters.

  Const cstrOriginal As String = mcstrSamplePath & "\sample.txt"
  Const cstrHuffman As String = mcstrSamplePath & "\compress.huf"
  Const cstrDecompress As String = mcstrSamplePath & "\decompress-huffman.txt"

  Dim dblCompressionRatio As Double

  ' Initialize class
  Set mclsHuffman = New CHuffman

  ' Example Huffman encoding a file
  mclsHuffman.InputFileName = cstrOriginal
  mclsHuffman.OutputFileName = cstrHuffman
  mclsHuffman.Compress

  ' Display the compression achieved
  dblCompressionRatio = FileLen(cstrHuffman) / FileLen(cstrOriginal)
  MsgBox "File compressed to " & Format$(dblCompressionRatio, "Percent") & " of its original size"

  ' Example Huffman decoding a file
  mclsHuffman.InputFileName = cstrHuffman
  mclsHuffman.OutputFileName = cstrDecompress
  mclsHuffman.Decompress
  MsgBox cstrDecompress & " created by decompressing " & cstrHuffman

  ' Close class
  Set mclsHuffman = New CHuffman

End Sub

Private Sub mclsHuffman_FileProgress(dblPercentage As Double)
  ' Comments: This procedure captures the Progress Event and reveals the percentage completion for the file that's being compressed
  '           This example puts the information on the Immediate Window. You could show the information on a form as text or display a graphic.
  ' Params  : dblPercentage       Percent of completion between 0 and 1

  Debug.Print "Percent done: " & Format$(dblPercentage, "Percent")
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS