41.linux下安装zlib库:测试zlib的压缩数据功能

一:zlib简介:

zlib是提供数据压缩用的函式库,由Jean-loup Gailly与Mark Adler所开发。

二:编译:

sudo tar -zxvf zlib-1.2.11.tar.gz -C .
sudo chown -R aston zlib-1.2.11
mkdir install_lib
sudo ./configure --prefix=/home/aston/huawei/zlib-1.2.11/install_lib
sudo make
sudo make install

三:测试:

1.代码:

#include <stdio.h>
#include <zlib.h>
 
int main(int argc,char **args)
{
    /*原始数据*/
    unsigned char strsrc[] = "这些是测试数据。123456789 abcdefghigklmnopqrstuvwxyz\n\t\0abcdefghijklmnopqrstuvwxyz\n"; //包含\0字符
    unsigned char buf[1024] = {0};
    unsigned char strdst[1024] = {0};
    unsigned long srclen = sizeof(strsrc);
    unsigned long buflen = sizeof(buf);
    unsigned long dstlen = sizeof(strdst);
	
    int i;
    FILE *fp = NULL;
 
    printf("源串:");
    for(i=0;i<srclen;++i)
    {
        printf("%c",strsrc[i]);
    }
    printf("原串长度为:%ld\n",srclen);
 
    printf("字符串预计算长度为:%ld\n",compressBound(srclen));
	
    //压缩
    compress(buf,&buflen,strsrc,srclen);

	printf("\nbuf start, buflen = %ld\n",buflen);
    for(i=0;i<buflen;++i)
    {
        printf("%x",buf[i]);
    }	
	printf("\nbuf end\n");
	
	
    printf("压缩后实际长度为:%ld\n",buflen);
    //解压缩
    uncompress(strdst,&dstlen,buf,buflen);
 
    printf("目的串:");
    for(i=0;i<dstlen;++i)
    {
        printf("%c",strdst[i]);
    }
 
    return 0;
}

2.编译:

g++ test_zlib.cpp -g -I./include -L./lib -lz -ldl -lpthread -lz -o app.out

打印:

aston@ubuntu:/mnt/hgfs/share/source_insight/main_135/test_zlib$ ./app.out 
源串:这些是测试数据。123456789 abcdefghigklmnopqrstuvwxyz
	abcdefghijklmnopqrstuvwxyz
原串长度为:83
字符串预计算长度为:96

buf start, buflen = 71
789cbbfae8c2ca53c7373d3a75e5d4df7d77172e36343236313533b7b054484c4a4e494d4bcfc84ccfcec9cdcb2f282c2a2e292d2bafa8ace2e264804b6661483200c76924ef
buf end
压缩后实际长度为:71
目的串:这些是测试数据。123456789 abcdefghigklmnopqrstuvwxyz
	abcdefghijklmnopqrstuvwxyz

四:交叉编译:

sudo tar -zxvf zlib-1.2.11.tar.gz -C .
sudo chown -R aston zlib-1.2.11
mkdir install_lib

sudo ./configure --prefix=/home/aston/huawei_linux/zlib-1.2.11/install_lib 

//改Makefile
CC=/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-gcc
LDSHARED=/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-gcc -shared -Wl,-soname,libz.so.1,--version-script,zlib.map
CPP=/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-gcc -E
AR=/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-ar
ARFLAGS=rc
RANLIB=/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin/arm-hisiv300-linux-ranlib

sudo make
sudo make install

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页