Gzip压缩封装

直接上代码微笑

#ifndef YANGCHEN_GZIP_INCLUDED
#define YANGCHEN_GZIP_INCLUDED

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

namespace YANGCHEN
{
	class GZip
	{
	public:
		static int gzcompress(Bytef *data, uLong ndata, Bytef *zdata, uLong *nzdata);
		static int gzdecompress(Byte *zdata, uLong nzdata, Byte *data, uLong *ndata);
	};
}

#endif

#include "GZip.h"

namespace YANGCHEN
{
	int GZip::gzcompress(Bytef *data, uLong ndata, 
		Bytef *zdata, uLong *nzdata)
	{
		z_stream c_stream;
		int err = 0;

		if(data && ndata > 0)
		{
			c_stream.zalloc = (alloc_func)0;
			c_stream.zfree = (free_func)0;
			c_stream.opaque = (voidpf)0;

			// change by yangchen 2013.4.7
			if(deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK) 
			{
				return -1;
			}
			//if(deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 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;
	}

	int GZip::gzdecompress(Byte *zdata, uLong nzdata,                 
		Byte *data, uLong *ndata)
	{
		int err = 0;
		z_stream d_stream = {0}; 
		static char dummy_head[2] = 
		{
			0x8 + 0x7 * 0x10,
			(((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
		};
		d_stream.zalloc = (alloc_func)0;
		d_stream.zfree = (free_func)0;
		d_stream.opaque = (voidpf)0;
		d_stream.next_in  = zdata;
		d_stream.avail_in = 0;
		d_stream.next_out = data;

		// if(inflateInit2(&d_stream, -MAX_WBITS) != Z_OK) return -1;
		// change by yangchen 2013.4.7
		if(inflateInit2(&d_stream, 15+16) != 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;
	}
}
参考:http://hispider.googlecode.com/svn/trunk/devel/hispider/src/utils/zstream.c
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值