您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

ZLib解压流和压缩流

时间:2011/9/3 15:38:22 点击:

  核心提示:procedure UnCompressStream(var MyStream: TMemoryStream);var SourceStream: TDecompressionStream; Buff...
procedure UnCompressStream(var MyStream: TMemoryStream);
var
  SourceStream: TDecompressionStream;
  Buffer: PChar;
  Count: Integer;
begin
  MyStream.Position := 0;
  //还原被压缩图像
  //从被压缩的图像流中读出原始图像的尺寸
  MyStream.ReadBuffer(Count, SizeOf(Count));
  //根据图像尺寸大小为将要读入的原始图像流分配内存块
  GetMem(Buffer, Count);
  SourceStream := TDecompressionStream.Create(MyStream);
  try
    //将被压缩的图像流解压缩,然后存入 Buffer内存块中
    SourceStream.ReadBuffer(Buffer^, Count);
    //将原始图像流保存至 DestStream流中
    MyStream.Clear;
    MyStream.WriteBuffer(Buffer^, Count);
    //复位流指针
    MyStream.Position := 0;
  finally
    FreeMem(Buffer);
  end;
end;   procedure CompressStream(var MyStream: TMemoryStream);
var
  SourceStream: TCompressionStream;
  DestStream: TMemoryStream;
  Count: Integer;
begin
  //压缩图像
  //获得图像流的原始尺寸
  Count := MyStream.Size;
  DestStream := TMemoryStream.Create;
  SourceStream := TCompressionStream.Create(clDefault, DestStream);
  try
    //SourceStream中保存着原始的图像流
    MyStream.SaveToStream(SourceStream);
    //将原始图像流进行压缩, DestStream中保存着压缩后的图像流
    SourceStream.Free;
    MYStream.Clear;
    //写入原始图像的尺寸
    MyStream.WriteBuffer(Count, SizeOf(Count));
    //写入经过压缩的图像流
    MyStream.CopyFrom(DestStream, 0);
    //复位流指针
    MyStream.Position := 0;
  finally
    DestStream.Free;
  end;
end;

Tags:解压 压缩 
作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2022 版权所有 All Rights Reserved.
  • 沪ICP备05001939号