- 论坛徽章:
- 0
|
本帖最后由 rjamesz 于 2013-07-19 17:54 编辑
- #include <zlib.h>
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string>
- #include <string.h>
- using namespace std;
- /*add by reggie */
- int main(){
- string message,pmessage;
- message = "absdfafsdsdf";
- uLong destlen = compressBound((uLong)message.size());
- const char *tmpbuf = message.c_str();
- char *compressbuf = new char[destlen];
- int ret = compress((Byte*)compressbuf, &destlen, (const Byte*)tmpbuf, (uLong)message.size());
- fprintf(stderr,"before compress is <%lu>,after compress is <%lu>\n",message.size(),strlen(compressbuf));
- if(ret == Z_OK)
- {
- fprintf(stderr, "compress success <%lu>\n", destlen);
- pmessage = compressbuf;
- }
- else
- {
- fprintf(stderr, "compress failed: %d", ret);
- pmessage = message;
- }
- delete[] compressbuf;
- //fprintf(stderr,"after compress ptr is [%s],len is <%lu>\n",pmessage.c_str(),pmessage.size());
- /*end by reggie*/
-
- /*add by reggie */
- {uLong destlen = pmessage.size() * 30;//30随便写的,一般不超过10怕不够大
- fprintf(stderr,"message size is <%lu>\n",pmessage.size());
- char *uncompressbuf = new char[destlen];
- int ret = uncompress((Byte*)uncompressbuf, &destlen, (const Byte*)pmessage.c_str(), pmessage.size());
- fprintf(stderr,"uncompress string is [%s]\n",uncompressbuf);
- if(ret == Z_OK)
- {
- fprintf(stderr,"uncompress success %lu\n", destlen);
- }
- else
- {
- fprintf(stderr, "uncompress failed: %d\n", ret);
- }
- delete[] uncompressbuf;
- }
- }
复制代码 是想要给数据在网络传输中压缩一下,本来一个客户端压缩,服务端解压。上面的代码是我一个测试的代码,压缩完解压显示-3错误,为甚么有一些多余的变量,因为是在源码里面拿出来测试的,稍微改了一下。麻烦大家帮忙看下为甚么一直解压错误。
PS:编译的时候 -lz |
|