【发布时间】:2011-11-24 07:28:49
【问题描述】:
我正在尝试实现 zlib.h deflate 和 inflate 函数来压缩和解压缩 char 数组(不是文件)。
我想知道下面的语法是否正确?我是否遗漏了什么或定义不正确?
char a[50] = "Hello World!";
char b[50];
char c[50];
// deflate
// zlib struct
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in = (uInt)sizeof(a); // size of input
defstream.next_in = (Bytef *)a; // input char array
defstream.avail_out = (uInt)sizeof(b); // size of output
defstream.next_out = (Bytef *)b; // output char array
deflateInit(&defstream, Z_DEFAULT_COMPRESSION);
deflate(&defstream, Z_FINISH);
deflateEnd(&defstream);
printf("Deflate:\n%lu\n%s\n", strlen(b), b);
// inflate
// zlib struct
z_stream infstream;
infstream.zalloc = Z_NULL;
infstream.zfree = Z_NULL;
infstream.opaque = Z_NULL;
infstream.avail_in = (uInt)sizeof(b); // size of input
infstream.next_in = (Bytef *)b; // input char array
infstream.avail_out = (uInt)sizeof(c); // size of output
infstream.next_out = (Bytef *)c; // output char array
inflateInit(&infstream);
inflate(&infstream, Z_NO_FLUSH);
inflateEnd(&infstream);
printf("Inflate:\n%lu\n%s\n", strlen(c), c);
【问题讨论】:
-
你问是因为它不起作用吗?您是否收到某种错误消息?
-
@larsks 它编译时没有警告,但我想知道我选择的函数和定义是否有意义,或者我是否应该使用其他函数和定义。
-
知道了。感谢您澄清您的问题。
标签: c compression zlib deflate