当前位置: 首页 > news >正文

QT外部库:zlib

前言

 新建项目:pro文件中新增代码

LIBS+= -lz

在main.cpp函数中#include "zlib.h",如果此时运行代码提示没有找到对应的函数,那么就qt安装目录:D:\C++\qt5.12.7\Tools\mingw730_64\x86_64-w64-mingw32\include(这里是博主的路径,作为参考找自己的路径)下查看是否有zlib.h头文件,复制这个文件到当前目录下即可运行

数据流压缩解压缩


#include "zlib.h"
#include <zlib.h>int main(int argc,char * argv[])
{
//    QApplication a(argc, argv);char text[] = "zlib compress and uncompress test\nturingo@163.com\n2012-11-05\n";uLong tlen = strlen(text) + 1;	/* 需要把字符串的结束符'\0'也一并处理 */char* buf = NULL;uLong blen;cout <<"start: tlen = "<< tlen << endl;/* 计算缓冲区大小,并为其分配内存 */blen = compressBound(tlen);	/* 压缩后的长度是不会超过blen的 */if((buf = (char*)malloc(sizeof(char) * blen)) == NULL){printf("no enough memory!\n");return -1;}cout << "compressBound: blen =  " << blen<< endl;
//        /* 压缩 */if(compress((Bytef*)buf, &blen,(Bytef*) text, tlen) != Z_OK){printf("compress failed!\n");return -1;}cout <<"compress \n";cout << strlen(buf)+1 << endl;cout <<"uncompress \n";/* 解压缩 */if(uncompress((Bytef*)text, &tlen, (Bytef*)buf, blen) != Z_OK){printf("uncompress failed!\n");return -1;}/* 打印结果,并释放内存 */printf("%s", text);if(buf != NULL){free(buf);buf = NULL;}//    return a.exec();return 0;
}
   char text[] = "zlib testddsa ";uLong tlen = strlen(text) + 1;	/* 需要把字符串的结束符'\0'也一并处理 */char* buf = NULL;uLong blen;cout <<"start: tlen = "<< tlen << endl;/* 计算缓冲区大小,并为其分配内存 */blen = compressBound(tlen);	/* 压缩后的长度是不会超过blen的 */if((buf = (char*)malloc(sizeof(char) * blen)) == NULL){printf("no enough memory!\n");return -1;}cout << "blen = " << blen<< endl;compress((Bytef*)buf,&blen,(Bytef*)text,tlen);cout << "blen = "<< blen<< endl;cout << "strlen(buf) = " << strlen(buf)<< endl;uLong ubound = compressBound(strlen(buf)+1 );/* 压缩后的长度是不会超过blen的 */cout << "bound =" << ubound<< endl;uncompress((Bytef*)text,&ubound,(Bytef*)buf,blen);cout << "unbound =" << ubound<< endl;

文件压缩解压缩

        uLong blen,tlen;char text[1024];char* dest;FILE* f,ff;
//        f = fopen("test.txt","r+");//        fread(text,1,1024,f);
//        tlen = strlen(text)+1;
//        cout << text << endl;
//        cout << "tlen = "<< tlen  << endl;//        blen = compressBound(tlen);
//        cout << "blen = "<< blen  << endl;//        dest = (char *)malloc(blen );
//        compress((Bytef*)dest,&blen,(Bytef*)text,tlen);//        cout << "blen = "<< blen  << endl;
//        ff = fopen("test.aaa","w");
//        fwrite(dest,blen,1,ff);
//        fclose(ff);
//        fclose(f);f= fopen("test.aaa","r+");fread(dest,1024,1,f);fclose(f);
//            cout << dest << endl;blen = compressBound(strlen(dest)+1);uncompress((Bytef*)text,&blen,(Bytef*)dest,1024);cout << text << endl;

http://www.lryc.cn/news/348633.html

相关文章:

  • 钡铼技术BL205模块分布式IO集成应用风电场状态监测
  • java博客目录
  • 从零开始学习Linux(8)----自定义shell
  • 《大数据分析-数据仓库项目实战》学习笔记
  • JDK介绍
  • JavaScript 对象入门:基础用法全解析
  • 如何获得一个Oracle 23ai数据库(docker容器)
  • 想跨境出海?云手机提供了一种可能性
  • 制药行业新突破:CANOpen转PROFINET网关配置案例解析
  • vue前端时间段选择控件
  • 用wordpress建外贸独立站的是主流的外贸建站方式
  • 差异基因散点图绘制教程
  • Windows安装多版本MySQL
  • Redis7降级到Redis6如何AOF备份恢复(错的)
  • 通过EXCEL控制PLC启停电机的一种方法
  • 【GPT4O 开启多模态新时代!】
  • HTTP协议及Python实现
  • 【机器学习】逻辑化讲清PCA主成分分析
  • Vue常见的指令
  • 【Ansible】ansible-playbook剧本
  • Linux的命令
  • No known conditions for “./lib/locale/lang/zh-cn“ entry in “element-plus“ pa
  • 实验名称:TCP 连接管理
  • go语言map底层及扩容机制原理详解(上)
  • 互联网职场说 | “领导找我谈话,原来是给我涨薪,但却只涨了200,还偷偷叮嘱我保密,这次只给我涨了薪”
  • Android 如何启用user版本的adb源码分析
  • linux phpstudy 重启命令
  • 台式电脑屏幕亮度怎么调节?让你的眼睛更舒适!
  • 打造安全的 Linux 环境:实用配置指南
  • 神经网络有哪些算法