LZO是一个开源的无损压缩C语言库,其优点是压缩和解压缩比较迅速占用内存小等特点(网络传输希望的是压缩和解压缩速度比较快,压缩率不用很高),其还有许多其他的优点详细参考其网站:
关于这个库性能的介绍可以参考
从下载minilzo-2.06.tar.gz压缩包之后,注意里面提供了1个C文件3个头文件,分别是minilzo.c,minilzo.h,lzoconf.h,lzodefs.h。
上代码吧!使用时包含minilzo.h头文件,注意lzo1x_1_compress和lzo1x_decompress两个函数。
//测试代码#include#include "minilzo.h" //注意头文件 #include #include using namespace std;#define IN_LEN 1024#define OUT_LEN 1024static unsigned char __LZO_MMODEL in [ IN_LEN ];static unsigned char __LZO_MMODEL out [ OUT_LEN ];#define HEAP_ALLOC(var,size) \ lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);int main(){ lzo_uint in_len; lzo_uint out_len; lzo_uint new_len; if (lzo_init() != LZO_E_OK) { printf("internal error - lzo_init() failed !!!\n"); printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n"); return 3; } /*压缩过程*/ in_len = IN_LEN; lzo_memset(in,0,in_len); in[0] = 0x11; in[1] = 0x11; in[2] = 0x11; //将in压缩成out int r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem); if (r == LZO_E_OK) printf("compressed %lu bytes into %lu bytes\n", (unsigned long) in_len, (unsigned long) out_len); else { /* this should NEVER happen */ printf("internal error - compression failed: %d\n", r); return 2; } //out_len是压缩后的缓冲区长度 printf("out[0]=%02x, outlen=%d\n",out[0],out_len); /*解压缩过程*/ new_len = in_len; //将out解压缩成in r = lzo1x_decompress(out,out_len,in,&new_len,NULL); if (r == LZO_E_OK && new_len == in_len) printf("decompressed %lu bytes back into %lu bytes\n", (unsigned long) out_len, (unsigned long) in_len); else { /* this should NEVER happen */ printf("internal error - decompression failed: %d\n", r); return 1; } //in_len是解压缩后还原的长度 printf("in[0]=%02x, inlen=%d\n",in[0], in_len); return 0;}