1. 程式人生 > >zlib庫 安裝與使用

zlib庫 安裝與使用

1. zlib簡介

  zlib 適用於資料壓縮的函式庫,由Jean-loup Gailly (負責compression)和 Mark Adler (負責decompression)開發。 
  zlib被設計成一個免費的、通用的、法律上不受阻礙(即沒有被任何專利覆蓋) 的無損資料壓縮庫。zlib幾乎適用於任何計算器硬體和作業系統。
  zlib本身的資料格式可以進行跨平臺的移植。 與在Unix上適用的LZW壓縮方法 以及 GIF 影象壓縮不同, zlib中適用的壓縮方法從不對資料進行拓展。(LZW在極端情況下會導致檔案大小變為原來的兩倍、甚至三倍)。zlib的記憶體佔用也是獨立於輸入資料的,並且在必要的情況下可以適當減少部分記憶體佔用。

2. zlib的安裝

首先, 下載zlib, 當前最新的 版本應該是1.2.8. 並解壓

windeal@ubuntu:opensource$ wget https://sourceforge.net/projects/libpng/files/zlib/1.2.8/zlib-1.2.8.tar.gz/download -o zlib-1.2.8.tar.gz
windeal@ubuntu:opensource$ tar zvfx zlib-1.2.8.tar.gz

進入zlib目錄,執行以下命令安裝zlib

windeal@ubuntu:zlib-1.2.8$ ./configure
windeal@ubuntu
:zlib-1.2.8$ make windeal@ubuntu:zlib-1.2.8$ make check windeal@ubuntu:zlib-1.2.8$ sudo make install

make install這一步,由於要把zlib安裝到/usr/local/lib 路徑下,所以可能需要root 許可權。
安裝成功後,可以在/usr/local/lib下找到 libz.a

libz.a是一個靜態庫,為了使用zlib的介面,我們必須在連線我們的程式時,libz.a連結進來。
只需在 連結命令後加-lz /usr/llocal/lib/libz.a 即可。

舉個例子, 我們有一個使用zlib庫的應用程式, 原始檔只有一個:zpipe.c

, 裡面呼叫了zlib的介面,這時執行以下命令編譯既可:

windeal@ubuntu:examples$ gcc -o zpipe.o -c zpipe.c
windeal@ubuntu:examples$ gcc -o zpipe zpipe.o -lz /usr/local/lib/libz.a 

zlib 提供了一系列用於壓縮和解壓的函式介面, 具體的含義可以參考zlib manual

3. zlib的使用

zlib 在 examples 路徑下提供了許多使用zlib的例子:

[email protected]:examples$ ls
enough.c  fitblk.c  gun.c  gzappend.c  gzjoin.c  gzlog.c  gzlog.h  README.examples  zlib_how.html  zpipe.c  zran.c
[email protected]:examples$ 

README.examples 有介紹各個example的作用。
這些例子已經大概說明了zlib的基本用法。

3.1 compress 與 uncompress

compressuncompress是zlib最基本的兩個函數了。他們分別用於壓縮和解壓資料。 原型如下:

ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
                                 const Bytef *source, uLong sourceLen));
ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
                                   const Bytef *source, uLong sourceLen));

引數型別Bytef表示位元組流,它與字串有所不同,位元組流沒有結束符,因而需要配備長度資訊,處理字串的時候需要把結束符也當成一個普通的位元組。 而uLongf則用於指明長度資訊了, 其實相當於unsigned long

看下面的例子:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <zlib.h>


int main(int argc, char *argv[])
{ 
    char inbuf[] = "Hello, This is a demo for compress and uncompress interface!\n"
                 "Written by windeal.li\n"
                 "email: [email protected]\n";
    uLong inlen = sizeof(inbuf);
    char *outbuf = NULL;
    uLong outlen;

    outlen = compressBound(inlen);
    printf("in_len: %ld\n", inlen);
    printf("out_len: %ld\n", outlen);

    if ((outbuf = (char *)malloc(sizeof(char) * outlen)) == NULL){
        fprintf(stderr, "Failed to malloc memory for outbuf!\n");
        return -1;
    }

    /* compress */
    if (compress(outbuf, &outlen, inbuf, inlen) != Z_OK) {
        fprintf(stderr, "Compress failed!\n");
        return -1;
    }
    printf("Compress Sucess!\n");
    printf("\routlen:%ld, outbuf:%s\n", outlen, outbuf);

    memset(inbuf, 0, sizeof(inbuf));
    /* Uncompress */
    if (uncompress(inbuf, &inlen, outbuf, outlen) != Z_OK){
        fprintf(stderr, "Uncompress failed!\n");
        return -1;
    }
    printf("Uncompress Success!\n");
    printf("\rinlen:%ld, inbuf:%s\n", inlen, inbuf);

    /* free memory */
    if (outbuf != NULL){
        free(outbuf);
        outbuf = NULL;
    }

    return 0;
}

3.2 infate、deflate、z_stream介紹

這裡infatedefate 其實是指兩組函式
+ deflateInit() + deflate() + deflateEnd()用於完成流的壓縮
+ inflateInit() + inflate() + inflateEnd()用於完成解壓縮功能

z_stream是上面兩組函式中用到的,用來表示流的資料結構。

typedef struct z_stream_s {
    z_const Bytef *next_in;     /* next input byte */
    uInt     avail_in;  /* number of bytes available at next_in */
    uLong    total_in;  /* total number of input bytes read so far */

    Bytef    *next_out; /* next output byte should be put there */
    uInt     avail_out; /* remaining free space at next_out */
    uLong    total_out; /* total number of bytes output so far */

    z_const char *msg;  /* last error message, NULL if no error */
    struct internal_state FAR *state; /* not visible by applications */

    alloc_func zalloc;  /* used to allocate the internal state */
    free_func  zfree;   /* used to free the internal state */
    voidpf     opaque;  /* private data object passed to zalloc and zfree */

    int     data_type;  /* best guess about the data type: binary or text */
    uLong   adler;      /* adler32 value of the uncompressed data */
    uLong   reserved;   /* reserved for future use */
} z_stream;    

看下兩組函式的原型:

int deflateInit (z_streamp strm, int level);  //初始化壓縮狀態,關聯相關的z_stream資料結構和壓縮比例
int deflate (z_streamp strm, int flush);   //壓縮資料,  flush表示以何種方式將壓縮的資料寫到緩衝區中。
int deflateEnd (z_streamp strm);    //壓縮結束
int inflateInit (z_streamp strm);        
int inflate (z_streamp strm, int flush);
int inflateEnd (z_streamp strm); 

關於這兩組函式的具體使用, examples目錄下有幾個具體的例子(如zpipe.c),可以直接參考。

相關推薦

zlib 安裝使用

1. zlib簡介   zlib 適用於資料壓縮的函式庫,由Jean-loup Gailly (負責compression)和 Mark Adler (負責decompression)開發。    zlib被設計成一個免費的、通用的、法律上不受阻礙(即沒有被

SQL server數據安裝表的基本使用

col 技術 gem -o x64 log sql shadow png **** SQL server 安裝與基礎使用**** 一.安裝SQL Server 2008 R2企業版(64位)x64前的準備 二、安裝S

mysql數據安裝配置

fig creat rest restart mysql常用命令 mysql 需要 數據庫安裝 upd 1.(訪問賬號權限控制)dql-查詢(select) dml-(insert update delete) ddl-(create table create view)

mongodb數據安裝卸載

all x86 端口 AR 刪除 進行 -m 文件 bsp 此處以centos下monggodb3.4版本安裝為例,可參考官網安裝教程 步驟如下: 1、配置mongodb ym源 vi /etc/yum.repos.d/mongodb-org-3.4.repo 文件內容

1.mysql數據安裝卸載

mysql 安裝 卸載 安裝前可以刪除mysql/bin/*.pdb(臨時文件)安裝步驟:如何卸載mysql?設置開機啟動mysql服務安裝mysql時指定服務名1.mysql數據庫安裝與卸載

Oracle數據安裝連接簡介

名稱 bsp dev ali 技術分享 改變 oracle oracle官網 註意 Oracle數據庫的安裝 1.登錄Oracle官網——試用和下載 2.同意協議--->file1 3.完成配置 4.測試連接:打開Oracle developer---

深度學習安裝使用

Theano windows下 Download Anaconda now! conda install mingw libpython pip install theano Keras Windows下 通過 cond

oprofile安裝使用

一、概述 oprofile庫是linux平臺上的一個功能強大的效能分析工具,支援兩種取樣方式:基於事件的取樣與基於時間的取樣。 1)基於事件的取樣:oprofile只記錄特定事件(比如L2 cache miss)的發生次數,當達到使用者設定的值時,oprofile就記錄一下

python3影象識別安裝使用

pytesseract庫的安裝 因為用的win10,就直說windows上面的安裝了。其實就是pip安裝就完事了。 $ pip install pytesseract 安裝了這個還不算完,得安裝Tesseract-OCR,安裝這個軟體的時候,因為我們需

Zlib安裝使用

1 /* zpipe.c: example of proper use of zlib's inflate() and deflate() 2 Not copyrighted -- provided to the public domain 3 Version 1.4 11 De

MySQL5.6 數據主從(Master/Slave)同步安裝配置詳解

inux bind 主從配置 希望 master 強調 數據庫主從 ria 配置文件 目錄(?)[+] 安裝環境 操作系統 :CentOS 6.5 數據庫版本:MySQL 5.6.27 主機A:192.168.1.1 (Master) 主機B:192.168.

CentOS6.4下Mysql數據安裝配置

商業 storage 不同的 pool use 速度 man aries ora 原文連接:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 說到數據庫,我們大多想到的是關系型數據

Mysql數據一:安裝創建windows服務

clear 程序 啟動 hang spa -- pan mysql數據庫 top Mysql數據庫安裝與創建windows服務 1.先下載壓縮包(mysql-5.7.18-winx64.zip)移動到對應目錄(如D:\software)後解壓. 2.安裝服務端: m

MySQL數據(1)_MySQL數據介紹安裝

structure 文件 nbsp code 字符串常量 blank 擴展性 比較 模式 一、數據庫相關概念的簡介   數據庫(database,DB)是指長期存儲在計算機內的,有組織,可共享的數據的集合。數據庫中的數據按一定的數學模型組織、描述和存儲,具有較小的冗余,較高

Mysql-day1數據安裝介紹

圖片 找到 環境變量 版本 安裝步驟 系統 com 安裝位置 image 一、mysql的安裝步驟 以5.7.20版本為例: 第1步: 第2步: 第3步: 第4步: 第5步: 第6步: 第7步: 第8步: 第9步:

oracle數據安裝連接關鍵點

navi clas bubuko mage 遠程連接 http height 連接 成了 一、window xp系統上安裝Oracle Database 10G 解鎖Scott、Hr賬號並重置口令 遠程連接數oracle數據庫地址 二、在Mac系統上使用Na

數據服務器的安裝配置 如何搭建數據專用服務器

mar sso 混合模式 api 需要 按鈕 有一個 通過 性能 理論基礎 數據庫服務器是當今應用最為廣泛的一種服務器類型,許多企業在信息化建設過程中都要購置數據庫服務器。數據庫服務器主要用於存儲、查詢、檢索企業內部的信息,因此需要搭配專用的數據庫系統,對服務器的兼容性、可

【MySQL系列】01.數據簡介MySQL安裝

都在 批量導入數據 ces 網絡 file 升級版 key rep 監聽 去年就想寫MySQL的教程,但是由於學的不好就沒有誤導大家,今年就把學習中的經驗分享給大家,大家也可以加我的QQ群:運維架構師交流群 ~~~~群號:476794643~~~~,一起學習交流 01.數

高性能內存對象緩存Memcached安裝及數據操作管理

客戶端程序 環境變量 rest close sql數據庫 blog erl gcc-c++ 安裝php 認識Memcached Memcached是一套開源的高性能分布式內存對象緩存系統,它將所有的數據都存儲在內存中,因為在內存中會統一維護一張巨大的Hash表,所以支持任意

Redis緩存數據安裝配置(1)

cte quest ready 計算 測試工具 報錯信息 數據庫的安裝 參數說明 div 1.安裝 tarxf redis-3.2.5.tar.gz cd redis-3.2.5 make mkdir -p /usr/local/redis/bin src目錄下這些文件作