?? unit1.pas
字號:
// *******************************************************
// There are no restrictions placed on the use of the code
// contained in this project. These project files include
// Project1.dpr, unit1.dfm, and unit1.pas.
// *******************************************************
// This project is a visual demonstration of how to compress data
// to a memory stream.
Unit unit1;
Interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, ComCtrls, ztvStreams;
Type
TForm1 = Class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
Procedure Button1Click(Sender: TObject);
Procedure Button2Click(Sender: TObject);
Procedure CompressToMemory(StrmToCompress: TStream32);
Private
{ Private declarations }
Public
{ Public declarations }
End;
Var
Form1: TForm1;
Implementation
{$R *.DFM}
Uses
ztvBase, ztvInflate, ztvDeflate;
//-------------------------------------------------------------
// for retrieving data from a memo control
Procedure StrToBuf( Buf: Pointer; Str: String );
Begin
Move( Str[1], Buf^, Length( Str ) );
End;
//-------------------------------------------------------------
// for placing into a memo control
Function BufToStr( Buf: Pointer; Size: Integer ): String;
Begin
SetLength( Result, Size );
Move( Buf^, Result[1], Size );
End;
//-------------------------------------------------------------
// flush stream data to the forms memo controls
Procedure StreamToMemo(Strm: TStream32; Memo: TMemo);
Var
Buf: Pointer;
Begin
// place the compressed memory stream in memo2.text
GetMem(Buf, Strm.Size);
Try
Strm.Read( Buf^, Strm.Size );
Finally
Memo.Text := BufToStr( Buf, Strm.Size );
FreeMem(Buf);
End;
End;
//-------------------------------------------------------------
Procedure TForm1.CompressToMemory(StrmToCompress: TStream32);
Var
MemStrm: TMemoryStream32;
Begin
// memory stream to hold compressed data
MemStrm := TMemoryStream32.Create();
Try
// ztvCompress_StreamToStream in ztvDeflate.pas
// It returns the crc value of the compressed stream (MemStrm)
ztvCompress_StreamToStream( StrmToCompress, MemStrm );
// placed the compressed data into memo and release stream
StreamToMemo(MemStrm, Memo2);
Finally
MemStrm.Free();
End;
End;
//-------------------------------------------------------------
Procedure TForm1.Button1Click(Sender: TObject);
Var
CompressStream: TStream32;
Begin
CompressStream := TMemoryStream32.Create();
Try
// place uncompressed data into the stream for compression
CompressStream.Write(Memo1.Text[1], Length(Memo1.Text));
CompressToMemory(CompressStream);
Finally
CompressStream.Free();
End;
// clear the uncompressed data memo control
Memo1.Text := '';
End;
//-------------------------------------------------------------
Procedure TForm1.Button2Click(Sender: TObject);
Var
buf: Pointer;
BufSize: Integer;
CompressedStrm,
DecompressedStrm: TStream32;
Begin
CompressedStrm := TMemoryStream32.Create();
Try
BufSize := Length( Memo2.Text );
// place the compress data from memo into stream from decompresssion
GetMem( Buf, BufSize + 1 );
Try
StrToBuf( Buf, Memo2.Text );
CompressedStrm.Write(Buf^, BufSize);
Finally
CompressedStrm.Position := 0;
Freemem(Buf, BufSize + 1);
End;
DecompressedStrm := TMemoryStream32.Create();
Try
// ztvDecompress_StreamToStream in ztvInflate.pas
// StreamToStream returns crc value of decompressed stream
ztvDecompress_StreamToStream( CompressedStrm, DecompressedStrm, CompressedStrm.Size );
Finally
StreamToMemo(DecompressedStrm, Memo1);
DecompressedStrm.Free();
// clear the compressed data memo control
Memo2.Text := '';
End;
Finally
CompressedStrm.Free();
End;
End;
//-------------------------------------------------------------
End.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -