紧缩和膨胀(zlib.h)用C [英] deflate and inflate (zlib.h) in C

查看:211
本文介绍了紧缩和膨胀(zlib.h)用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的zlib.h deflate和膨胀功能融为一体preSS和DECOM preSS字符数组(而不是文件)。

我想知道,如果下面的语法是正确的?我缺少的东西或事情定义不正确?

一个烧焦[50] =的Hello World!;
焦B〔50];
焦C [50];//放气
// zlib的结构
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in =(UINT)的sizeof(一); //输入的大小
defstream.next_in =(Bytef *)一个; //输入字符数组
defstream.avail_out =(UINT)的sizeof(B); //输出的大小
defstream.next_out =(Bytef *)B: //输出字符数组deflateInit(安培; defstream,Z_DEFAULT_COM preSSION);
放气(放; defstream,Z_FINISH);
deflateEnd(安培; defstream);的printf(放气:\\ n%吕\\ n%S \\ n,strlen的(B),B);//膨胀
// zlib的结构
z_stream infstream;
infstream.zalloc = Z_NULL;
infstream.zfree = Z_NULL;
infstream.opaque = Z_NULL;
infstream.avail_in =(UINT)的sizeof(B); //输入的大小
infstream.next_in =(Bytef *)B: //输入字符数组
infstream.avail_out =(UINT)的sizeof(C); //输出的大小
infstream.next_out =(Bytef *)C; //输出字符数组inflateInit(安培; infstream);
膨胀(安培; infstream,Z_NO_FLUSH);
inflateEnd(安培; infstream);的printf(充气:\\ n%吕\\ n%S \\ n,strlen的(C),C);


解决方案

您不能printf的像这样的泄气输​​出。这不是空终止。你不能strlen的,要么。

由于您输入的是一个字符串,虽然你也许只想通过包括空终止字符串的内容。所以设置avail_in为strlen的(A)+ 1。

您需要检查next_out和avail_out领域你叫放气,看看有多少数据被写入到输出缓冲区后。

这里见放气号召下文档

下面是修改过的code。注意:如果你的COM pressing未尝不是一个字符串你需要改变这一点,也与琴弦你可能COM preSS没有终止零后DECOM pressing添加回来。

 的char a [50] =!Hello World的;
焦B〔50];
焦C [50];//放气
// zlib的结构
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
defstream.avail_in =(UINT)的strlen(一)+1; //输入字符串+终结者的大小
defstream.next_in =(Bytef *)一个; //输入字符数组
defstream.avail_out =(UINT)的sizeof(B); //输出的大小
defstream.next_out =(Bytef *)B: //输出字符数组deflateInit(安培; defstream,Z_DEFAULT_COM preSSION);
放气(放; defstream,Z_FINISH);
deflateEnd(安培; defstream);//这是获得的输出的大小的一种方式
的printf(平减尺寸为:鲁%\\ n,(字符*)defstream.next_out - B);//膨胀
// zlib的结构
z_stream infstream;
infstream.zalloc = Z_NULL;
infstream.zfree = Z_NULL;
infstream.opaque = Z_NULL;
infstream.avail_in =(UINT)((字符*)defstream.next_out - B); //输入的大小
infstream.next_in =(Bytef *)B: //输入字符数组
infstream.avail_out =(UINT)的sizeof(C); //输出的大小
infstream.next_out =(Bytef *)C; //输出字符数组inflateInit(安培; infstream);
膨胀(安培; infstream,Z_NO_FLUSH);
inflateEnd(安培; infstream);的printf(充气:\\ n%吕\\ n%S \\ n,strlen的(C),C);

I am trying to implement the zlib.h deflate and inflate functions to compress and decompress a char array (not a file).

I would like to know if the following syntax is correct ? Am I missing something or defined something incorrectly ?

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);

解决方案

You can't printf the deflated output like this. It's not null terminated. You can't strlen it either.

Since your input is a string though you probably do want to only pass the content of the string including the null terminator. So set avail_in to strlen(a) + 1.

You need to examine the next_out and avail_out fields after you call deflate to see how much data was written to the output buffer.

See documentation here under the deflate call.

Here's your modified code. Note if you're compressing something that is not a string you'll need to change this and also with strings you may compress without the terminating zero and add it back after decompressing.

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)strlen(a)+1; // size of input, string + terminator
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);

// This is one way of getting the size of the output
printf("Deflated size is: %lu\n", (char*)defstream.next_out - b);

// inflate
// zlib struct
z_stream infstream;
infstream.zalloc = Z_NULL;
infstream.zfree = Z_NULL;
infstream.opaque = Z_NULL;
infstream.avail_in = (uInt)((char*)defstream.next_out - 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);

这篇关于紧缩和膨胀(zlib.h)用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆