zlip库的下载编译和使用

1.下载网址


http://zlib.net/

2.安装到本地

目前最新版本zlib是zlib1.2。8,安装开始;
$tar -xvzf zlib-1.2.3.tar.gz
$cd zlib-1.2.3.tar.gz
$./configure
$make
$sudo make install

3.使用

借鉴的别人的压缩和解压函数

gzip.c gzip.h

gzip.h

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <zlib.h>
      5 #ifndef _ZGZIP_H
      6 #define _ZGZIP_H
      7 #define  Z_HEADER_SIZE 12
      8 
      9 /* Compress gzip data */
     10 int gzcompress(Byte *data, uLong ndata, Byte *zdata, uLong *nzdata);
     11 
     12 /* http gzip Uncompress data */
     13 int httpgzdecompress(Byte *zdata, uLong nzdata,Byte *data, uLong *ndata);
     14 #endif

gzip.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "zlib.h"

#ifndef GZIP_H
#define GZIP_H

/* Compress gzip data */
/* data 原数据 ndata 原数据长度 zdata 压缩后数据 nzdata 压缩后长度 */
int gzcompress(Byte *data, uLong ndata, 
	Byte *zdata, uLong *nzdata)
{
	z_stream c_stream;
	int err = 0;

	if(data && ndata > 0)
	{
		c_stream.zalloc = NULL;
		c_stream.zfree = NULL;
		c_stream.opaque = NULL;
		if(deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 
			 MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1;
		c_stream.next_in  = data;
		c_stream.avail_in  = ndata;
		c_stream.next_out = zdata;
		c_stream.avail_out  = *nzdata;
		while (c_stream.avail_in != 0 && c_stream.total_out < *nzdata) 
		{
			if(deflate(&c_stream, Z_NO_FLUSH) != Z_OK) return -1;
		}
		if(c_stream.avail_in != 0) return c_stream.avail_in;
		for (;;) {
			if((err = deflate(&c_stream, Z_FINISH)) == Z_STREAM_END) break;
			if(err != Z_OK) return -1;
		}
		if(deflateEnd(&c_stream) != Z_OK) return -1;
		*nzdata = c_stream.total_out;
		return 0;
	}
	return -1;
}

/* HTTP gzip decompress */
/* zdata 数据 nzdata 原数据长度 data 解压后数据 ndata 解压后长度 */
int httpgzdecompress(Byte *zdata, uLong nzdata,
	Byte *data, uLong *ndata)
{
	int err = 0;
	z_stream d_stream = {0}; /* decompression stream */
	static char dummy_head[2] = {
		0x8 + 0x7 * 0x10,
		(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
	};
	d_stream.zalloc = NULL;
	d_stream.zfree = NULL;
	d_stream.opaque = NULL;
	d_stream.next_in  = zdata;
	d_stream.avail_in = 0;
	d_stream.next_out = data;
	//if(inflateInit2(&d_stream, MAX_WBITS + 16) != Z_OK) return -1;
	if(inflateInit2(&d_stream, 47) != Z_OK) return -1;
	while(d_stream.total_out < *ndata && d_stream.total_in < nzdata) {
		d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
		if((err = inflate(&d_stream, Z_NO_FLUSH)) == Z_STREAM_END) break;
		if(err != Z_OK) {
			if(err == Z_DATA_ERROR) {
				d_stream.next_in = (Bytef*) dummy_head;
				d_stream.avail_in = sizeof(dummy_head);
				if((err = inflate(&d_stream, Z_NO_FLUSH)) != Z_OK) {
					return -1;
				}
			} else return -1;
		}

	}
	if(inflateEnd(&d_stream) != Z_OK) return -1;
	*ndata = d_stream.total_out;
	return 0;
}

#endif // GZIP_H

main.c

      1 #include <stdio.h>
      2 #include "gzip.h"
      3 
      4 int main()
      5 {
      6     int err;
      7     Byte compr[200], uncompr[200];    // big enough
      8     uLong comprLen, uncomprLen;
      1 #include <stdio.h>
      2 #include "gzip.h"
      3 
      4 int main()
      5 {
      6     int err;
      7     Byte compr[200], uncompr[200];    // big enough
      8     uLong comprLen, uncomprLen;
      9     char* hello = "12345678901234567890123456789012345678901234567890";
     10 
     11     uLong len = strlen(hello) + 1;
     12     comprLen  = sizeof(compr) / sizeof(compr[0]);
     13     err = gzcompress((Byte*)hello, len, compr, &comprLen);
     14 
     15     if (err != Z_OK)
     16     {
     17         exit(1);
     18     }
     19     printf("cpmprlen=%d\n",comprLen);
     20     strcpy((char*)uncompr, "garbage");
     21     err = httpgzdecompress(compr, comprLen, uncompr, &uncomprLen);
     22 
     23     if (err != Z_OK)
     24     {
     25         exit(1);
     26     }
     27 
     28 
     29     if (strcmp((char*)uncompr, hello))
     30     {
     31         exit(1);
     32     } else {
     33 
     34         printf("uncomprlen=%d\nuncompr=%s\n",uncomprLen,uncompr);
     35     }
     36 
     37     return 0;
     38 }

编译gcc main.c gzip.c -lz





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ZLIB是一种用于数据压缩的开源,它提供了各种压缩和解压缩的功能。TCP/IP协议栈是Internet上最常用的网络通信协议集合,用于在计算机之间进行数据传输。 在嵌入式系统中,由于资源受限和处理能力有限,使用压缩技术可以有效地减少网络数据的传输量,提高网络传输的效率和速度。而ZLIB作为一种高效的数据压缩算法,可以应用于嵌入式系统中的TCP/IP协议栈,用于压缩和解压缩网络数据。 在嵌入式系统中使用ZLIB TCP/IP协议栈的应用可以有以下几个方面: 首先,ZLIB可以用于嵌入式系统中的数据压缩,可以将要发送的数据进行压缩,有效减少数据包的大小,减少带宽的占用,提高数据传输的速度。 其次,ZLIB可以用于嵌入式系统中的数据解压缩,对于接收到的经过压缩的数据包,可以使用ZLIB进行解压缩,恢复原始数据,提高数据的可读性和使用性。 此外,ZLIB还可以用于提供数据的完整性保护。在数据传输过程中,通过对原始数据进行压缩,再进行传输,接收端在接收到数据后可以使用ZLIB进行解压缩,并进行校验,以确保数据的完整性。 总的来说,通过在嵌入式系统中使用ZLIB TCP/IP协议栈,可以有效地提高网络数据传输的效率和速度,减少带宽的占用,同时保证数据的完整性。这在对于资源有限和处理能力有限的嵌入式系统中尤为重要,能够提高系统的性能和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值