流的压缩和解压_Delphi教程

2006-02-04     评论:0条 进入论坛
  •   

(*//
标题:流的压缩和解压
说明:适用文件压缩、图象压缩等;调用ZLib单元的方法实现
设计:Zswang
支持:wjhu111@21cn.com
日期:2004-03-24
//*)
(*//============================================================================
设计思路:
创建TCompressionStream、TDecompressionStream实例进行压缩和解压
============================================================================//*)
uses ZLib;
const cBufferSize = $4096;
function StreamCompression(mInputStream: TStream; mOutputStream: TStream): Integer;
var
  I: Integer;
  vBuffer: array[0..cBufferSize]of Char;
begin
  Result := -1;
  if not (Assigned(mInputStream) and Assigned(mOutputStream)) then Exit;
  with TCompressionStream.Create(clMax, mOutputStream) do try
    for I := 1 to mInputStream.Size div cBufferSize do begin
      mInputStream.Read(vBuffer, cBufferSize);
      Write(vBuffer, cBufferSize);
    end;
    I := mInputStream.Size mod cBufferSize;
    if I > 0 then begin
      mInputStream.Read(vBuffer, I);
      Write(vBuffer, I);
    end;
  finally
    Free;
  end;
end; { StreamCompression }
function StreamDecompression(mInputStream: TStream; mOutputStream: TStream): Integer;
var
  vBuffer: array[0..cBufferSize]of Char;
  I: Integer;
begin
  Result := -1;
  if not (Assigned(mInputStream) and Assigned(mOutputStream)) then Exit;
  with TDecompressionStream.Create(mInputStream) do try
    repeat
      I := Read(vBuffer, cBufferSize);
      mOutputStream.Write(vBuffer, I);
    until I = 0;
    Result := mOutputStream.Size;
  finally
    Free;
  end;
end; { StreamDecompression }
//Example
procedure TForm1.Button1Click(Sender: TObject);
var
  vInputStream: TFileStream;
  vOutputStream: TFileStream;
begin
  vInputStream := TFileStream.Create('c:\temp\temp.exe', fmOpenRead);
  vOutputStream := TFileStream.Create('c:\temp\temp.exe.z', fmCreate);
  try
    StreamCompression(vInputStream, vOutputStream);
  finally
    vInputStream.Free;
    vOutputStream.Free;
  end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
  vInputStream: TFileStream;
  vOutputStream: TFileStream;
begin
  vInputStream := TFileStream.Create('c:\temp\temp.exe.z', fmOpenRead);
  vOutputStream := TFileStream.Create('c:\temp\temp~.exe', fmCreate);
  try
    StreamDecompression(vInputStream, vOutputStream);
  finally
    vInputStream.Free;
    vOutputStream.Free;
  end;
end;

【声明】 http://www.Stuceo.com 版权与免责声明:本站转载其他媒体稿件是为传播更多的信息,此 类稿件不代表本站观点,本站不承担此类稿件侵权行为的连带责任。如您因版权等问题需要与本站联络,请联系 stuceo@163.com

讨论区
查看
已有 0 位对 此新闻感兴趣的网友发表了看法
今日推荐
 
news



热点推荐



中国学习基地
关于站点 - 意见反馈 - 广告服务 - 合作伙伴 - 程序支持 - 客户服务 - 联系我们

CopyRight 2006-2008, Stuceo.Com by 中国学习基地 Inc. All Rights Reserved .

鲁ICP备05047442号