#include <zlib.hpp>
void __fastcall TFrmMain::BtnCompressClick(TObject *Sender) // 压缩
{
TMemoryStream *pMemsrc=new TMemoryStream();
pMemsrc->LoadFromFile("E://Test.bdb"); //源流
pMemsrc->Seek(0,soFromEnd);
pMemsrc->Seek(0,soFromBeginning);
int count=pMemsrc->Size;
TMemoryStream *pDestMem=new TMemoryStream; //目的流
TCompressionStream *pCompress=new TCompressionStream(Zlib::clMax, pDestMem);
pMemsrc->SaveToStream(pCompress); // 压缩
delete pCompress; //析构,完成数据压缩的任务
pDestMem->Seek(0,soFromBeginning);
pDestMem ->SaveToFile("E://Compressed.dat");
delete pDestMem;
delete pMemsrc;}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender) // 解压
{
TMemoryStream *pMemsrc=new TMemoryStream;
pMemsrc->LoadFromFile("E://Compressed.dat");
pMemsrc->Seek(0,soFromBeginning);
TDecompressionStream *pCompress;
pCompress=new TDecompressionStream(pMemsrc);
pCompress->Seek(0,soFromBeginning);
TMemoryStream *pDestMem=new TMemoryStream;
pCompress->Seek(0,soFromBeginning);
try
{
while(1)
pCompress->Seek(1,soFromCurrent);
}
catch(...)
{
}
int count=pCompress->Position;
char * pbuf=(char*)malloc(count);
pCompress->Seek(0,soFromBeginning);
pCompress->ReadBuffer(pbuf,count);
pDestMem->WriteBuffer(pbuf,count);
pDestMem->Seek(0,soFromBeginning);
pDestMem->SaveToFile("E://test1.bdb");
delete pDestMem;
delete pCompress;
delete pMemsrc;
}
Zlib在Delphi中才有,但是也不是缺省安装的,你需要在Delphi5光盘中的/Info/Extras/Zlib中才能找到Zlib的东西。
1.先将D:/ Info/Extras/Zlib/(假设你的光驱盘符为D:)下的东西复制到你的硬盘上的某个目录。
2.将 /Info/Extras/Zlib/obj/*.obj与/Info/Extras/Zlib/*.pas复制到 ../CBuilder5/Lib/Obj
3.新建一个工程,然后将zlib.pas添加到你的工程中(通过Project|Add to Project…),然后移除Form1,选择菜单Project|Complie Unit,编译zlib.pas此时此生成zlib.dcu、zlib.obj、zlib.hpp、zlibconst.dcu、 zlibconst.obj、zlibconst.hpp六个文件
4.打开zlib.hpp在namespace Zlib前添加如下代码:
#pragma link "zlib.obj"
#pragma link "zlibconst.obj"
5.将zlib.hpp和zlibconst.hpp拷贝到…/CBuilder5/Include目录下
6.将zlib.obj和zlibconst.obj拷贝到…/Cbuilder5/Lib/Obh目录下
这样你就将zlib安装到了C++Builder中,你可以开始使用它
在C++BUILDER中应用ZLIB
最新推荐文章于 2017-01-26 11:33:00 发布