官方帮助手册
http://www.zlib.net/manual.html
 
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "zlib.h"
 
/* Compress data */
int zcompress(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;
               if( deflateInit(&c_stream, Z_DEFAULT_COMPRESSION ) != 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;
}
/* Compress gzip data */
int 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;
               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;
}
/* Uncompress data */
int zdecompress(Byte *zdata , uLong nzdata ,
                            Byte * data, uLong * ndata)
{
        int err = 0;
        z_stream d_stream; /* decompression stream */
       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( inflateInit(&d_stream) != 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) return -1;
       }
        if(inflateEnd(&d_stream) != Z_OK) return -1;
       * ndata = d_stream.total_out;
        return 0;
}
/* HTTP gzip decompress */
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 = ( 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, 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;
}
/* Uncompress gzip data */
int gzdecompress(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,
       };
 
        int v = 20;
       * ndata = 20;
        for (;v<65330; v++)
       {
               zdata[v] = '\0';
       }
 
 
       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;
}
 
int main()
{
 
        char *data = "hello word. this test message for this programe.for our work!hello word. this test message for this programe.for our work!hello word. this test message for this programe.for our work!";
        uLong ndata = strlen(data);
        Bytef zdata[ BUF_SIZE];
        uLong nzdata = BUF_SIZE;
        Bytef odata[ BUF_SIZE];
        uLong nodata = BUF_SIZE;
       memset(zdata, 0, BUF_SIZE);
 
        //if(zcompress((Bytef *)data, ndata, zdata, &nzdata) == 0)
        if(gzcompress(( Bytef *)data, ndata, zdata, &nzdata) == 0)
       {
              fprintf( stdout, "nzdata:%d %s\n", nzdata, zdata);
              memset(odata, 0, BUF_SIZE);
               //if(zdecompress(zdata, ndata, odata, &nodata) == 0)
               if(gzdecompress(zdata, ndata, odata, &nodata) == 0)
               //if(httpgzdecompress(zdata, ndata, odata, &nodata) == 0)
              {
                     fprintf( stdout, "%d %s\n", nodata, odata);
              }
       }
}
  

zlib stream的更多相关文章

  1. composer "Failed to decode zlib stream"

    dockerFile 中安装composer.... RUN curl -s -f -L -o /tmp/installer.php https://raw.githubusercontent.com ...

  2. centos7 php7 安装composer时Failed to decode zlib stream解决办法

    1 下载安装脚本 php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 2 运行安装脚 ...

  3. 安装composer Failed to decode zlib stream 问题解决方法

    https://getcomposer.org/download/ 页面下载最新版本 composer.phar 放到php.exe 页面下.创建一个.bat文件,存入下面内容 @ECHO OFF p ...

  4. composer Failed to decode zlib stream 无法解码zlib流

    Win7 中安装 Composer (PHP) 国内有些网络不能访问美国的Composer官网,可访问 Composer 中文网 学习. 目标 可以在任何目录下的项目中执行 PHP composer. ...

  5. win7安装composer Failed to decode zlib stream

    今天学习php的时候想安装一下composer,刚开始采用的是exe文件的安装方式,可是安装了好几次都没有安装成功,如下图: 可能还有其他的一些错误,所以这里我就换了一个方式安装,就是自己手动来安装c ...

  6. Win7下安装composer, 并使用其安装smarty

    安装composer需要开启PHP openssl扩展. 1) 先查看PHP是否开启了openssl扩展 键盘win+r 输出cmd, 可以看到Dos窗口, 然后执行php -m (需要添加PHP环境 ...

  7. composer安装。

    我们这里实在Windows下进行的安装.1.下载Composer安装包.网址:https://getcomposer.org/download/下载Composer-Setup.exe 安装文件.2. ...

  8. table2excel使用

    原table2excel代码 /* * 采用jquery模板插件——jQuery Boilerplate * * Made by QuJun * 2017/01/10 */ //table2excel ...

  9. 如何查看PHP的配置信息

    1.问题描述 如何利用PHP函数查看PHP的配置信息 2.问题函数 <?php echo phpinfo(); ?> 3.输出结果 phpinfo() PHP Version => ...

随机推荐

  1. Windows 下开发.NET Core应用

    一.使用Visual Studio 2015开发1.1 依次安装Visual Studio 2015 Update 3.NET Core 1.0.0 - VS 2015 Tooling Preview ...

  2. 原始套接字--traceroute

    traceroute, 也就是 trace route,跟踪路由.这个程序最早是Van Jacobson实现的.源码在网上可以找到,不过我还没有去找.是IP路由过程中对数据包TTL(Time to L ...

  3. bash语法注意点

    bash 语法注意点 =和不能分开 如: val=expr $a + $b` [空格 *** 空格]条件判断要有空格 如: if [ $a ==$b ] 表达式和运算符之间要有空格, $a空格 + 空 ...

  4. Android可移动的Button

    关键 package com.example.administrator.mystudent.ButtonMove; import android.app.Activity; import andro ...

  5. urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed解决办法

    描述 今天学习写一篇用python(我的是Python 3.6)登录知乎网(https://www.zhihu.com) 的爬虫,测试时报错:urlopen error [SSL: CERTIFICA ...

  6. 【Python】- yield 使用浅析

    您可能听说过,带有 yield 的函数在 Python 中被称之为 generator(生成器),何谓 generator ? 我们先抛开 generator,以一个常见的编程题目来展示 yield ...

  7. nf_register_hooks解读

    nf_register_hooks是什么 net->netns_nf->nf_hook_entry[NFPROTO_NUMPROTO][NF_MAX_HOOKS=8] (nf)       ...

  8. WCF信道绑定代码

    监听端创建信道Listener,代码 using System; using System.Collections.Generic; using System.Linq; using System.T ...

  9. 洛谷T8115 毁灭

    题目描述 YJC决定对入侵C国的W国军队发动毁灭性打击.将C国看成一个平面直角坐标系,W国一共有n^2个人进入了C国境内,在每一个(x,y)(1≤x,y≤n)上都有恰好一个W国人.YJC决定使用m颗核 ...

  10. linux内核情景分析之execve()

    用来描述用户态的cpu寄存器在内核栈中保存情况.可以获取用户空间的信息 struct pt_regs { long ebx; //可执行文件路径的指针(regs.ebx中 long ecx; //命令 ...