C++ zLib compress byte array

泄露秘密 提交于 2019-12-25 03:49:18

问题


I want to compress a byte array with zlib and here is my code :

void CGGCBotDlg::OnBnClickedButtonCompress()
{
   // TODO: Add your control notification handler code here
   z_const char hello[] = "hello, hello!";
   Byte *compr;
   uLong comprLen;

   int ReturnCode;
   uLong Length = (uLong)strlen(hello)+1;

   ReturnCode = compress(compr, &comprLen, (const Bytef*)hello, Length);
}

But ReturnCode always returns -2 (Z_STREAM_ERROR)
I took this code directly from zlib example codes (example.c), it works on its own example program and it returns 0 (Z_OK) there but in my program it returns -2
Any help would be appreciated


回答1:


You need to allocate the compression buffer and send its size as the first two params, like so:

Byte compr[SomeReasonableSize];
uLong comprLen = sizeof(compr);
...


来源:https://stackoverflow.com/questions/16464925/c-zlib-compress-byte-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!