ZLib的使用(100分)

  • 主题发起人 主题发起人 hq_pan
  • 开始时间 开始时间
H

hq_pan

Unregistered / Unconfirmed
GUEST, unregistred user!
在介绍如何使用Delphi自带的Zlib.pas的时候,大家给出的例子几乎都是一样的,
解压的时候无一例外的源流肯定是TFileStream!!!!!!!!!!!!!!!
如:
procedure UnCompressData(const CompressedStream: TFileStream;
var SDestStream: TMemoryStream);
var
SourceStream: TDecompressionStream;
DestStream: TMemoryStream;
Buffer: PChar;
Count: Integer;
begin
//从被压缩的流中读出原始的尺寸
CompressedStream.ReadBuffer(Count, SizeOf(Count));
//根据尺寸大小为将要读入的原始流分配内存块
GetMem(Buffer, Count);
DestStream := TMemoryStream.Create;
SourceStream := TDecompressionStream.Create(CompressedStream);
Try
//将被压缩的流解压缩,然后存入 Buffer内存块中
SourceStream.ReadBuffer(Buffer^, Count);
//将原始流保存至 DestStream流中
DestStream.WriteBuffer(Buffer^, Count);
DestStream.Position := 0;//复位流指针
//从 DestStream流中载入流
sDestStream.LoadFromStream(DestStream);
finally
FreeMem(Buffer);
DestStream.Free;
end;
end;
那位高手能给出一个都是使用TMemoryStream的解压过程出来??
如:procedure UnCompressData(const SourceMS: TMemoryStream;
var DestMS: TMemoryStream);
此外,能否给出一步解压一个文件的例子??
如:procedure UnCompressFile(const FileName: string);
很急啊!!!!!!!!!!!
 
没人回答???
sigh...
 
既然没人回答,板主请帮忙删了,把分还给我 [:D]
 
delphi自身所带的zlib单元,使用时,需要Zlib.pas和 Zlibconst.pas两个单元文件,在Delphi 5.0光盘上 /Info/Extras/Zlib,此外,在Info/Extras/Zlib/Obj目录中还保存了 Zlib.pas单元引用的 Obj文件,把这个目录拷贝到delphi的lib下
原来的例子是抓计算机屏幕,现在改成文件流,适应所有情况。也可以适当的改动比如增加目录压缩和分文件压缩,其实就是在文件流前面增加一部分描述结构就是。使用时,还要use zlib单元。
两个函数如下
procedure CompressIt(var CompressedStream: TMemoryStream;
const CompressionLevel: TCompressionLevel);
// 参数是传递的流和压缩方式
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: int64;
//注意,此处修改了,原来是int
begin

Count := CompressedStream.Size;//获得流的原始尺寸
DestStream := TMemoryStream.Create;
SourceStream := TCompressionStream.Create(CompressionLevel, DestStream);
try
CompressedStream.SaveToStream(SourceStream);//SourceStream中保存着原始的流
SourceStream.Free;//将原始流进行压缩,DestStream中保存着压缩后的流
CompressedStream.Clear;
CompressedStream.WriteBuffer(Count, SizeOf(Count));//写入原始图像的尺寸
CompressedStream.CopyFrom(DestStream, 0);//写入经过压缩的流
finally
DestStream.Free;
end;

end;

procedure UnCompressit(const CompressedStream: TMemoryStream;
var UnCompressedStream: TMemoryStream);
//参数压缩过的流,解压后的流
var
SourceStream: TDecompressionStream;
DestStream: TMemoryStream;
Buffer: PChar;
Count: int64;
begin

CompressedStream.ReadBuffer(Count, SizeOf(Count));//从被压缩的图像流中读出原始的尺寸
GetMem(Buffer, Count);//根据尺寸大小为将要读入的原始流分配内存块
DestStream := TMemoryStream.Create;
SourceStream := TDecompressionStream.Create(CompressedStream);
try
SourceStream.ReadBuffer(Buffer^, Count);//将被压缩的流解压缩,然后存入 Buffer内存块中
DestStream.WriteBuffer(Buffer^, Count);//将原始流保存至 DestStream流中
DestStream.Position := 0;
//复位流指针
DestStream.Position := length(VER_INFO);
UnCompressedStream.LoadFromStream(DestStream);//从 DestStream流中载入图像流
finally
FreeMem(Buffer);
DestStream.Free;
end;

end;

使用的例子如下:
procedure TForm1.Button5Click(Sender: TObject);
//把指定文件压缩然后保存为另外一个压缩包
var
SM: TMemoryStream;
begin

if OpenDialog1.Execute then
begin
if SaveDialog1.Execute then
begin
SM := TMemoryStream.Create;
try
Sm.LoadFromFile(OpenDialog1.FileName);
SM.Position := 0;
Compressit(sm, clDefault);
sm.SaveToFile(SaveDialog1.FileName);
finally
SM.Free;
end;
end;
end;

end;

procedure TForm1.BitBtn2Click(Sender: TObject);
//把指定的压缩包解成原来的文件。
var
SM, DM: TMemoryStream;
begin
if OpenDialog1.Execute then
begin
if SaveDialog1.Execute then
begin
SM := TMemoryStream.Create;
DM := TMemoryStream.Create;
try
Sm.LoadFromFile(OpenDialog1.FileName);
SM.Position := 0;
UnCompressit(sm, dm);
dm.Position := 0;
dm.SaveToFile(SaveDialog1.FileName);
finally
SM.Free;
DM.Free;
end;
end;
end;

end;
 
先给分,因为现在没时间测试,如果不行或有其它什么问题,再向您请教!
 
注:
测试时中间有一行有错,把它注掉就行了
 

Similar threads

I
回复
0
查看
496
import
I
I
回复
0
查看
641
import
I
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部