我想在EDIT1中输入字符后,点第一按钮压缩,压缩后的内容存入EDIT2中;点第二按钮解压,并把解压后的内容写入MEMO1中,即最后edit1中的内容跟memo1中的内容应该是一样的,但我在点第二按钮时DELPHI7提示出错,但却无法定位到出错的指令。请各位老大帮我看看这程序该怎么改正。unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Zlib;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var ins,ous:tmemorystream;
ss:tstringstream;
cs:tcompressionstream;
begin
ins:=tmemorystream.Create ;
ous:=tmemorystream.Create ;
ss:=tstringstream.Create('');
ins.WriteBuffer(pchar(edit1.Text)^,length(edit1.Text));
cs:=tcompressionstream.Create(clmax,ous);
cs.CopyFrom(ins,0);
cs.Free;
ous.Position :=0;
ss.CopyFrom(ous,0);
edit2.Text :=ss.DataString ;
ins.Free ;
ous.Free;
ss.Free;
end;procedure TForm1.Button2Click(Sender: TObject);
var ins,ous:tmemorystream;
ss:tstringstream;
cs:tdecompressionstream;
begin
ins:=tmemorystream.Create ;
ous:=tmemorystream.Create ;
ss:=tstringstream.Create('');
ins.WriteBuffer(pchar(edit2.Text)^,length(edit2.Text));
ins.Position :=0;
cs:=tdecompressionstream.Create(ins);
ous.CopyFrom(cs,cs.Size);
cs.Free;
ous.Position :=0;
ss.CopyFrom(ous,0);
memo1.Lines.Add(ss.DataString );
ins.Free ;
ous.Free;
ss.Free;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Zlib;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var ins,ous:tmemorystream;
ss:tstringstream;
cs:tcompressionstream;
begin
ins:=tmemorystream.Create ;
ous:=tmemorystream.Create ;
ss:=tstringstream.Create('');
ins.WriteBuffer(pchar(edit1.Text)^,length(edit1.Text));
cs:=tcompressionstream.Create(clmax,ous);
cs.CopyFrom(ins,0);
cs.Free;
ous.Position :=0;
ss.CopyFrom(ous,0);
edit2.Text :=ss.DataString ;
ins.Free ;
ous.Free;
ss.Free;
end;procedure TForm1.Button2Click(Sender: TObject);
var ins,ous:tmemorystream;
ss:tstringstream;
cs:tdecompressionstream;
begin
ins:=tmemorystream.Create ;
ous:=tmemorystream.Create ;
ss:=tstringstream.Create('');
ins.WriteBuffer(pchar(edit2.Text)^,length(edit2.Text));
ins.Position :=0;
cs:=tdecompressionstream.Create(ins);
ous.CopyFrom(cs,cs.Size);
cs.Free;
ous.Position :=0;
ss.CopyFrom(ous,0);
memo1.Lines.Add(ss.DataString );
ins.Free ;
ous.Free;
ss.Free;
end;
end.
procedure TForm1.Button2Click(Sender: TObject);
var ins,ous:tmemorystream;
ss:tstringstream;
cs:tdecompressionstream; buf: array[1..1024]of char;
i: integer;
begin
ins:=tmemorystream.Create ;
ous:=tmemorystream.Create ;
ss:=tstringstream.Create('');
ins.WriteBuffer(pchar(edit2.Text)^,length(edit2.Text));
ins.Position :=0;
cs:=tdecompressionstream.Create(ins);
{ ous.CopyFrom(cs,cs.Size);}
i:=cs.Read(buf, 1024);
while i>0 do begin
ous.Write(buf, i);
i:=cs.Read(buf, 1024);
end; cs.Free;
ous.Position :=0;
ss.CopyFrom(ous,0);
memo1.Lines.Add(ss.DataString );
ins.Free ;
ous.Free;
ss.Free;
end;
{14.OletoStream(将OLE变量转化为数据流) }
{15.ZipOle(压缩OLE变量) }
{16.UnZipOle(将OLE变量解压缩)
var
ole:olevariant;
p:pointer;
size:integer;
begin
sourcestream.Position:=0;
size:=sourcestream.Size;
ole:=VarArrayCreate([0,Size-1],varByte);
p:=vararraylock(ole);
try
sourcestream.ReadBuffer(p^,size);
finally
vararrayunlock(ole);
end;
result:=ole;
end;function zipole(const sourceole:olevariant):olevariant; //压缩OLE变量
var
p,zipp:pointer;
zipole:olevariant;
size:integer;
CompressionStream:TCompressionStream;
SourceStream,DestStream:TMemoryStream;
begin
sourcestream:=tmemorystream.Create;//创建内存流,保存压缩后的数据
DestStream:=tmemorystream.Create;//创建临时的数据流
try
size:= VarArrayHighBound(sourceole,1)-VarArrayLowBound(sourceole,1)+1;//取得ole的大小
P:=VarArrayLock(sourceole);//转化为指针类型
sourcestream.WriteBuffer(p^,size);//将原始数据写入sourcestream中
sourcestream.Seek(0,sobeginning);//复位指针 CompressionStream:=tcompressionstream.Create(cldefault,DestStream);//创建压缩数据流
sourcestream.SaveToStream(CompressionStream); //将传过来的OLE写入压缩流中
CompressionStream.Free;
sourcestream.Clear;
sourcestream.WriteBuffer(size,sizeof(size));//写入原始数据的大小
sourcestream.CopyFrom(DestStream,0);//写入经过压缩的数据流
try
sourcestream.Seek(0,soFromBeginning);//复位指针
zipole:=VarArrayCreate([0,sourcestream.Size-1],varByte);
zipp:=vararraylock(zipole);
sourcestream.Readbuffer(zipp^,sourcestream.Size);//将压缩的数据流写入OLE
result:=zipole;
finally
VarArrayUnlock(sourceole);
vararrayunlock(zipole);
end;
finally
SourceStream.Free;
deststream.Free;
end;
end;function unzipole(const zipstream:tmemorystream):olevariant; //还原压缩流
var
size:integer;
p:pointer;
unzipole:olevariant;
DecompressionStream:TDecompressionStream;
begin
zipstream.seek(0,sobeginning);//复位指针
zipstream.readbuffer(size,sizeof(size));//读取原始数据的大小
DecompressionStream:=tDecompressionStream.Create(zipstream);
try
unzipole:=VarArrayCreate([0,Size-1],varByte);
p:=vararraylock(unzipole);
decompressionstream.Readbuffer(P^,size);
finally
vararrayunlock(unzipole);
end;
end;procedure OletoStream(const zipole:olevariant;var zipstream:tmemorystream);//将OLE变量转化为数据流
var
p:pointer;
size:integer;
begin
size:=VarArrayHighBound(zipole,1)-VarArrayLowBound(zipole,1)+1;//取得ole的大小
p:=vararraylock(zipole);
try
zipstream.WriteBuffer(p^,size);//将OLE的内容写入数据流
finally
vararrayunlock(zipole);
end;
end;