zlib中基本函数都有哪些,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
获取版本函数:zlibVersion
压缩函数:deflateInit、deflate、deflateEnd
解压缩函数:inflateInit、inflate、inflateEnd
#include <iostream> #include <string> #include <zlib.h> #include <zconf.h> using namespace std; #define CHUNK 16384 int def(FILE *source, FILE *dest, int level) { z_stream strm; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; deflateInit(&strm, level); int flush; int have; unsigned char in[CHUNK]; unsigned char out[CHUNK]; do { strm.avail_in = fread(in, 1, CHUNK, source); strm.next_in = in; flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; do { strm.avail_out = CHUNK; strm.next_out = out; deflate(&strm, flush); have = CHUNK - strm.avail_out; fwrite(out, 1, have, dest); } while (0 == strm.avail_out); } while (Z_FINISH != flush); deflateEnd(&strm); return Z_OK; } int inf(FILE *source, FILE *dest) { z_stream strm; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; inflateInit(&strm); int ret = Z_OK; int have; unsigned char in[CHUNK]; unsigned char out[CHUNK]; do { strm.avail_in = fread(in, 1, CHUNK, source); strm.next_in = in; if(0 == strm.avail_in) break; do { strm.avail_out = CHUNK; strm.next_out = out; ret = inflate(&strm, Z_NO_FLUSH); have = CHUNK - strm.avail_out; fwrite(out, 1, have, dest); } while (0 == strm.avail_out); } while (Z_STREAM_END != ret); inflateEnd(&strm); return Z_OK; } void main(int argc, char *argv[]) { cout<<"zlibVersion:"<<zlibVersion()<<endl; FILE *fp1 = fopen("1.txt","rb+"); FILE *fp2 = fopen("2.txt", "rb+"); FILE *fp3 = fopen("3.txt", "rb+"); if((NULL == fp3) || (NULL == fp3) || (NULL == fp3)) { cout<<"fail to open file."<<endl; return; } //压缩 def(fp1, fp2, Z_DEFAULT_COMPRESSION); //解压缩,这个2个函数要分别调用。 inf(fp2, fp3); }
此代码参数官网示例代码,本代码为了学习,只写了最简单的函数,没有错误处理。
看完上述内容,你们掌握zlib中基本函数都有哪些的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。