对Zlib单元进行再封装

低版本DELPHI,如D7,ZLIB.pas单元封装的很简陋,因此有必要再封装,以增加使用的便利性。

高版本DELPHI,zlib.pas本身提供的接口已经相当完善。

Zlib.pas是DELPHI自带的压缩单元,下面对对Zlib单元进行再封装,增加两个压缩函数,一个压缩流,一个压缩字符串:

分别在D7和XE10.3.1下面,测试通过。

unit Unit1;

interface

uses
ZLib, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms, Dialogs; type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1;
// 两个过程都有Compress参数,这个参数用来决定进行压缩操作还是解压操作: True--压缩; false--解压.
procedure Zip(Input, Output: TStream; Compress: Boolean); overload; function Zip(Input: string; Compress: Boolean): string; overload; implementation {$R *.dfm} procedure Zip(Input, Output: TStream; Compress: Boolean);
const
MAXBUFSIZE = 1024 * 16; //16 KB
var
CS: TCompressionStream;
DS: TDecompressionStream;
Buf: array[0..MAXBUFSIZE - 1] of Byte;
BufSize: Integer;
begin
if Assigned(Input) and Assigned(Output) then
begin
if Compress then // 压缩
begin
CS := TCompressionStream.Create(ZLib.clDefault, Output);
try
CS.CopyFrom(Input, 0); //从开始处复制
finally
CS.Free;
end;
end
else
begin // 解压
DS := TDecompressionStream.Create(Input);
try
BufSize := DS.Read(Buf, MAXBUFSIZE);
while BufSize > 0 do
begin
Output.Write(Buf, BufSize);
BufSize := DS.Read(Buf, MAXBUFSIZE);
end;
finally
DS.Free;
end;
end;
end;
end; function Zip(Input: string; Compress: Boolean): string;
var
InputStream, OutputStream: TStringStream;
begin
if Input = '' then
Exit;
InputStream := TStringStream.Create(Input);
try
OutputStream := TStringStream.Create('');
try
Zip(InputStream, OutputStream, Compress);
Result := OutputStream.DataString;
finally
OutputStream.Free;
end;
finally
InputStream.Free;
end;
end; end.

  

最新文章

  1. angular2系列教程(三)components
  2. 第三章 EnumUtil根据值获取枚举对象
  3. maven总结5
  4. dede使用方法----如何转换时间戳
  5. u3d动态加入模型
  6. swap分区添加
  7. 阅读代码分析工具Understand 2.0试用
  8. 02-windows 安装以太坊 ethereum 客户端 (win7-64)-大叔思维
  9. 各模拟器adb连接端口
  10. 引用第三方dll引发的问题解决
  11. 高德Location
  12. eclipse下将maven项目打包为jar(1.不带第三方jar,2.带第三方jar)
  13. Richard Sabey于2004年给出了由123456789各出现一次的e的估计
  14. 编译libcurl支持https协议
  15. JS的小判断
  16. Mysqldumpslow的用法汇总
  17. wampserver 403forbidden问题
  18. learning docker steps(4) ----- docker swarm 初次体验
  19. 用最简单的例子理解策略模式(Strategy Pattern)
  20. getXXXPos()约定

热门文章

  1. C#-MailHelper
  2. centos7创建共享文件夹
  3. c# 定制处理未处理异常
  4. ORA-03113:通信通道的文件结尾 解决办法
  5. web上常见的攻击方式及简单的防御方法
  6. python练习题(一)
  7. mysqlcheck(MyISAM表维护工具)
  8. Gitlab,Git设置及使用前的准备
  9. 多任务案例--文件夹copy.py
  10. django-haystack 安装No local packages or working download links found for setuptools_scm