If you want to compress or decompress file when writing C++ code,you can choose zlib library,that's quite easy.

1,Download zlib library's code form website:http://zlib.net/

2,Decompress the file you've downloaded,and then go to the directory "zlib-1.2.11\zlib-1.2.11\contrib\vstudio\vc14", open the zlibvc.sln in it with your vs.

3,Now you have opened the project of zlib,you need to compile it next.rebuild the project named "zlibvc",there are errors:

1>  The system cannot find the path specified.

1>  'bld_ml64.bat' is not recognized as an internal or external command,

1>  operable program or batch file.

1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(123,5): error MSB3073: The command "cd ..\..\contrib\masmx64

1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(123,5): error MSB3073: bld_ml64.bat

1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(123,5): error MSB3073: :VCEnd" exited with code 9009.

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

4,you need to reset the Properties of the project,select Build Event,then select Pre-Build Event,then delete the content of Command Line on it.rewrite the command line with your zlib's path.for example:

C:
cd C:\Users\sonne\Desktop\zlib-1.2.11\zlib-1.2.11\contrib\masmx64
bld_ml64.bat

so 'C:\Users\sonne\Desktop\zlib-1.2.11' is the path where I put my zlib downloaded form website before.

5,compile again,rebuild your project,now succeed.

After compiling zlib library,now need to use the dll.

1,put the zlibwapi.dll and zlibwapi.lib in your project.

2,you also need to include the zlib.h and zconf.h.

3,add a line in cpp file:

#pragma comment(lib,"zlibwapi.lib")

4,now you can use zlib APIs.

here is zlib usage examples: https://zlib.net/zlib_how.html

the manual is also useful: https://zlib.net/manual.html

5, my test code here,shows how to compress and decompress by zlib:

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "zlib.h" #define CHUNK 16384
#pragma comment(lib,"zlibwapi.lib") int def(FILE *source, FILE *dest, int level)
{
int ret, flush;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, level);
if (ret != Z_OK)
return ret;
/* compress until end of file */
do {
strm.avail_in = fread(in, , CHUNK, source);
if (ferror(source)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
strm.next_in = in;
/* run deflate() on input until output buffer not full, finish
compression if all of source has been read in */
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, flush); /* no bad return value */
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
have = CHUNK - strm.avail_out;
if (fwrite(out, , have, dest) != have || ferror(dest)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
} while (strm.avail_out == );
assert(strm.avail_in == ); /* all input will be used */
/* done when last data in file processed */
} while (flush != Z_FINISH);
assert(ret == Z_STREAM_END); /* stream will be complete */
/* clean up and return */
(void)deflateEnd(&strm);
return Z_OK;
} int inf(FILE *source, FILE *dest)
{
int ret;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = ;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if (ret != Z_OK)
return ret;
/* decompress until deflate stream ends or end of file */
do {
strm.avail_in = fread(in, , CHUNK, source);
if (ferror(source)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}
if (strm.avail_in == )
break;
strm.next_in = in;
/* run inflate() on input until output buffer not full */
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void)inflateEnd(&strm);
return ret;
}
have = CHUNK - strm.avail_out;
if (fwrite(out, , have, dest) != have || ferror(dest)) {
(void)inflateEnd(&strm);
return Z_ERRNO;
}
} while (strm.avail_out == );
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);
/* clean up and return */
(void)inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
} int main() {
FILE *fp_src;
FILE *fp_dst; //compress a file
//fp_src = fopen( "test.txt", "r" );
//fp_dst = fopen( "myzip.zip", "w" );
//int ret = def(fp_src,fp_dst,9); //decompress a file compressed
fp_src = fopen( "myzip.zip", "r" );
fp_dst = fopen( "test2.txt", "w");
int ret = inf(fp_src, fp_dst); return ;
}

use zlib lib to compress or decompress file的更多相关文章

  1. cloudera-scm-server启动时出现Caused by: java.io.FileNotFoundException: /var/lib/cloudera-scm-server/.keystore (No such file or directory)问题解决方法(图文详解)

    不多说,直接上干货! 问题详情 查看/var/log/cloudera-scm-server.log的启动日志 问题来源 我在用cloudermanager安装好之后,然后,在对如下. 配置kerbe ...

  2. Starting MySQL ** mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists

    本地虚拟机(CentOS6.8)启动MySQL(MySQL5.6.35)服务失败 [root@VMUest ~]# service mysql status ERROR! MySQL is not r ...

  3. error while loading shared libraries: lib******: cannot open shared object file: No such file or directory

    程序编译成功后,运行时错误: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object fi ...

  4. Compress and decompress string

    You are given a string with lower case letters only. Compress it by putting the count of the letter ...

  5. failed to create rwlayer: lstat /var/lib/docker/overlay2/ no such file or directory

    在使用Docker构建微服务镜像时出现的错误.第一天构建好好的,第二天就出现了这样的错误.通过百度这条错误的信息非常少,只在 stackoverflow.com 上找到一条,问题指向了 dockerf ...

  6. C# GZip Compress DeCompress

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. Zlib库的安装与使用

    在实际应用中经常会遇到要压缩数据的问题,常见的压缩格式有zip和rar,而Linux下那就更多了,bz2,gz,xz什么的都有,单单Linux下的解压和压缩命令就有好多呢?没有什么好不好的.查了资料, ...

  8. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  9. [充电][库]Zlib文件压缩和解压

    原文链接: http://www.cnblogs.com/fairycao/archive/2009/12/09/1620414.html 开源代码:http://www.zlib.net/zlib使 ...

随机推荐

  1. junit测试模板 unit-test

    一个项目能否发布上线,重要的环节就是测试.经过集成测试.性能测试.压力测试等不断循环的测试过后依据测试报告来确定上线.这些由专业的测试人员来完成,因此会导致程序开发者对自身的单元测试的弱化.若在代码中 ...

  2. wifi入侵思路

    一.得到wifi密码   系统:Kali Linux   工具:Aircrack-ng,EWSA   方法:   1.WEP加密:deauth攻击:得到足够报文直接破解.   2.WPA加密:deau ...

  3. 使用quartz实现不重启服务器修改自定义配置

    为了方便维护系统,开发中通常会设置一些自定义参数,写在单独的配置文件里,需要调整时可直接登录服务器修复配置文件,而不需要修改程序.但尴尬的是,web服务器并不会自动重新加载配置文件,重启服务器又会中断 ...

  4. 开发问题(一)在windows和linux端口占用问题

    前言 今天在MyEclipse中使用tomcat发现tomcat端口8080竟然被占用了,所以就找了一下解决办法共参考! 在网络程序的调试过程中,经常发生一些出乎意料的事情,比如创建一个TCP服务失败 ...

  5. HDU2825 Wireless Password

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

  6. 微信小程序使用字体图标的方法

    一.先到阿里巴巴矢量图标库(http://iconfont.cn/),用微博帐号登录,搜索你想要的图标,然后添加入库 从项目里下载下来并解压,找到ttf格式文件 二.到这个平台https://tran ...

  7. cesium编程入门(四)界面介绍及小控件隐藏

    感性认识 界面介绍,viewer Geocoder : 查找位置工具,查找到之后会将镜头对准找到的地址,默认使用bing地图 Home Button :视角返回初始位置. Scene Mode Pic ...

  8. 浅析@Deprecated,调用方法时出现横线划掉样式

    Deprecated 这个注释是一个标记注释.所谓标记注释,就是在源程序中加入这个标记后,并不影响程序的编译,但有时编译器会显示一些警告信息. 那么Deprecated注释是什么意思呢?如果你经常使用 ...

  9. Mysql开启远程连接方法

    分类: 数据库开发技术 解决MySQL不允许从远程访问的方法 开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要 ...

  10. iis配置完成,出现HTTP 错误 403.14 - Forbidden

    版权声明:本文为博主原创文章,未经博主允许不得转载.转载请标明文章出处和原文链接. 403.14 禁止访问:在 Web 服务器上已拒绝目录列表 解决方案一:一般情况站点都是不会允许直接读取目录内容的, ...