home *** CD-ROM | disk | FTP | other *** search
- {*
- Xceed Streaming Compression Library - Compression Manager sample
- Copyright (c) 2001 Xceed Software Inc.
-
- This sample demonstrates how to compress a file using different kinds of
- compression formats and methods, and how to decompress a compressed file.
- It specifically uses:
- - The ProcessFile 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 the Xceed Streaming Compression Library's documentation and is provided
- "as is" without warranty of any kind, either expressed or implied.
- *}
-
- unit unManager;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, XceedStreamingCompressionLib_TLB;
-
- type TCompressionFormat = ( cfBZip2, //0
- cfGZip, //1
- cfStandard, //2
- cfZip3, //3
- cfZLib, //4
- // The next 3 are not compression formats. They
- // are compression methods. See the comments in
- // the PrepareCompressionFormat function
- cfBWT, //5
- cfDeflate, //6
- cfStore ); //7
-
- type
- TfrmManager = class(TForm)
- Label1 : TLabel;
- Label2 : TLabel;
- Label3 : TLabel;
- Label4 : TLabel;
- cboCompressionFormat : TComboBox;
- edtSourceFile : TEdit;
- edtDestinationFile : TEdit;
- btBrowseForSource : TButton;
- btBrowseForDestination : TButton;
- btCompress : TButton;
- btDecompress : TButton;
- xOpenDialog : TOpenDialog;
- xSaveDialog : TSaveDialog;
- lstMessages : TListBox;
- procedure FormCreate(Sender: TObject);
- procedure btBrowseForDestinationClick(Sender: TObject);
- procedure btBrowseForSourceClick(Sender: TObject);
- procedure btCompressClick(Sender: TObject);
- procedure btDecompressClick(Sender: TObject);
- procedure edtSourceFileExit(Sender: TObject);
- procedure cboCompressionFormatChange(Sender: TObject);
- private
- { Private declarations }
- procedure SetDestinationFilename();
-
- function RemoveFileExtension( sFilename : string ) : string;
- function PrepareCompressionFormat( var xCompressor : TXceedStreamingCompression ) : boolean;
- function CompressFile( sSourceFilename : string;
- sCompressedFilename : string ) : boolean;
- function DecompressFile( sSourceFilename : string;
- sDecompressedFilename : string ) : boolean;
- public
- { Public declarations }
- end;
-
- var
- frmManager: TfrmManager;
-
- implementation
-
- {$R *.DFM}
-
- {-----------------------------------------------------------------------}
- { Assign a default value to the destination file name if the }
- { destination edit box is empty }
- {-----------------------------------------------------------------------}
- procedure TfrmManager.SetDestinationFilename();
- var
- sCompressedFilename : string;
- begin
- sCompressedFilename := edtDestinationFile.Text;
-
- if( Length( sCompressedFilename ) = 0 ) then
- begin
- sCompressedFilename := RemoveFileExtension( edtSourceFile.Text );
- if( Length( sCompressedFilename ) <> 0 ) then
- begin
- case TCompressionFormat( cboCompressionFormat.ItemIndex ) of
- cfBZip2 : edtDestinationFile.Text := sCompressedFilename + '.bz2';
- cfGZip : edtDestinationFile.Text := sCompressedFilename + '.gz';
- cfStandard : edtDestinationFile.Text := sCompressedFilename + '.std';
- cfZip3 : edtDestinationFile.Text := sCompressedFilename + '.zp3';
- cfZLib : edtDestinationFile.Text := sCompressedFilename + '.zl';
- cfBWT : edtDestinationFile.Text := sCompressedFilename + '.bwt';
- cfDeflate : edtDestinationFile.Text := sCompressedFilename + '.dfl';
- cfStore : edtDestinationFile.Text := sCompressedFilename + '.sto';
- end;
- end;
- end;
- end;
-
- {-----------------------------------------------------------------------}
- { Returns the path and file name without it's extension }
- {-----------------------------------------------------------------------}
- function TfrmManager.RemoveFileExtension( sFilename : string ) : string;
- var
- i : integer;
- nFilenameLen : integer;
- nLenToRemove : integer;
- begin
- nFilenameLen := Length( sFilename );
- i := nFilenameLen;
- nLenToRemove := -1;
-
- while ( i > 0 ) and ( nLenToRemove = -1 ) do
- begin
- if( copy( sFilename, i, 1 ) = '.' ) then
- nLenToRemove := i - 1;
-
- if( copy( sFilename, i, 1 ) = '\' ) then
- nLenToRemove := nFilenameLen;
-
- i := i - 1;
- end;
-
- if( nLenToRemove = -1 ) then
- RemoveFileExtension := ''
- else
- RemoveFileExtension := copy( sFilename, 0, nLenToRemove );
- end;
-
- {-----------------------------------------------------------------------}
- { Prepare the compression format according to the user selection. }
- { Return True if all succeeded. }
- {-----------------------------------------------------------------------}
- function TFrmManager.PrepareCompressionFormat( var xCompressor : TXceedStreamingCompression ) : boolean;
- var
- xBZip2 : DXceedBZip2CompressionFormat;
- xGZip : DXceedGZipCompressionFormat;
- xStandard : DXceedStandardCompressionFormat;
- xZip3 : DXceedZip3CompressionFormat;
- xZLib : DXceedZLibCompressionFormat;
- xBWT : DXceedBWTCompressionMethod;
- xDeflate : DXceedDeflateCompressionMethod;
- xStore : DXceedStoreCompressionMethod;
- begin
- // We instantiate a new compression format, assigning it directly to
- // the CompressionFormat of the XceedStreamingCompression object.
- try
- case TCompressionFormat( cboCompressionFormat.ItemIndex ) of
- cfBZip2 : begin
- xBZip2 := CoXceedBZip2CompressionFormat.Create();
- xCompressor.CompressionFormat := xBZip2;
- end;
- cfGZip : begin
- xGZip := CoXceedGZipCompressionFormat.Create();
- xCompressor.CompressionFormat := xGZip;
- end;
- cfStandard : begin
- xStandard := CoXceedStandardCompressionFormat.Create();;
- xCompressor.CompressionFormat := xStandard;
- end;
- cfZip3 : begin
- xZip3 := CoXceedZip3CompressionFormat.Create();
- xCompressor.CompressionFormat := xZip3;
- end;
- cfZLib : begin
- xZLib := CoXceedZLibCompressionFormat.Create();
- xCompressor.CompressionFormat := xZLib;
- end;
- // The next three items are not compression formats. They are compression
- // methods that can be assigned to the CompressionFormat property of the
- // Xceed Streaming Compression object. In these cases, the resulting
- // compressed streams will have no formatting (header, footer, checksum...)
-
- cfBWT : begin
- xBWT := CoXceedBWTCompressionMethod.Create();
- xCompressor.CompressionFormat := xBWT;
- end;
- cfDeflate : begin
- xDeflate := CoXceedDeflateCompressionMethod.Create();
- xCompressor.CompressionFormat := xDeflate;
- end;
- // Using store as the compression format will produce an output compressed
- // stream identical to the text to compress!
- cfStore : begin
- xStore := CoXceedStoreCompressionMethod.Create();
- xCompressor.CompressionFormat := xStore;
- end;
- end;
- PrepareCompressionFormat := true;
- except
- on xErr: Exception do
- begin
- lstMessages.Items.Add( 'PREPARE FORMAT : ' + xErr.Message );
- PrepareCompressionFormat := false;
- end;
- end;
- end;
-
- {-----------------------------------------------------------------------}
- { Perform the actual compression of a source file to a destination file }
- {-----------------------------------------------------------------------}
- function TfrmManager.CompressFile( sSourceFilename : string;
- sCompressedFilename : string ) : boolean;
- var
- xCompressor : TXceedStreamingCompression;
- vaBytesRead : OleVariant;
- begin
- // Initialize the Compressor and set the compression format the user chose
- xCompressor := TXceedStreamingCompression.Create( self );
-
- if( PrepareCompressionFormat( xCompressor ) ) then
- begin
- lstMessages.Clear();
- try
- // Process the file:
- // - Compress the entire file (no offset or size)
- // - Compress it in a single call (bEndOfData is TRUE)
- // - Overwrite the destination file (bAppend is FALSE)
- xCompressor.ProcessFile( sSourceFilename, 0, 0, cfpCompress, true,
- sCompressedFilename, false, vaBytesRead );
-
- lstMessages.Items.Add( sSourceFilename + ' successfully compressed in ' +
- sCompressedFilename );
- except
- on xErr: Exception do
- lstMessages.Items.Add( 'COMPRESS : ' + xErr.Message );
- end;
- end;
- CompressFile := true;
- xCompressor.Free();
- end;
-
- {-----------------------------------------------------------------------}
- { Perform the actual decompression of a source file to a destination }
- { file. }
- {-----------------------------------------------------------------------}
- function TfrmManager.DecompressFile( sSourceFilename : string;
- sDecompressedFilename : string ) : boolean;
- var
- xCompressor : TXceedStreamingCompression;
- vaBytesRead : OleVariant;
- begin
- // Initialize the Compressor and set the compression format the user chose
- xCompressor := TXceedStreamingCompression.Create( self );
-
- if( PrepareCompressionFormat( xCompressor ) ) then
- begin
- lstMessages.Clear();
-
- try
- // Process the file:
- // - Decompress the entire file (no offset or size)
- // - Decompress it in a single call (bEndOfData is TRUE)
- // - Overwrite the destination file (bAppend is FALSE)
- xCompressor.ProcessFile( sSourceFilename, 0, 0, cfpDecompress, true,
- sDecompressedFilename, false, vaBytesRead );
-
- lstMessages.Items.Add( sSourceFilename + ' successfully decompressed in ' +
- sDecompressedFilename );
- except
- on xErr: Exception do
- lstMessages.Items.Add( 'DECOMPRESS : ' + xErr.Message );
- end;
- end;
- DecompressFile := true;
- xCompressor.Free();
- end;
-
- {-----------------------------------------------------------------------}
- { Initialize the form's controls. For the purposes of this example, }
- { the combo box was filled from Delphi's property menu. }
- {-----------------------------------------------------------------------}
- procedure TfrmManager.FormCreate(Sender: TObject);
- begin
- cboCompressionFormat.ItemIndex := 0;
- end;
-
- {-----------------------------------------------------------------------}
- { Let the user select a destination file name and path using a file }
- { save dialog. }
- {-----------------------------------------------------------------------}
- procedure TfrmManager.btBrowseForDestinationClick(Sender: TObject);
- begin
- xSaveDialog.Files.Clear();
- xSaveDialog.Title := 'Destination File';
- xSaveDialog.Filter := 'Compressed (*.bz2;*.gz;*.std;*.zp3;*.zl;*.bwt;' +
- '*.dfl;*.sto)|*.bz2;*.gz;*.std;*.zp3;*.zl;*.bwt;' +
- '*.dfl;*.sto|All Files (*.*)|*.*';
- xSaveDialog.FilterIndex := 0;
-
- if( xSaveDialog.Execute ) then
- edtDestinationFile.Text := trim( xSaveDialog.Files.Text );
- end;
-
- {-----------------------------------------------------------------------}
- { Let the user select a source file name and path using a file open }
- { dialog. }
- {-----------------------------------------------------------------------}
- procedure TfrmManager.btBrowseForSourceClick(Sender: TObject);
- begin
- xOpenDialog.Files.Clear();
- xOpenDialog.Title := 'Source File';
- xOpenDialog.Filter := 'All Files (*.*)';
- xOpenDialog.FilterIndex := 0;
-
- if( xOpenDialog.Execute ) then
- begin
- edtSourceFile.Text := trim( xopenDialog.Files.Text );
- SetDestinationFilename();
- end;
- end;
-
- {-----------------------------------------------------------------------}
- { Compress the selected source file to the destination file }
- {-----------------------------------------------------------------------}
- procedure TfrmManager.btCompressClick(Sender: TObject);
- begin
- if( CompressFile( edtSourceFile.Text, edtDestinationFile.Text ) ) then
- begin
- // If the compression is successful, empty the source and destination
- // edit boxes to simplify subsequent compression/decompression
- edtSourceFile.Text := '';
- edtDestinationFile.Text := '';
- end;
- end;
-
- {-----------------------------------------------------------------------}
- { Decompress the selected source file to the specified destination file }
- {-----------------------------------------------------------------------}
- procedure TfrmManager.btDecompressClick(Sender: TObject);
- begin
- if( DecompressFile( edtSourceFile.Text, edtDestinationFile.Text ) ) then
- begin
- // If the compression is successful, empty the source and destination
- // edit boxes to simplify subsequent compression/decompression
- edtSourceFile.Text := '';
- edtDestinationFile.Text := '';
- end;
- end;
-
- {-----------------------------------------------------------------------}
- { Initialize the destination file to a default value if the destination }
- { edit box is empty. }
- {-----------------------------------------------------------------------}
- procedure TfrmManager.edtSourceFileExit(Sender: TObject);
- begin
- SetDestinationFilename();
- end;
-
- {-----------------------------------------------------------------------}
- { Initialize the destination file to a default value if the destination }
- { edit box is empty. }
- {-----------------------------------------------------------------------}
- procedure TfrmManager.cboCompressionFormatChange(Sender: TObject);
- begin
- SetDestinationFilename();
- end;
-
- end.
-