RSS订阅 | 匿名投稿
您的位置:网站首页 > 技巧 > 正文

Delphi使用ZLib压缩和解压文件

作者:admin 来源: 日期:2016/1/26 21:58:56 人气: 标签:

Delphi的单元文件ZLib.pas提供了使用ZLib数据压缩算法的接口,只要在我们的Delphi工程里引用该单元即可使用ZLib数据压缩功能,以下是使用ZLib对数据流进行压缩和解压的函数:

uses 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
        //SourceStream中保存着原始的流
        CompressedStream.SaveToStream(SourceStream);
        //将原始流进行压缩, DestStream中保存着压缩后的流
        SourceStream.Free;
        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
        //将被压缩的流解压缩,然后存入 Buffer内存块中 
        SourceStream.ReadBuffer(Buffer^, Count);
        //将原始流保存至 DestStream流中 
        DestStream.WriteBuffer(Buffer^, Count);
        DestStream.Position := 0; //复位流指针
        //从DestStream流中载入数据流
        UnCompressedStream.LoadFromStream(DestStream); 
    finally
        FreeMem(Buffer);
        DestStream.Free;
    end;
end;

//对文件进行压缩

procedure TForm1.Button1Click(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.Button2Click(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;

以上代码在Delphi7编译环境下调试通过。


读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址:
下一篇:没有资料