LZ compression, also known as sliding window compression, uses redundancy to compress data. As data is read, a dictionary of previous data is kept in memory. If a string of characters in the input data matches an entry in the dictionary, a code pointing to the dictionary entry is written to the output. If a match is not found in the dictionary the plain character is sent to the output.
Procedure Name | Type | Description |
(Declarations) | Declarations | Declarations and private variables for the CLZ class. |
InputFileName | Property | Get the name of the input file. |
OutputFileName | Property | Get the name of the output file. |
Compress | Method | Compress the input file to the output file. |
CompressString | Method | Compress a string. |
Decompress | Method | Decompress the input file to the output file. |
DecompressString | Method | Decompress a string that was previously compressed with the CompressString method. |
BitSetByte | Private | Set a bit in a byte variable. |
BufLength | Private | Get the length of an byte value. |
BufPosition | Private | Get the window position of an entry. |
CompressionInitialize | Private | Initialize the module variables for the compression and decompression routines. |
dblToLong | Private | Perform an unsigned conversion from a double value to a long integer value. This function correctly handles doubles greater than 2,147,483,647 and less than or equal to 4,294,967,295. |
DeletePosition | Private | Remove a character from the window. |
FindMatch | Private | Search for a match in the window. |
HiByte | Private | Get the high byte of the passed integer. |
HiWord | Private | Get the high integer of the passed long. |
InsertPosition | Private | Insert a character into the window. |
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. |
IntToLong | Private | Convert an integer value to a long value, treating the integer as unsigned. For example, lngNumber = -1 assigns -1 to the variable lngNumber. However, 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 value to an integer value. |
LoWord | Private | Get the low integer of the long integer. |
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. |
Shli | Private | Shift an 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. |
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. |
Shri | Private | Shift a long integer value right by the selected number of places. Right shifting is a division operation. For the number of bits to shift to the right, raise two to that power, then divide the original value by the result. |
WriteBufferByte | Private | Write a single byte to the output buffer. |
WriteBufferEntry | Private | Write a window entry to the output buffer. |
WriteBufferFinish | Private | Flush any remaining data to the output buffer. |
WriteByte | Private | Write a single byte to the output file. |
WriteEntry | Private | Write a window entry to the output file. |
WriteFinish | Private | Flush any remaining data to the output file. |
' Example of CLZ ' ' 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 mclsLZ As CLZ Private Sub cmdTest_Click() ' Comments: Examples of compressing and decompressing files and strings using Lempel-Ziv (LZ) compression ' LZ compression, also known as sliding window compression, uses redundancy to compress data. ' As data is read, a dictionary of previous data is kept in memory. ' If a string of characters in the input data matches an entry in the dictionary, a code pointing to the dictionary entry is written to the output. ' If a match is not found in the dictionary the plain character is sent to the output. Const cstrOriginal As String = mcstrSamplePath & "\sample.txt" Const cstrLZ As String = mcstrSamplePath & "\compress.lz" Const cstrDecompress As String = mcstrSamplePath & "\decompress-lz.txt" Dim dblCompressionRatio As Double Dim strString As String Dim strCompressed As String ' Initialize Set mclsLZ = New CLZ ' Example LZ compressing a file mclsLZ.InputFileName = cstrOriginal mclsLZ.OutputFileName = cstrLZ mclsLZ.Compress ' Display the compression ratio dblCompressionRatio = FileLen(cstrLZ) / FileLen(cstrOriginal) MsgBox "File compressed to " & Format$(dblCompressionRatio, "Percent") & " of its original size" ' Example LZ decompressing a file mclsLZ.InputFileName = cstrLZ mclsLZ.OutputFileName = cstrDecompress mclsLZ.Decompress MsgBox cstrDecompress & " created by decompressing " & cstrLZ ' Example of compressing a string strString = "One small step for [a] man; one giant leap for mankind" strCompressed = mclsLZ.CompressString(strString) ' Example of decompressing a string strString = mclsLZ.DecompressString(strCompressed) ' Display the compression ratio dblCompressionRatio = Len(strCompressed) / Len(strString) MsgBox "String compressed to " & Format$(dblCompressionRatio, "Percent") & " of its original size" ' Close Set mclsLZ = Nothing End Sub Private Sub mclsLZ_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
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!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"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