您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。