流的应用~~~~(100分)

  • 主题发起人 主题发起人 Mclkings
  • 开始时间 开始时间
M

Mclkings

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么使用流进行数据压缩~~~
下面是我写的一个压缩过程,一个解压过程,
压缩可以实现,解压不可以实现~~不知道怎么解决~~~
procedure TFrmMain.btn1Click(Sender: TObject);
var
Count: Integer;
Com: TCompressionStream;
MeM,Source: TMemoryStream;
ComLevel: TCompressionLevel;
begin
ComLevel := clFastest;
MeM := TMemoryStream.Create;
Source := TMemoryStream.Create;
Count := Source.Size;
mmo1.Lines.SaveToStream(Source);

Com := TCompressionStream.Create(ComLevel, MeM);
Source.SaveToStream(Com);
Com.Free;
Source.Clear;
Source.Write(Count, SizeOf(Count));
Source.CopyFrom(MeM, 0);
Source.Position := 0;
Source.SaveToFile('b.txt');
end;

procedure TFrmMain.btn2Click(Sender: TObject);
var
Source: TDecompressionStream;
Mem: TMemoryStream;
Count: Integer;
Buf: PChar;
FileM: TFileStream;
sName : string;
begin
sName := ExtractFilePath(ParamStr(0))+'b.txt';
Mem := TMemoryStream.Create;
FileM := TFileStream.Create(sName, fmOpenRead);
FileM.ReadBuffer(Count , SizeOf(Count));
GetMem(Buf, Count);
Source := TDecompressionStream.Create(FileM);
Source.ReadBuffer(Buf^, Count);
Mem.WriteBuffer(Buf^, Count);

Mem.Position := 0;
Mem.SaveToFile('bb.txt');
FreeMem(Buf);
end;

谢谢~~~~
 
procedure TForm1.btnCompressClick(Sender: TObject);//压缩
var
aStreamSource, aStreamDest: TFileStream;
begin
aStreamSource := TFileStream.Create (edOrigin.Text, fmOpenRead);
aStreamDest := TFileStream.Create (edCompressed.Text, fmCreate);
try
CompressStream (aStreamSource, aStreamDest);
aStreamDest.Position := 0;
Memo1.Lines.LoadFromStream (aStreamDest);
finally
aStreamSource.Free;
aStreamDest.Free;
end;
end;

procedure TForm1.btnDecompressClick(Sender: TObject);//解压
var
aStreamSource: TFileStream;
aStreamDest: TMemoryStream;
begin
aStreamSource := TFileStream.Create (edCompressed.Text, fmOpenRead);
aStreamDest := TMemoryStream.Create;
try
DecompressStream(aStreamSource, aStreamDest);
aStreamDest.Position := 0;
Memo1.Lines.LoadFromStream(aStreamDest);
finally
aStreamSource.Free;
aStreamDest.Free;
end;
end;
 
CompressStream
DecompressStream

这两个方法你并没有贴出来啊~~~

兄弟
 
function CompressStream (aSource, aTarget: TStream): Single;//压缩方法
var
comprStream: TCompressionStream;
begin
comprStream := TCompressionStream.Create(
clFastest, aTarget);
try
comprStream.CopyFrom(aSource, aSource.Size);
Result := comprStream.CompressionRate;
finally
comprStream.Free;
end;
end;

// 解压方法
procedure DecompressStream (aSource, aTarget: TStream) ;
var
decompStream: TDecompressionStream;
nRead: Integer;
Buffer: array [0..1023] of Char;
begin
decompStream := TDecompressionStream.Create(aSource);
try
repeat
nRead := decompStream.Read(Buffer, 1024);
aTarget.Write (Buffer, nRead);
until nRead = 0;
finally
decompStream.Free;
end;
end;
 
多人接受答案了。
 
后退
顶部