zlib compression string

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi all,

I was trying to do compression of char. Please have an look at the code below
<pre class="prettyprint int GetMaxCompress(int nLenSrc)
{
int n16k = (nLenSrc+16383)/16384;
return(nLenSrc+6+(n16k*5));
}
BYTE* CompressData(const BYTE* abSrc, int nLenSrc,BYTE* abDst, int nLenDst)
{
z_stream zi = {0};
zi.total_in = zi.avail_in = nLenSrc;
zi.total_out = zi.avail_out = nLenDst;
zi.next_in = (BYTE*)abSrc;
zi.next_out = abDst;
int nErr, nRet = -1;
nErr = deflateInit(&zi,Z_BEST_COMPRESSION);
if(nErr == Z_OK)
{
nErr = deflate(&zi,Z_FINISH);
if(nErr == Z_STREAM_END)
{
nRet = zi.total_out ;
}
}
//inflateEnd(&zi);
return(abDst);
}
int uncompressdata(const BYTE* abSrc, int nLenSrc, BYTE* adDst, int nLenDst)
{
z_stream zi ={0};
zi.total_in = zi.avail_in = nLenSrc;
zi.total_out = zi.avail_out = nLenDst;
zi.next_in = (BYTE*)abSrc;
zi.next_out = adDst;
int nErr, nRet = -1;
nErr = inflateInit(&zi, Z_DEFAULT_COMPRESSION);
if(nErr == Z_OK)
{
nErr = inflate(&zi,Z_FINISH);
if(nErr == Z_STREAM_END)
{
nRet = zi.total_out ;
}
}
deflateEnd(&zi);
return(nRet);
}


void DemoDlg::OnBnClickedButton2()
{
BYTE pdsrc[] = "hello world";
int nlenorig = strlen((char *)pdsrc)+1;
int nlendst = GetMaxCompress(nlenorig);
BYTE* pddst = new byte [nlendst];
BYTE* pdst = new byte[nlendst];
//int nlenpacked = CompressData(pdsrc,nlenorig,pddst,nlendst);
CompressData(pdsrc,nlenorig,pddst,nlendst);
int rt = uncompressdata(pdsrc,nlenorig,pdst,nlendst);
int size = strlen((char *)pddst);
int siz = strlen((char *)pdst);
CString st;
st.Format(L"NO of charis %dnChars in compressed data %dnChars in decompressed data is %d n GetMaxCompress return is %d",nlenorig,size,siz,nlendst);
MessageBox((LPCTSTR)st);
CFile file; CString path("c:\test\te.dat");
if(file.Open(path, CFile::modeCreate | CFile::modeWrite))
{
char a[] = {0x00};
//file.Write(&pdsrc,sizeof(pdsrc));
for(int i=1;i<=4;i++){file.Write(&a,1);}
file.Write(&pddst,sizeof(pddst));
for(int i=1;i<=4;i++){file.Write(&a,1);}
file.Write(&pdst,sizeof(pdst));
file.Close();
}

}[/code]
I get strange result. Please help me sort out the solution. The compression is not happening as it should do...?

View the full article
 
Back
Top