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

Class that implements run length encoding (RLE) to compress files and strings with VB6 and VBA.

Run Length Encoding algorithms reduce strings of recurring characters to a single character, followed by a count of how many times the character occurred.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CRLE class.
InputFileName Property Get the input file name.
OutputFileName Property Get the output file name.
Compress Method Compress the input file to the output file.
CompressString Method Compress an input string.
Decompress Method Decompress the input file to the output file.
DecompressString Method Decompress a string that was previously compressed using the CompressString method of this class.
dblToLong Private Perform an unsigned conversion from a double value to a long value. This function correctly handles doubles greater than 2,147,483,647 and less than or equal to 4,294,967,295.
ExpandBufferRun Private Write a run of bytes to a buffer.
ExpandRun Private Write a run of bytes to the file.
HiByte Private Get the high byte of the passed integer.
HiWord Private Get the high integer of the passed long.
IntToLong Private Convert an integer value to a long integer value, treating the integer as unsigned.
For example, performing the following assignment lngNumber = -1 assigns -1 to the variable lngNumber.
Assigning lngNumber = IntToLong(-1) assigns 65,535 to lngNumber.
LoByte Private Get the low byte of the passed integer.
LongToInt Private Perform an unsigned conversion from a long integer value to an integer value.
LoWord Private Get the low integer of the passed long.
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.
Shll Private Shift a long integer 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.
WriteBufferRun Private Write a run of bytes to the output buffer.
WriteRun Private Write a run of bytes to the file.
' Example of CRLE
'
' 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 mclsRLE As CRLE

Private Sub cmdTest_Click()
  ' Comments: Examples of compressing and decompressing files and strings using run length encoding (RLE)
  '           Run Length Encoding algorithms reduce strings of recurring characters to a single character,
  '           followed by a count of how many times the character occurred.

  Const cstrOriginal As String = mcstrSamplePath & "\original.bmp"
  Const cstrRLE As String = mcstrSamplePath & "\compress.RLE"
  Const cstrDecompress As String = mcstrSamplePath & "\decompress-rle.bmp"

  Dim dblCompressionRatio As Double
  Dim strString As String
  Dim strCompressed As String

  ' Initialize class
  Set mclsRLE = New CRLE

  ' Example of RLE encoding a file
  mclsRLE.InputFileName = cstrOriginal
  mclsRLE.OutputFileName = cstrRLE
  mclsRLE.Compress

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

  ' Example of RLE decoding a file
  mclsRLE.InputFileName = cstrRLE
  mclsRLE.OutputFileName = cstrDecompress
  mclsRLE.Decompress
  MsgBox cstrDecompress & " created by decompressing " & cstrRLE

  ' Example of compressing a string
  strString = "AAAABBBBCCCCDDDDDD"
  strCompressed = mclsRLE.CompressString(StrConv(strString, vbFromUnicode))

  ' Example of decompressing a string
  strString = StrConv(mclsRLE.DecompressString(strCompressed), vbUnicode)

  ' Display the compression ratio
  dblCompressionRatio = Len(strCompressed) / Len(strString)
  MsgBox "String compressed to " & Format$(dblCompressionRatio, "Percent") & " of its original size"

  ' Close
  Set mclsRLE = Nothing

End Sub

Private Sub mclsRLE_Progress(dblPercentage As Double)
  ' Comments: This procedure captures the Progress Event and reveals the percentage completion for the file that's being compressed or decoded.
  '           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