论坛帖子:http://community.csdn.net/Expert/topic/3288/3288152.xml?temp=.2455866
Delphi附带的zlib.pas包括了两个压缩和解压流的辅助类。使用起来相当方便,这使得很多C++ Builder的使用者都想要使用这个zlib.pas,偏偏borland没有在BCB中带上这个东西。也许可能是担心C++社群对BCB进行攻击吧。哈~(我一直也认为本来就是C的源码,非要用pascal来封装,呵呵,特别怪啊)
不过得分要紧,而且Indy 9也使用了zlib的开放原始码,和zlib.pas的做法如出一辙,
肯定是有人抄袭 -_-|||。
由于delphi中使用了dcu来提高编译速度,所以borland并没有提供可供重新编译zlib.pas任何相关的obj文件。本来我们需要下载zlib的原始码,自己来动手编译。不过既然我记得indy 9里面有相同的obj,这一步就不用了。去http://www.indyproject.org/download/Borland.html 下载一个原始码。
解压出所需的obj文件和zlib.pas放在同一目录下,就可以成功的使用bcb来编译这个pas了,哈~
{$L deflate.obj}
{$L inflate.obj}
{$L inftrees.obj}
{$L trees.obj}
{$L adler32.obj}
{$L infblock.obj}
{$L infcodes.obj}
{$L infutil.obj}
{$L inffast.obj}
就是上面几个。
现在我们可以和使用其他的vcl类一样来使用了。
顺便给出我测试的代码:
#include "ZLib.hpp"
void CompressFile( AnsiString FileName, AnsiString CompressedFileName)
{
TFileStream *FIn, *FOut;
TCompressionStream* C;
if(!FileExists(FileName)) throw Exception("File not Exist");
FIn = new TFileStream( FileName, fmOpenRead | fmShareDenyWrite );
FOut = NULL;
if (FileExists(CompressedFileName))
FOut = new TFileStream(CompressedFileName, fmOpenWrite | fmShareExclusive);
else
FOut = new TFileStream(CompressedFileName, fmCreate | fmShareExclusive);
try
{
C = new TCompressionStream(Zlib::clMax, FOut);
try
{
C->CopyFrom(FIn, 0);
}
__finally
{
delete C;
}
}
__finally
{
delete FIn;
delete FOut;
}
}
//---------------------------------------------------------------------------
void DecompressFile(AnsiString FileName, AnsiString DecompressedFileName)
{
TFileStream *FIn, *FOut;
TDecompressionStream* D;
Byte Buf[4096];
int Count;
if(!FileExists(FileName)) throw Exception("File not Exist");
FIn = new TFileStream( FileName, fmOpenRead | fmShareDenyWrite );
FOut = NULL;
if (FileExists(DecompressedFileName))
FOut = new TFileStream(DecompressedFileName, fmOpenWrite | fmShareExclusive);
else
FOut = new TFileStream(DecompressedFileName, fmCreate | fmShareExclusive);
try
{
D = new TDecompressionStream(FIn);
try
{
for(Count = 1;Count>0;)
{
Count = D->Read(Buf, sizeof(Buf));
FOut->Write(Buf,Count);
}
}
__finally
{
delete D;
}
}
__finally
{
delete FIn;
delete FOut;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute() && SaveDialog1->Execute())
{
CompressFile(OpenDialog1->FileName,SaveDialog1->FileName);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(OpenDialog1->Execute() && SaveDialog1->Execute())
{
DecompressFile(OpenDialog1->FileName,SaveDialog1->FileName);
}
}
//---------------------------------------------------------------------------