几个数据压缩库介绍

背景:项目中的需要与工业的无线网络进行通讯,工业使用的无线网络主要用于传输传感器的实时数据,由于无线的处理能力有限,现阶段是收集到一个包处理后就转发,为了提高网络的传输效率,无线系统想接受到几个包后,进行压缩在转发。所以特找到 了几个数据压缩库,看网上的使用代码较少,特demo如下代码,

1. Zlib

//引入zlib头文件和zlib静态库,注意在引入静态库的时候还需要定义ASMV;ASMINF;ZLIB_WINAPI;
//如果你使用的是vsstudio环境可以再项目的属性中的c++下的preprocessor下的processor definition下添加上面三个宏定义
#ifdef __cplusplus
extern "C"{
#endif

#include "Zlib\zlib.h"
#include "Zlib\zconf.h"

#ifdef __cplusplus
}
#endif
#include "Zlib.h"
#pragma comment(lib,"zlibstat.lib")

double ZlibCompress(char * text, unsigned long sourceLen)
{
	//这个只是介绍数据压缩含义,具体的解压缩如出一辙
	//compressBound函数获取压缩后数据最大字节数,这个是为了保证你压缩后存储的缓存大小,这个地方我没有使用
	uLong blen = compressBound(sourceLen);
	char buf[1000] = { 0 };
	//compress函数第一参数是压缩后数据存储的缓冲,compress成功后,函数将改变第二个参数blen的值为压缩后数据的大小,
	//注意第二个参数要的是一个指针型
	//第三个参数是要压缩的文本呢,第四个参数是要压缩文本的大小
	if (compress((unsigned char *)buf, &blen, (unsigned char *)text, sourceLen) != Z_OK)
	{
		printf("compress failed!\n");
		return -1;
	}
	return (double)blen / sourceLen;
}

2. Quicklz

<pre name="code" class="cpp">//quicklz库只包含了两个文件:quicklz.h和quicklz.c
#ifdef __cplusplus
extern "C" {
#endif

#include "Quicklz\quicklz.h"
#include "Quicklz\quicklz.c"

#ifdef __cplusplus
}
#endif

double QuicklzCompress(char * text, int sourceLen)
{
	qlz_state_compress *state_compress = (qlz_state_compress *)malloc(sizeof(qlz_state_compress));
	
	// Always allocate size + 400 bytes for the destination buffer when compressing.
	char *compressed = (char *)malloc(sourceLen + 400);
	int r;

	r = qlz_compress(text, compressed, sourceLen, state_compress);
	//printf("Compressed %d bytes into %d bytes.\n", sourceLen, r);
	free(state_compress);
	free(compressed);
	return (double)r / sourceLen;
}


 

3. bzip2

http://en.wikipedia.org/wiki/Bzip2 维基百科下的external link下有各个版本的库下载。

#ifdef __cplusplus
extern "C"
{
#endif
#include "libZip\bzlib.h"
#ifdef __cplusplus
}
#endif
#pragma comment(lib,"bzip2.lib")


double LibZipCompress(char * text, int sourceLen)
{
	bz_stream  bzStream = { 0 };
	bzStream.bzalloc = NULL;
	bzStream.bzfree = NULL;
	bzStream.opaque = NULL;
	//next_in 要压缩的文本,avail_in文本的大小
	bzStream.next_in = text;
	bzStream.avail_in = sourceLen;
	char buffer[1000] = {0};
	//压缩后数据存储的缓冲
	bzStream.next_out = buffer;
	bzStream.avail_out = 1000;

	BZ2_bzCompressInit(&bzStream, 9, 0, 0);

	int bStatus;
	do{
		//数据的压缩不是一次就能进行完的,bzip会根据需要压缩的数据大小进行分次压缩,压缩完的数据放入
		//next_out所指向的缓冲当中,如果compress函数返回的bz_run_ok继续循环,如果返回的bz_stream_end
		//则结束
		bStatus = BZ2_bzCompress(&bzStream, (bzStream.avail_in) ? BZ_RUN : BZ_FINISH);
		if (bStatus != BZ_RUN_OK && bStatus != BZ_STREAM_END)
			printf("error");
	} while (bStatus != BZ_STREAM_END);
	//压缩成功后,next_out将存储压缩后的数据,total_out_lo32和total_out_ho32用来进入数据的大小
	double ratio = (double)bzStream.total_out_lo32 / sourceLen;
	BZ2_bzCompressEnd(&bzStream);
	return ratio;
}

4 对比压缩率(时间不考虑)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值