Android原生(Native)C開發之五:zlib移植筆記

來源:互聯網
上載者:User

 原文:http://blog.sina.com.cn/s/blog_4a0a39c30100b4ao.html

 

 

zlib(http://www.zlib.net/)是一套非常流行的且開源的壓縮、解壓縮庫,由Jean-loup
Gailly與Mark
Adler所開發,初版0.9版在1995年5月1日發表。zlib使用DEFLATE演算法,最初是為libpng函式庫所寫的,後來普遍為許多軟體所使用。

zlib目前的版本是
1.2.3,其移植性非常好,本人已經在Windows、Linux、WinCE、Symbian等平台成功地移植了zlib,當然也包括今天要移植的平台:Android。

首先,從zlib 的官方網站下載zlib的原始碼:zlib123.zip,解壓後得到一個目錄zlib-1.2.3,zib已經提供好了一個Makefile,改動幾行(19、20、27、36、37)就可以動手編譯了,改動如下:

......
CC = arm-none-linux-gnueabi-gcc

CFLAGS =
-O2
......
LDFLAGS=libz.a -s -static
......
AR =
arm-none-linux-gnueabi-ar rc
RANLIB = arm-none-linux-gnueabi-ranlib
......

進入zlib-1.2.3目錄,在命令列輸入:make 即可。

最後會產生兩個可Android可執行檔: example 、minigzip 。

運行Android模擬器, 進入命令列,將兩個可執行檔 push 至Android模擬器,再執行,命令如下:

 

$adb push example
/dev/sample/example
$adb
push minigzip /dev/sample/minigzip
$adb shell chmod 777 /dev/sample/*
$adb shell
#cd /dev/sample
#./example


zlib version 1.2.3 = 0x1230, compile flags = 0x55

uncompress(): hello, hello!

gzread(): hello, hello!

gzgets() after gzseek:  hello!

inflate(): hello, hello!

large_inflate(): OK

after inflateSync(): hello, hello!

inflate with dictionary: hello, hello!

#ls -l

ls -l
-rwxrwxrwx
root    
root      
513228 2008-11-10 04:18 minigzip
-rwxrwxrwx
root    
root      
517640 2008-11-10 04:18 example
-rwxrwxrwx
root    
root          
31 2008-11-10 04:28 foo.gz

#exit

$adb pull /dev/sample/foo.gz d:/foo.gz

其中,“$”是Linux或Cygwin命令列的提示符,#是Android命令列的提示符。

最後,用 ls -l 命令查看目前的目錄,會發現一個 foo.gz的檔案,這個檔案就是example產生的,退出adb
shell,用命令: adb pull /dev/sample/foo.gz d:/foo.gz 可將模擬器上產生的檔案 foo.gz
pull至D盤根目錄,用winrar或7zip可查看這個檔案的內容,正是代碼中所寫的“hello, hello! ”!

 

至此,zlib移植Android平台運行成功!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.