用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧Delphi

流的压缩和解压

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 20:05:07
(*//
标题:流的压缩和解压
说明:适用文件压缩、图象压缩等;调用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;

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

相关文章

PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:5,421.87500 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号