(* //
Title: Compression and decompression of flow
Description: Applicable file compression, image compression, etc.
Design: zswang
Support: wjhu111@21cn.com
Date: 2004-03-24
// *)
(* / / ================================================================================================================================================================================ ===============================
Design ideas:
Create a TcompressionStream, TDecompressionStream instance compression and decompression
============================================================================================================================================================================================================= ========================== // *)
Uses zlib;
Const CBuffersize = $ 4096;
Function streamcompression (MINPUTSTREAM: TSTREAM; MOUTPUTSTREAM: TSTREAM): Integer;
VAR
I: integer;
VBuffer: array [0..cbuffersize] of char;
Begin
RESULT: = -1;
IF not (Assigned (MINPUTSTREAM) and assigned (moutputstream).
With TcompressionStream.create (CLMAX, MoutputStream) Do Try
For i: = 1 to minputstream.size div cbuffersize do begin
MinputStream.read (vbuffer, cbuffersize);
Write (vbuffer, cbuffersize);
END;
I: = minputstream.size mod cbuffers;
IF i> 0 THEN Begin
MinputStream.read (VBuffer, i);
Write (vbuffer, i);
END;
Finally
FREE;
END;
End; {streamcompression}
Function streamDecompression (MINPUTSTREAM: TSTREAM; MoutputStream: TSTream): Integer;
VAR
VBuffer: array [0..cbuffersize] of char;
I: integer;
Begin
RESULT: = -1;
IF not (Assigned (MINPUTSTREAM) and assigned (moutputstream).
With TDecompressionStream.create (MINPUTSTREAM) DO TRY
Repeat
I: = read (vbuffer, cbuffersize);
MoutputStream.write (vbuffer, i);
Until i = 0;
Result: = moutputStream.size;
Finally
FREE;
END;
End; {streamDecompression}
// esample
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
VAR
VinputStream: tfilestream;
VOUTPUTSTREAM: TFILESTREAM;
Begin
VinputStream: = TFileStream.create ('c: /temp/temp.exe', fmopenread);
VOUTPUTSTREAM: = TFileStream.create ('c: /temp/temp.exe.z', fmcreate);
Try
StreamCompRession (VinputStream, VoutputStream);
Finally
VinputStream.free;
VOUTPUTSTREAM.FREE;
END;
END;
Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);
VAR
VinputStream: tfilestream;
VOUTPUTSTREAM: TFILESTREAM;
Begin
VinputStream: = TFileStream.create ('c: /temp/temp.exe.z', fmopenread);
VOUTPUTSTREAM: = TFileStream.create ('c: /temp/temp ~.exe', fmcreate);
Try
StreamDecompression (VinputStream, VoutputStream);
Finally
VinputStream.free;
VOUTPUTSTREAM.FREE;
END;
END;