见示例:

其中调用inflate()后,zlib会保存解压进程,avail_in,next_in会更新,保存解压进程的断点。avail_out == 0说明这次输出内存用完,数据可能还没解压完,需要继续调用inflate(), 会从上次的位置继续解压。

#include <stdio.h>
/* For "exit". */
#include <stdlib.h>
/* For "strerror". */
#include <string.h>
/* For "errno". */
#include <errno.h>
#include <zlib.h>/* CHUNK is the size of the memory chunk used by the zlib routines. */#define CHUNK 0x4000/* The following macro calls a zlib routine and checks the returnvalue. If the return value ("status") is not OK, it prints an errormessage and exits the program. Zlib's error statuses are all lessthan zero. */#define CALL_ZLIB( x )                                                                             \{                                                                                              \int status;                                                                                \status = x;                                                                                \if( status < 0 )                                                                           \{                                                                                          \fprintf( stderr,                                                                       \"%s:%d: %s returned a bad status of %d.\n",                                   \__FILE__,                                                                     \__LINE__,                                                                     \#x,                                                                           \status );                                                                     \exit( EXIT_FAILURE );                                                                  \}                                                                                          \}/* if "test" is true, print an error message and halt execution. */#define FAIL( test, message )                                                                      \{                                                                                              \if( test )                                                                                 \{                                                                                          \inflateEnd( &strm );                                                                   \fprintf( stderr,                                                                       \"%s:%d: " message " file '%s' failed: %s\n",                                  \__FILE__,                                                                     \__LINE__,                                                                     \file_name,                                                                    \strerror( errno ) );                                                          \exit( EXIT_FAILURE );                                                                  \}                                                                                          \}/* These are parameters to inflateInit2. Seehttp://zlib.net/manual.html for the exact meanings. */#define windowBits 15
#define ENABLE_ZLIB_GZIP 32int main()
{const char*   file_name = "test.gz";FILE*         file;z_stream      strm = {0};unsigned char in[CHUNK];unsigned char out[CHUNK];strm.zalloc = Z_NULL;strm.zfree = Z_NULL;strm.opaque = Z_NULL;strm.next_in = in;strm.avail_in = 0;CALL_ZLIB( inflateInit2( &strm, windowBits | ENABLE_ZLIB_GZIP ) );/* Open the file. */file = fopen( file_name, "rb" );FAIL( !file, "open" );while( 1 ){int bytes_read;int zlib_status;bytes_read = fread( in, sizeof( char ), sizeof( in ), file );FAIL( ferror( file ), "read" );strm.avail_in = bytes_read;strm.next_in = in;do{unsigned have;strm.avail_out = CHUNK;strm.next_out = out;zlib_status = inflate( &strm, Z_NO_FLUSH );switch( zlib_status ){case Z_OK:case Z_STREAM_END:case Z_BUF_ERROR:break;default:inflateEnd( &strm );fprintf( stderr, "Gzip error %d in '%s'.\n", zlib_status, file_name );return -1;}have = CHUNK - strm.avail_out;fwrite( out, sizeof( unsigned char ), have, stdout );} while( strm.avail_out == 0 );if( feof( file ) ){inflateEnd( &strm );break;}}FAIL( fclose( file ), "close" );return 0;
}

Ref:

https://www.lemoda.net/c/zlib-open-read/

https://www.zlib.net/manual.html

C/C++ 用zlib解压gzip文件相关推荐

  1. Zlib解压/压缩实现

    Zlib解压/压缩实现 针对目前2503平台,请参考以下方式实现zlib解压/压缩文件: 1:将plutommi\Customer\ResGenerator\zlib整个文件夹copy到贵司的modu ...

  2. linux图形界面解压rar文件夹,压缩与解压

    出自Linux Wiki 提示:此文已超过 5 年(1880 天)未更新,如发现内容过时或有误,欢迎改进:) 本文介绍Linux环境中处理rar.zip.gz.bz2.tar等常见压缩/归档文件的方法 ...

  3. 使用tar解压gz文件出现 “not in gzip format”错误解决办法

    我在解压.gz文件时使用的下面命令: tar -zxvf *.tar.gz 然后出现下面的提示: gzip: stdin: not in gzip format tar: Child returned ...

  4. Linux C语言 Zlib 实现解压zip文件

    Linux C语言 Zlib 实现解压zip文件 因为在做嵌入式Linux,做OTA功能的时候需要解压zip升级包.所以需要用到zip文件的解压缩功能. 就想着能不能用zlib来完成这个功能.在网上一 ...

  5. 解压 *.gz文件 gzip压缩

    压缩文件 gzip file1.txtls -l file1.* -rw-r--r-- 1 root root 134416 Aug 21 08:08 file1.txt.gz 解压文件 gzip - ...

  6. python 解压zip文件_Python 解压缩文件详解

    zipfile模块及相关方法介绍: 1 压缩 1.1 创建zipfile对象 zipfile.ZipFile(file, mode='r', compression=0, allowZip64=Tru ...

  7. C#压缩解压zip 文件

    我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...

  8. 解压zip文件出现-bash:unzip:commond not found

    来源:http://www.cnblogs.com/thinksasa/archive/2013/01/20/2868232.html 解压zip文件出现-bash:unzip:commond not ...

  9. java解压gz文件

    http://panshaobinsb.iteye.com/blog/1566231 下面是网上的代码 http://www.iteye.com/topic/894879 Java代码   impor ...

  10. python批量解压文件_python 批量解压压缩文件的实例代码

    下面给大家介绍python 批量解压压缩文件的实例代码,代码如下所述: #/usr/bin/python#coding=utf-8import os,sys import zipfile open_p ...

最新文章

  1. 软件测试培训分享:性能测试的目的是什么
  2. [life]见证本届世界杯意大利的出局
  3. Fedora telnet配置
  4. 27个赢得别人欣赏的诀窍
  5. 【C语言】创建一个函数,利用该函数将两个字符串连接起来
  6. red hat 5.3补丁_您如何解释组织的宗旨? 来自Red Hat的3课
  7. 产品经理应该先写需求文档还是先画原型?
  8. 利用Ninject实现依赖注入
  9. 什么是MES?与ERP有什么区别呢?
  10. python数据处理可以做什么菜_厌倦了EXCEL想玩点新花样?教你利用Python做数据筛选(下)...
  11. 使用 WebView2 封装一个生成 PDF 的 WPF 控件
  12. 动视暴雪利润下降22%,投身移动端能否重回王座?
  13. html鼠标经过自动下拉菜单,用纯CSS实现鼠标经过后出现下拉菜单,实例讲解(附代码)...
  14. AWS ARN 学习
  15. 微信支付报错提示“商户号该产品权限未开通,请前往商户平台产品中心检查后重试...
  16. java半角转全角_JAVA 半角全角相互转换
  17. IDEA中配置Vue启动Configurations
  18. React中实现类似v-click-outside效果
  19. C# Linq 的三种去重方式(Distinct)
  20. AWS S3 in china 地址

热门文章

  1. php转换编码%e4,常见编码之间的转换
  2. LInux usb mouse(鼠标)驱动分析
  3. 中国各省所处的经纬度范围
  4. 苹果电脑上堪称神器的几款软件
  5. win10运行Flink
  6. Linux卸载驱动方法
  7. Zigbee协议栈中文说明
  8. Windows10易升下载
  9. PHP调用拼多多接口以及配置签名
  10. 写给Javaer看的Kotlin教程