Zlib库的使用实现对zip文件的解压缩

来源:互联网 发布:照相机内存卡数据恢复 编辑:程序博客网 时间:2024/05/22 00:45

实习项目中涉及到获取GigEVision设备的XML设备描述文件

A GigE Vision device MUST have an XML device description with the syntax described

in the GenApi module of the GenICam standard.

XML文件有两种形式:压缩的(.zip)XML文件,和未压缩的(.xml)XML文件

对应两种url描述: 

1)Local:MER-200-14Gx_V1.0.29.zip;40ba0000;c350      

2)Local:eco655MVGE_r2_v1.6.0_b2545.xml;10000;271DC  

对于未压缩的.xml文件,直接读取就行;对于压缩的.zip文件,需要进行解压缩

于是去了解了zip文件的压缩原理,推荐文章:http://blog.csdn.net/ljh0302/article/details/50381470

发现zip的压缩原理还是比较复杂的,要手动去写一个zip的解压缩程序还是很费力,于是考虑用已有的解压缩的库zlib来实现。

一、下载并配置zlib

系统Win7 64位

官网下载编译好的DLL:zlib128-dll.zip,官网地址http://www.zlib.net/ 选择如下的版本

zlib compiled DLL,version 1.2.8, zipfile format (139K, MD5 checksum 42eccc2af5bac6b7a1188d7817d03549):

同时我上传了一份资源,地址:http://download.csdn.net/detail/ljh0302/9373083

在VS2010中使用zlib的静态库

将zlib128-dll/include 中的zconf.h 和 zlib.h加入工程的目录下,并且添加作为头文件

将zlib128-dll/lib中的zdll.lib复制到工程的资源目录

将zlib-128-dll/zlib1.dll放入C:\Windows\SysWOW64(因为系统是64位,32位则放入C:\\Windows\\System32)

配置文件项目-属性-链接器-输入,在附加依赖项中加入zdll.lib,在忽略特定默认库中加入MSVCRT


最后在使用的时候include头文件即可

#include "zconf.h"

#include "zlib.h"

#pragma comment(lib,"zdll.lib")

注意:

这里的项目必须是win32的,因为编译好的zlib dll是32位的,而且官网上好像只提供了这一种,换成x64是不可行的

二、如果希望x64的工程能够使用zlib呢?

下载zlib128.zip自己进行编译,我上传一份资源,地址:http://download.csdn.net/detail/ljh0302/9373381

如何编译参考文章:http://blog.sina.com.cn/s/blog_6e0693f70100sjgj.html

不过进行的编译的时候会报错LINK : fatal error LNK1104: 无法打开文件“x64\ZlibDllDebug\zlibwapi.lib”

虽然最后只用静态库是可以,但是一直不清楚这个错误是为什么,求教知道的同行

而我只使用了zconf.h zlib.h 和zlibstat.lib三个文件,利用静态库,配置很简单,如下


#include "zconf.h"

#include "zlib.h"

即可

三、zlib库函数的使用

1、int compress(Bytef *dest, uLongf *destLen, const Bytef* source, uLong sourceLen);

compress函数将source缓冲区的内容压缩到dest压缩区。sourceLen表示source缓冲区的大小(以字节计)。

destLen是传址调用,当调用函数的时候,destLen表示dest缓冲区的大小 destLen>(sourceLen + 12)*100.1%

或者使用compressBound(sourceLen),当函数退出,destLen表示压缩后缓冲区的实际大小

compress 若成功,返回Z_OK,若没有足够内存,返回Z_MEM_ERROR,若缓冲区不够大,则返回Z_BUF_ERROR


2、int uncompress(Bytef *dest ,uLongf *destLen, const Bytef*source, uLong sourceLen);

uncompress函数将source缓冲区的内容解压缩到dest缓冲区。sourceLen是source缓冲区的大小,destLen是传址调用,dest缓冲区必须足以容下解压后的数据,函数退出后,destLen是解压后的数据的实际大小

uncompress若成功,则返回Z_OK,若没有足够内存,则返回Z_MEM_ERROR,若输出缓冲区不够,则Z_BUF_ERROR,若输入数据有误,则返回Z_DATA_ERROR


#include "zconf.h"#include "zlib.h"#include <iostream>using namespace std;#pragma comment(lib,"zdll.lib")int main(){int err;Byte compr[200],uncompr[200];uLong comprLen,uncomprLen;const char* hello = "1213135454646544665456465465457877874655312333131";uLong len = strlen(hello)+1;comprLen = sizeof(compr)/sizeof(compr[0]);err = compress(compr,&comprLen,(const Bytef*)hello,len);if(err != Z_OK){cerr<<"compress error: "<<err<<endl;exit(1);}cout<<"original size: "<<len<<" ,compressed size: "<<comprLen<<endl;strcpy((char*)uncompr,"garbage");err = uncompress(uncompr,&uncomprLen,compr,comprLen);if(err != Z_OK){cerr<<"uncompress error: "<<err<<endl;exit(1);}else{cout<<"uncompress() succeed: "<<endl;cout<<(char*)uncompr<<endl;}return 0;}
结果:



0 0
原创粉丝点击