Article View: borland.public.cppbuilder.nativeapi
Article #5399Re: Compress/uncompress a Buffer
From: sam
Date: Fri, 19 Aug 2005 18:51
Date: Fri, 19 Aug 2005 18:51
75 lines
3572 bytes
3572 bytes
Andrue, very thanks for your sample that works PERFECTLY with the magic
vector<BYTE> !
Here your code with some added for testing returned value came from compress
and uncompres zlib.
Thanks again, you are not TeamB by chance but by experience !
================================
#include <vector>
#include <zlib.h>
#define MULTIPLICATOR_FACTOR 3
//------------------------------------------------------------------------------
bool compress( AnsiString const & sInputStr ,std::vector<BYTE> &result)
{
//Alloue un Buffer �gale � 2 fois la taille de la chaine � d�compresser
int iSizeBuf = MULTIPLICATOR_FACTOR * sInputStr.Length();
std::vector<BYTE> resultTempo(iSizeBuf); // Allow for an overhead
DWORD len=resultTempo.size();
if(Z_OK == compress(&(resultTempo[0]),&len, reinterpret_cast<BYTE
*>(sInputStr.c_str()),sInputStr.Length())){
//Ajuste la taille � len qui est la taille r�elle utilis�e
resultTempo.resize(len);
result=resultTempo;
return true;
}
return false;
}
//------------------------------------------------------------------------------
bool decompressData( std::vector<BYTE> DecompressBuffer, AnsiString
&sResult)
{
//Taille du bufer qui contiendra la chaine d�compress�e
size_t SizeArray = MULTIPLICATOR_FACTOR * DecompressBuffer.size();
std::vector<BYTE> result(SizeArray);
DWORD len=result.size();
if(Z_OK ==
uncompress(&(result[0]),&len,&(DecompressBuffer[0]),DecompressBuffer.size())){
sResult = (AnsiString)reinterpret_cast<char *>(&(result[0]));
sResult.SetLength(len);
return true;
}
return false;
}
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
AnsiString sInput="I am testing the fabulous ZLIB compression library !!";
AnsiString sOutput;
std::vector<BYTE> compressedData;
if(!compress(sInput,compressedData))
return 0;
if(decompressData(compressedData, sOutput))
ShowMessage("["+sOutput+"]");
else
ShowMessage("Decompression error");
return 0;
}
================================
Sam
Technical Details
Message-ID:
<43062a49@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<4304a1d6@newsgroups.borland.com> <4304a3dd@newsgroups.borland.com> <4304c079@newsgroups.borland.com> <4305934f$1@newsgroups.borland.com> <4305ccc9@newsgroups.borland.com> <4305d471$1@newsgroups.borland.com> <4305f25b@newsgroups.borland.com> <43060098$1@newsgroups.borland.com> <430603f6$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,68b90959c01bbcf3 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII Reply-To: "sam" <s33@hotmail.com> From: "sam" <s33@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4304a1d6@newsgroups.borland.com> <4304a3dd@newsgroups.borland.com> <4304c079@newsgroups.borland.com> <4305934f$1@newsgroups.borland.com> <4305ccc9@newsgroups.borland.com> <4305d471$1@newsgroups.borland.com> <4305f25b@newsgroups.borland.com> <43060098$1@newsgroups.borland.com> <430603f6$1@newsgroups.borland.com> Subject: Re: Compress/uncompress a Buffer Date: Fri, 19 Aug 2005 20:51:59 +0200 Lines: 77 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 NNTP-Posting-Host: 83.195.12.122 Message-ID: <43062a49@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1124477513 83.195.12.122 (19 Aug 2005 11:51:53 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail