📄 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,
ztvConsts,
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 memo control
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
CRC: u_long;
Level: TDeflateType;
MemStrm: TMemoryStream32;
Begin
// memory stream to hold compressed data
MemStrm := TMemoryStream32.Create();
Try
// =======================================================
// Possible values for level:
// dtDeflateS: Stored (no compression - fastest speed)
// dtDeflateF: Fast (less compression, increased speed)
// dtDeflateN: Normal (medium compression - medium speed)
// dtDeflateX: eXtra (most compression - less speed)
// =======================================================
Level := dtDeflateN;
StrmToCompress.Seek(0, soBeginning);
// ztvCompress_StreamToStream in ztvDeflate.pas
// It returns the crc value of the compressed stream (MemStrm)
CRC := ztvCompress_StreamToStream(
StrmToCompress,
TStream32(MemStrm),
StrmToCompress.Size,
Level);
// returned calculated Crc
If CRC <> 0 Then
;
// placed the compressed data into memo and release stream
MemStrm.Seek(0, soFromBeginning);
StreamToMemo(MemStrm, Memo2);
Finally
MemStrm.Free();
End;
End;
//-------------------------------------------------------------
// compress
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;
//-------------------------------------------------------------
// decompress
Procedure TForm1.Button2Click(Sender: TObject);
Var
CRC: u_long;
Buf: Pointer;
BufSize: Integer;
CompressedStrm,
DecompressedStrm: TStream32;
Begin
// create the stream to be decompressed
CompressedStrm := TMemoryStream32.Create();
Try
// =================================================
// place the compress data from the memo into stream
// for decompresssion
// =================================================
BufSize := Length(Memo2.Text);
GetMem(Buf, BufSize + 1);
Try
StrToBuf(Buf, Memo2.Text);
CompressedStrm.Write(Buf^, BufSize);
Finally
CompressedStrm.Position := 0;
FreeMem(Buf, BufSize + 1);
End;
// =================================================
// =================================================
// decompress the CompressedStrm
// =================================================
DecompressedStrm := TMemoryStream32.Create();
Try
// ztvDecompress_StreamToStream in ztvInflate.pas
CRC :=
ztvDecompress_StreamToStream(
CompressedStrm,
DecompressedStrm,
CompressedStrm.Size);
// returned calculated Crc
If CRC <> 0 Then
;
Finally
// add the DecompressedStrm to the memo control
StreamToMemo(DecompressedStrm, Memo1);
DecompressedStrm.Free();
End;
// =================================================
Finally
CompressedStrm.Free();
// clear the compressed data memo control
Memo2.Text := '';
End;
End;
//-------------------------------------------------------------
End.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -