TLzrw1: file compression component (V2.01.00)

Danny Heijl   [URL]

TLzrw1 compresseses a file with :
either the LZRW1/KH compression algorithm, with code posted by Kurt Haenen on the SWAG (lzrw1).
or the Japanese LZH compression algorithm
  • LZSS coded by Haruhiko OKUMURA
  • Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI
  • Edited and translated to English by Kenji RIKITAKE
  • Translated from C to Turbo Pascal by Douglas Webb 2/18/91
  • posted by Doug Webb on the SWAG (preskit2\lzh).
Visual feedback ((de)compression progress status) on a Panel if so desired.
  1. Installation Instructions
  2. Component Description
  3. Version History

Installation instructions :

  1. copy compon\*.* to your component directory
  2. For :
    16 bit : install tlzrw116.pas in your component library
    32 bit : install tlzrw132.pas in your component library
  3. rebuild the component library.

From this moment on you can excercize the test application compressp.dpr. Or you can play with the "tlzrw1" component itself.

Component Description :

Data types :

type TCompressMode = (Good, Fast, Auto);

There are three modes of compression :

  1. Good : the compression algorithm used is LZH. Takes longer but gives a very good compression ratio.
  2. Fast : the compression algorithm used if LZRW1/KH. This algorithm is simple and fast, but the compression ratio is not so good. Works very well for text files and some types of database files.
  3. Auto : the compression ratio is chosen by Tlzrw1. Two blocks (at 1/3 and 2/3 the file size) are compressed with LZRW1/KH (fast), and if both blocks compress more than a threshold (of default 40%), the "Fast" (LZRW1/KH) method is used. Otherwise the "Good" (LZH) method is used. You can specify the Threshold property if the default does not suit you.

Definition :

Tlzrw1 = class(TCustomPanel)

constructor Create(AOwner : TComponent); override;

The component can provide visual feedback on a panel if visible := True. So all properties/methods/events of Tpanel apply.
Only the properties applicable to the LZRW part of the component are listed here, not those of Tpanel.


property InputFile : String;

The file to be (de)compressed when UseStream is False.

property OutputFile : String;

The (de)compressed file when UseStream is False.

property InputStream : TStream;

The stream to be (de)compressed. Can be any descendant of TStream that implements Read, Write and Seek. You are responsible for positioning the stream before and after compression. Used instead of InputFile when UseStream is True.

property OutputStream : TStream;

The stream that the (de)compressed result gets written to. Can be any descendant of TStream that implements Read, Write and Seek. You are responsible for positioning the stream before and after compression. Used instead of OutputFile when UseStream is True.

property CompressMode : TCompressMode default Good;

This property should be set prior to calling the Compress method. Possible values : Good (default, LZH), Fast (LZRW1/KH), or Auto.
It is also returned by the Advise method.

property Visible;

When you set the visible property to True, TLzrw1 shows itself as a "statuspanel" on your form, that gives a visible progress indication when (de)compressing. The progress indicator is text-mode only, not a graphic gauge.

property UseStream : TStream;

When you set the UseStream property to True, TLzrw1 will use the InputStream and OutputStream properties to read and write, instead of using the InputFile and OutputFile properties.

property Threshold : Integer;

Specify the compression ratio (default is 40 %) that the Advise method and the compression with CompressMode = Auto use to choose between Fast (LZRW1/KH) and Good (LZH) compression. If compression with LZRW1/KH reduces the file size to more than Threshold (default 40) percent of it's original size, then Fast is advised or used.

All properties of the standard Tpanel.


function Compress : LongInt;

This function compresses the file specified in the Inputfile property to the file specified in the Outputfile property using the compression algorithm chosen in Compressmode property.
The return value is either the compressed file size if all goes well, or -1 if not.

function Decompress : Longint;

This function decompresses the file specified in the Inputfile property to the file specified in the Outputfile property using the algorithm used when compressing the file.
The return value is either the uncompressed file size, or -1 if something goes wrong.

function Advise : TcompressMode;

This function compresses a block at 1/3 of the file, and one at 2/3 of the file. If the combined compression ratio reduces the file size to more then Threshold (default 40) %, the fast (LZRW1/KH) compression method is advised by returning the "Fast" TCompressMode, otherwise "Good" is returned.

All methods of the standard Tpanel.


All events of the standard Tpanel.

Version History :

Version 1.01.00:

all VCL code by D. Heijl , may 8-9 1996

The Getblock/PutBlock procedures are based on the code in lzhtest.pas by Douglas Webb. The files lzh.pas and lzrw1kh.pas are essentially untouched (only some cosmetic changes, also added exceptions)

Version 1.01.01:

Bug discovered in LZRW1KH code by Dominque Willems, fixed may 97, Danny Heijl

Version 2.00.00:

Stream support added by Stefan Westner, modified slightly by Danny Heijl (May 1997). You can use any stream that implements the Read, Write and Seek methods.

I Also added a new "Threshold" property, and discovered that I introduced a new bug with the fix of 1.01.01 (signaled by David Mok ). This should now be fixed. (Danny Heijl, July 1997)

Version 2.01.00:

Component made thread-safe by "Paul Gertzen" .

Feel free to use or give away this software as you see fit. Just leave the credits in place if you alter the source.
This software is delivered to you "as is", i.e. no guarantees, it may blow up or trigger World War Three for all I know.
If you find any bugs and let me know, I will try to fix them. If you have good suggestions or made some useful changes, please post them to me, and I will incorporate them in a future release.
I believe in programmers around the world helping each other out by distributing free source code.
Reinventing the wheel can be a lot of fun, but is not always the best way to handle things. Also, someone else may have come up with an idea for much better wheels.

Danny Heijl, may 10 1996.