home *** CD-ROM | disk | FTP | other *** search
- {*
- Xceed Streaming Compression Library - Memory Compress sample
- Copyright (c) 2001 Xceed Software Inc.
-
- [unMemComp.pas]
-
- This form module contains the main form's code. It demonstrates how to
- compress a chunk of memory data using different kinds of compression formats,
- and decompress a compressed memory data. It specifically uses:
- - The Compress and Decompress method.
- - The CompressionFormat property.
-
- This file is part of the Xceed Streaming Compression Library sample
- applications. The source code in this file is only intended as a supplement
- to Xceed Streaming Compression Library's documentation, and is provided
- "as is", without warranty of any kind, either expressed or implied.
- *}
-
- unit unMemComp;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ExtCtrls, OleCtrls, XceedStreamingCompressionLib_TLB;
-
- const
- sTextToCompress = 'This is a little test to show you how the memory ' +
- 'compression works. And it is very easy to use. ' +
- 'And there is some repeating repeating repeating ' +
- 'repeating repeating repeating repeating repeating text.';
-
- type
- TfrmMemComp = class(TForm)
- Label1 : TLabel;
- Label2 : TLabel;
- Label3 : TLabel;
- Label4 : TLabel;
- lblOrigSize : TLabel;
- lblCompSize : TLabel;
- Label5 : TLabel;
- lblDecompSize : TLabel;
- Label6 : TLabel;
- btCompress : TButton;
- btDecompress : TButton;
- mmoTextToCompress : TMemo;
- mmoDecompressedText : TMemo;
- xCompressor : TXceedStreamingCompression;
- cboCompressionFormat: TComboBox;
-
- procedure FormCreate(Sender: TObject);
- procedure btCompressClick(Sender: TObject);
- procedure btDecompressClick(Sender: TObject);
- procedure mmoTextToCompressChange(Sender: TObject);
- private
- { Private declarations }
- procedure PrepareFormat( nFormat : integer );
- public
- //Will contain the compressed byte array.
- m_vaCompressed : OleVariant;
- end;
-
- var
- frmMemComp: TfrmMemComp;
-
- implementation
-
- {$R *.DFM}
-
- {--------------------------------------------------------------}
- { Initialize the original size label. The combo box was filled }
- { manually using the property box }
- {--------------------------------------------------------------}
- procedure TfrmMemComp.FormCreate(Sender: TObject);
- begin
- mmoTextToCompress.Text := sTextToCompress;
- lblOrigSize.Caption := IntToStr( Length( sTextToCompress ) );
-
- cboCompressionFormat.ItemIndex := 0; //cfBZip2
- end;
-
- {--------------------------------------------------------------}
- { Create and prepare the compression method/format depending }
- { on the item selected in the combo box }
- {--------------------------------------------------------------}
- procedure TFrmMemComp.PrepareFormat( nFormat : integer );
- var
- xBZip2CompressioFormat : XceedBZip2CompressionFormat;
- xGZipCompressionFormat : XceedGZipCompressionFormat;
- xStandardCompressionFormat : XceedStandardCompressionFormat;
- xZip3CompressionFormat : XceedZip3CompressionFormat;
- xZLibCompressionFormat : XceedZLibCompressionFormat;
- xBWTCompressionFormat : XceedBWTCompressionMethod;
- xDeflateCompressionFormat : XceedDeflateCompressionMethod;
- xStoreCompressionFormat : XceedStoreCompressionMethod;
- begin
- case cboCompressionFormat.ItemIndex of
- 0 : begin
- xBZip2CompressioFormat := CoXceedBZip2CompressionFormat.Create();
- xCompressor.CompressionFormat := xBZip2CompressioFormat;
- end;
- 1 : begin
- xGZipCompressionFormat := CoXceedGZipCompressionFormat.Create;
- xCompressor.CompressionFormat := xGZipCompressionFormat;
- end;
- 2 : begin
- xStandardCompressionFormat := CoXceedStandardCompressionFormat.Create();
- xCompressor.CompressionFormat := xStandardCompressionFormat;
- end;
- 3 : begin
- xZip3CompressionFormat := CoXceedZip3CompressionFormat.Create();
- xCompressor.CompressionFormat := xZip3CompressionFormat;
- end;
- 4 : begin
- xZLibCompressionFormat := CoXceedZLibCompressionFormat.Create();
- xCompressor.CompressionFormat := xZLibCompressionFormat;
- end;
-
- // The next three items are not compression formats. They are compression methods.
- // You can assign them directly to the CompressionFormat property, to get
- // unformatted compressed streams (no header, footer, checksum, etc).
- 5 : begin
- xBWTCompressionFormat := CoXceedBWTCompressionMethod.Create();
- xCompressor.CompressionFormat := xBWTCompressionFormat;
- end;
- 6 : begin
- xDeflateCompressionFormat := CoXceedDeflateCompressionMethod.Create();
- xCompressor.CompressionFormat := xDeflateCompressionFormat;
- end;
- 7 : begin
- xStoreCompressionFormat := CoXceedStoreCompressionMethod.Create();
- xCompressor.CompressionFormat := xStoreCompressionFormat;
- end;
- end;
- end;
-
- {--------------------------------------------------------------}
- { Compress the text }
- {--------------------------------------------------------------}
- procedure TfrmMemComp.btCompressClick(Sender: TObject);
- var
- vaSource : OleVariant;
- begin
- vaSource := mmoTextToCompress.Text;
- PrepareFormat( cboCompressionFormat.ItemIndex );
- try
- //Compress the data in a single block (bEndOfData is true)
- m_vaCompressed := xCompressor.Compress( vaSource, true );
-
- if ( VarIsEmpty( m_vaCompressed ) ) then
- //No output was produced
- lblCompSize.Caption := IntToStr( 0 )
- else
- //Display the compressed byte array size.
- lblCompSize.Caption := IntToStr( VarArrayHighBound( m_vaCompressed, 1 )
- - VarArrayLowBound( m_vaCompressed, 1 ) + 1 );
- except
- on xErr: Exception do
- ShowMessage( 'Error during compression : ' + xErr.Message );
- end;
- end;
-
- {--------------------------------------------------------------}
- { Decompress the byte array (compressed data) }
- {--------------------------------------------------------------}
- procedure TfrmMemComp.btDecompressClick(Sender: TObject);
- var
- vaDecompressed : OleVariant;
- begin
- if VarIsEmpty( m_vaCompressed ) then
- //The user did not performed a compression first. Refuse to Decompress
- exit;
-
- PrepareFormat( cboCompressionFormat.ItemIndex );
-
- try
- //Decompress the compressed byte array in a single block
- //(bEndOfData is true).
- vaDecompressed := xCompressor.Decompress( m_vaCompressed, true );
- mmoDecompressedText.Text := VarToStr( vaDecompressed );
- lblDecompSize.Caption := IntToStr( Length( VarToStr( vaDecompressed ) ) );
- except
- on xErr: Exception do
- ShowMessage( 'Error during decompression : ' + xErr.Message );
- end;
- end;
-
- {--------------------------------------------------------------}
- { Update the original size label when the user modifies the }
- { text to compress text box }
- {--------------------------------------------------------------}
- procedure TfrmMemComp.mmoTextToCompressChange(Sender: TObject);
- begin
- lblOrigSize.Caption := IntToStr( Length( mmoTextToCompress.Text ) );
- end;
-
- end.
-