home *** CD-ROM | disk | FTP | other *** search
- unit FileStream;
- {------------------------------------------------------------------------------}
- { Stream Compression sample using The Xceed Zip Compression Library 4 }
- { For Delphi 3, 4 and 5 }
- { Copyright (c) 1999 Xceed Software Inc. }
- {------------------------------------------------------------------------------}
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- OleCtrls, XceedZipLib_TLB, StdCtrls;
-
- type
- TfrmFileStream = class(TForm)
- GroupBox1: TGroupBox;
- Label1: TLabel;
- Label2: TLabel;
- edtCompSrc: TEdit;
- Label3: TLabel;
- edtCompDest: TEdit;
- btCompress: TButton;
- xComp: TXceedCompression;
- GroupBox2: TGroupBox;
- Label4: TLabel;
- edtUncompSrc: TEdit;
- Label5: TLabel;
- edtUncompDest: TEdit;
- btUncompress: TButton;
- procedure edtCompSrcChange(Sender: TObject);
- procedure edtCompDestChange(Sender: TObject);
- procedure btCompressClick(Sender: TObject);
- procedure btUncompressClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- frmFileStream: TfrmFileStream;
-
- implementation
-
- {$R *.DFM}
-
- procedure TfrmFileStream.edtCompSrcChange(Sender: TObject);
- begin
- edtUncompDest.Text := edtCompSrc.Text + '.Restored';
- end;
-
- procedure TfrmFileStream.edtCompDestChange(Sender: TObject);
- begin
- edtUncompSrc.Text := edtCompDest.Text;
- end;
-
- procedure TfrmFileStream.btCompressClick(Sender: TObject);
- var
- fsRead : TFileStream;
- fsWrite : TFileStream;
- lRead : LongInt;
- lToWrite : LongInt;
- lWritten : LongInt;
- pData : Pointer;
- vaSource : OleVariant;
- vaDest : OleVariant;
- xErr : xcdCompressionError;
- begin
- try
- fsRead := TFileStream.Create( edtCompSrc.Text, fmOpenRead );
- fsWrite := TFileStream.Create( edtCompDest.Text, fmCreate );
-
- if Assigned( fsRead ) and Assigned( fsWrite )then
- begin
- vaSource := VarArrayCreate( [0, 4095], varByte );
-
- repeat
- { Read some bytes from source file }
- pData := VarArrayLock( vaSource );
- lRead := fsRead.Read( pData^, 4096 );
- VarArrayUnlock( vaSource );
-
- if lRead > 0 then
- begin
- { Watch out for the trap: You may have read less than 4096, but the
- variant is still reporting to hold 4096 bytes. Make sure to reduce
- the size if applicable (why not always!) }
- VarArrayRedim( vaSource, lRead-1 );
-
- { Compress this block. The method may return less than the
- compressed buffer, as it keeps some data in bank in order to
- compress even better (rewind and recompress) }
-
- xErr := xComp.Compress( vaSource, vaDest, false );
- end
- else
- begin
- { Just call compress with an empty source, to tell the compression
- engine to flush remaining compressed data }
- VarClear( vaSource );
- xErr := xComp.Compress( vaSource, vaDest, true );
- end;
-
- if xErr = xceSuccess then
- begin
- lToWrite := VarArrayHighBound( vaDest, 1 )
- - VarArrayLowBound( vaDest, 1 ) + 1;
-
- pData := VarArrayLock( vaDest );
- lWritten := fsWrite.Write( pData^, lToWrite );
- VarArrayUnlock( vaDest );
-
- if lWritten <> lToWrite then
- begin
- ShowMessage( 'Could not write all the data to the destination file.' );
- lRead := 0;
- end;
- end
- until ( lRead = 0 ) or ( xErr <> xceSuccess );
-
- ShowMessage( xComp.GetErrorDescription( xErr ) );
- end;
-
- fsRead.Free;
- fsWrite.Free;
- except
- ShowMessage( 'Unhandled exception' );
- end;
- end;
-
- procedure TfrmFileStream.btUncompressClick(Sender: TObject);
- var
- fsRead : TFileStream;
- fsWrite : TFileStream;
- lRead : LongInt;
- lToWrite : LongInt;
- lWritten : LongInt;
- pData : Pointer;
- vaSource : OleVariant;
- vaDest : OleVariant;
- xErr : xcdCompressionError;
- begin
- { As you can see, uncompressing is quite similar as compressing! }
- try
- fsRead := TFileStream.Create( edtUncompSrc.Text, fmOpenRead );
- fsWrite := TFileStream.Create( edtUncompDest.Text, fmCreate );
-
- if Assigned( fsRead ) and Assigned( fsWrite )then
- begin
- vaSource := VarArrayCreate( [0, 4095], varByte );
-
- repeat
- { Read some bytes from source file }
- pData := VarArrayLock( vaSource );
- lRead := fsRead.Read( pData^, 4096 );
- VarArrayUnlock( vaSource );
-
- if lRead > 0 then
- begin
- { Again, wtahc out for the Variant size trap! }
- VarArrayRedim( vaSource, lRead-1 );
-
- { Uncompress this block. The method may return less than the
- uncompressed buffer, as it requires some other bytes ahead to
- unscramble these. }
- xErr := xComp.Uncompress( vaSource, vaDest, false );
- end
- else
- begin
- { Just call compress with an empty source, to tell the compression
- engine that it holds the last bytes }
- VarClear( vaSource );
- xErr := xComp.Uncompress( vaSource, vaDest, true );
- end;
-
- if xErr = xceSuccess then
- begin
- lToWrite := VarArrayHighBound( vaDest, 1 )
- - VarArrayLowBound( vaDest, 1 ) + 1;
-
- pData := VarArrayLock( vaDest );
- lWritten := fsWrite.Write( pData^, lToWrite );
- VarArrayUnlock( vaDest );
-
- if lWritten <> lToWrite then
- begin
- ShowMessage( 'Could not write all the data to the destination file.' );
- lRead := 0;
- end;
- end
- until ( lRead = 0 ) or ( xErr <> xceSuccess );
-
- ShowMessage( xComp.GetErrorDescription( xErr ) );
- end;
-
- fsRead.Free;
- fsWrite.Free;
- except
- ShowMessage( 'Unhandled exception' );
- end;
- end;
-
- end.
-