首页 > 学院 > 开发设计 > 正文

流的压缩和解压

2019-11-18 18:24:33
字体:
来源:转载
供稿:网友
(*//标题:流的压缩和解压说明:适用文件压缩、图象压缩等;调用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 }
//Exampleprocedure 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;

上一篇:剪贴板的流存储

下一篇:搜索字符串在流中的位置

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
学习交流
热门图片

新闻热点

疑难解答

图片精选

网友关注