读书人

求大神指导关于gzip解压出有关问题

发布时间: 2013-03-28 10:20:24 作者: rapoo

求大神指导关于gzip解压,出问题!
我从http上下载了gizp格式的数据,用解压缩软件已经解压成功,我现在想自己写解压代码,发现解压之后的都是空白,不知道什么原因,解压代码如下

#include <stdio.h>    
#include "zlib.h"
#pragma comment(lib, "./zlib1.lib")

int main()
{
/* 原始数据 */
FILE* p=fopen("mysocket_test.gzip","rb+");
FILE* p2=fopen("123.txt","wb");

unsigned char buf[104857] = {0};
unsigned char strDst[104857] = {0};
unsigned long bufLen = sizeof(buf);
unsigned long dstLen = sizeof(strDst);
fread(buf,1,145910,p);

/* 解压缩 */
uncompress(strDst, &dstLen, buf, 145910);
printf("%s\n",strDst);
fwrite(strDst,1,dstLen,p2);
return 0;
}
其中mysocket_test.gzip放着的是gzip压缩的数据,大小是145910.
[解决办法]
大哥你压缩了以后都145910了,你解压的buffer才104857,肯定放不下 啊,另外,看Unconpress的返回值,zlib.h里有错误吗的描述。
[解决办法]
引用:
引用:引用:
大哥你压缩了以后都145910了,你解压的buffer才104857,肯定放不下 啊,另外,看Unconpress的返回值,zlib.h里有错误吗的描述。大哥,分配的是不够,我改成了1048576,结果还是空白,返回值是Z_DATA_ERROR,怎么解决啊?实在是不了解zlib我把……


zlib 里有sample code。请参阅。我看的是 V1.2.7的版本。

/* ===========================================================================
* Test compress() and uncompress()
*/
void test_compress(compr, comprLen, uncompr, uncomprLen)
Byte *compr, *uncompr;
uLong comprLen, uncomprLen;
{
int err;
uLong len = (uLong)strlen(hello)+1;

err = compress(compr, &comprLen, (const Bytef*)hello, len);
CHECK_ERR(err, "compress");

strcpy((char*)uncompr, "garbage");

err = uncompress(uncompr, &uncomprLen, compr, comprLen);
CHECK_ERR(err, "uncompress");

if (strcmp((char*)uncompr, hello)) {
fprintf(stderr, "bad uncompress\n");
exit(1);
} else {
printf("uncompress(): %s\n", (char *)uncompr);
}
}

/* ===========================================================================
* Usage: example [output.gz [input.gz]]
*/

int main(argc, argv)
int argc;
char *argv[];
{
Byte *compr, *uncompr;
uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
uLong uncomprLen = comprLen;
static const char* myVersion = ZLIB_VERSION;

if (zlibVersion()[0] != myVersion[0]) {


fprintf(stderr, "incompatible zlib version\n");
exit(1);

} else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
fprintf(stderr, "warning: different zlib version\n");
}

printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());

compr = (Byte*)calloc((uInt)comprLen, 1);
uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
/* compr and uncompr are cleared to avoid reading uninitialized
* data and to ensure that uncompr compresses well.
*/
if (compr == Z_NULL
[解决办法]
uncompr == Z_NULL) {
printf("out of memory\n");
exit(1);
}

#ifdef Z_SOLO
argc = strlen(argv[0]);
#else
test_compress(compr, comprLen, uncompr, uncomprLen);

test_gzio((argc > 1 ? argv[1] : TESTFILE),
uncompr, uncomprLen);
#endif

test_deflate(compr, comprLen);
test_inflate(compr, comprLen, uncompr, uncomprLen);

test_large_deflate(compr, comprLen, uncompr, uncomprLen);
test_large_inflate(compr, comprLen, uncompr, uncomprLen);

test_flush(compr, &comprLen);
test_sync(compr, comprLen, uncompr, uncomprLen);
comprLen = uncomprLen;

test_dict_deflate(compr, comprLen);
test_dict_inflate(compr, comprLen, uncompr, uncomprLen);

free(compr);
free(uncompr);

return 0;
}

读书人网 >C语言

热点推荐