C++ C++;zLib压缩字节数组

C++ C++;zLib压缩字节数组,c++,zlib,C++,Zlib,我想用zlib压缩一个字节数组,下面是我的代码: 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 = (u

我想用zlib压缩一个字节数组,下面是我的代码:

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);
}
但是ReturnCode总是返回-2(Z_STREAM_ERROR)
我直接从zlib示例代码(example.c)中获取了这段代码,它在自己的示例程序中工作,并返回0(Z_OK),但在我的程序中返回-2

任何帮助都将不胜感激

您需要分配压缩缓冲区并将其大小作为前两个参数发送,如下所示:

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

您需要分配压缩缓冲区并将其大小作为前两个参数发送,如下所示:

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

在示例代码中设置“somereasonalsize”时,我如何猜测压缩数据的大小?ZLib要求压缩缓冲区大小至少比源缓冲区大小大0.1%,再加上12个字节(四舍五入时加1)。从compress()返回时,compresslen将包含生成的压缩大小函数将返回一个合适的目标缓冲区大小以供使用。如何猜测压缩数据的大小以在示例代码中设置“SomeReasonicalSize”?ZLib要求压缩缓冲区大小至少比源缓冲区大小大0.1%,再加上12个字节(加1表示舍入)。从compress()返回时,compresslen将包含生成的压缩大小。compressBound()函数将返回要使用的适当目标缓冲区大小。