先用ps后用dw做網(wǎng)站私域流量營(yíng)銷
前言
?新建項(xiàng)目:pro文件中新增代碼
LIBS+= -lz
在main.cpp函數(shù)中#include "zlib.h",如果此時(shí)運(yùn)行代碼提示沒(méi)有找到對(duì)應(yīng)的函數(shù),那么就qt安裝目錄:D:\C++\qt5.12.7\Tools\mingw730_64\x86_64-w64-mingw32\include(這里是博主的路徑,作為參考找自己的路徑)下查看是否有zlib.h頭文件,復(fù)制這個(gè)文件到當(dāng)前目錄下即可運(yùn)行
數(shù)據(jù)流壓縮解壓縮
#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; /* 需要把字符串的結(jié)束符'\0'也一并處理 */char* buf = NULL;uLong blen;cout <<"start: tlen = "<< tlen << endl;/* 計(jì)算緩沖區(qū)大小,并為其分配內(nèi)存 */blen = compressBound(tlen); /* 壓縮后的長(zhǎng)度是不會(huì)超過(guò)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;}/* 打印結(jié)果,并釋放內(nèi)存 */printf("%s", text);if(buf != NULL){free(buf);buf = NULL;}// return a.exec();return 0;
}
char text[] = "zlib testddsa ";uLong tlen = strlen(text) + 1; /* 需要把字符串的結(jié)束符'\0'也一并處理 */char* buf = NULL;uLong blen;cout <<"start: tlen = "<< tlen << endl;/* 計(jì)算緩沖區(qū)大小,并為其分配內(nèi)存 */blen = compressBound(tlen); /* 壓縮后的長(zhǎng)度是不會(huì)超過(guò)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 );/* 壓縮后的長(zhǎng)度是不會(huì)超過(guò)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;