zlib (gzip) (二) winodws gzib使用

压缩:

#include "stdafx.h"
#include "zlib/zlib.h"
#include <stdlib.h>
#include <windows.h>
#pragma comment (lib, "zlibstatic.lib")
static int  gzCompress(const unsigned char *src, int srcLen, unsigned char *dest, int destLen)
{
	z_stream c_stream;
	int err = 0;
	int windowBits = 15;
	int GZIP_ENCODING = 16;

	/* 压缩级别 */
//#define Z_NO_COMPRESSION         0  
//#define Z_BEST_SPEED             1  
//#define Z_BEST_COMPRESSION       9  
//#define Z_DEFAULT_COMPRESSION  (-1)  
	if (src && srcLen > 0)
	{
		c_stream.zalloc = (alloc_func)0;
		c_stream.zfree = (free_func)0;
		c_stream.opaque = (voidpf)0;
		if (deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
			windowBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY) != Z_OK) return -1;
		c_stream.next_in = (Bytef *)src;
		c_stream.avail_in = srcLen;
		c_stream.next_out = (Bytef *)dest;
		c_stream.avail_out = destLen;
		while (c_stream.avail_in != 0 && c_stream.total_out < destLen)
		{
			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;
		return c_stream.total_out;
	}
	return -1;
}

解压:(有问题)

static int  gzDecompress(const char *src, int srcLen, const char *dst, int dstLen,
    int * outLen) {
    z_stream strm;
    strm.zalloc = NULL;
    strm.zfree = NULL;
    strm.opaque = NULL;

    strm.avail_in = srcLen;
    strm.avail_out = dstLen;
    strm.next_in = (Bytef *)src;
    strm.next_out = (Bytef *)dst;

    int err = -1, ret = -1;
    err = inflateInit2(&strm, MAX_WBITS + 16);
    if (err == Z_OK) {
        err = inflate(&strm, Z_FINISH);
        if (err == Z_STREAM_END) {
            ret = strm.total_out;
        }
        else {
            inflateEnd(&strm);
            return err;
        }
    }
    else {
        inflateEnd(&strm);
        return err;
    }
    inflateEnd(&strm);
    (*outLen) = ret;
    return err;
}

这个解压在 MD编译的情况下会有问题

更新一下

  int utils_idcardverify::gzDecompress(const unsigned char *src, int srcLen, const unsigned char *dst,  
	   int * outLen)
   
   {
	   Byte *zdata = (Byte *)src;
	   uLong nzdata = (uLong)srcLen;
	   Byte *data = (Byte *)dst;
	   uLong *ndata = (uLong*)outLen;
	   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 = (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;
	   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;
   }

具体:https://github.com/GreateLi/windws_zlib/edit/master/README.md

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋恋西风

up up up

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值