一、下载
官网下载地址:http://zlib.net
二、编译
1. 解压到目录,如d:/zlib
2. 开启VS2010的Command Prompt (32bit)
3. 进入到zlib-1.2.8/contrib/masmx86目录
4. 执行bld_ml32.bat
5. 打开工程项目zlib-1.2.8/contrib/vstudio/vc10/zlibvc.sln
6. 编译整个solution(Debug, Release)
三、VS2010使用zlib
1.复制出需要的文件,debug和release版本的zlib.h zconf.h zlibstat.lib zlibwapid.dll zlibwapid.lib
四、遇到的问题
vc10创建win32工程,在]win32下调用zlib库的函数compress编译错误(error LNK2019: 无法解析的外部符号 _compress,该符号在函数 _wmain 中被引用),
首先要在Zlib.h头文件添加上ZLIB_WINAPI的宏定义(只有在vs2010的win32项目时候奥);
然后再 项目->属性->链接器->输入->忽略特定的默认库,把libcmt.lib,加上就ok了。
#if _MSC_VER==1600
#ifdef WINDOWS32 //win32项目
#define ZLIB_WINAPI
#endif
#ifdef _CONSOLE //控制台项目
#define ZLIB_WINAPI
#endif
#endif
#include "..\Zlib\include\zlib.h"
#include "..\Zlib\include\zip.h"
#include "..\Zlib\include\unzip.h"
#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"..\\Zlib\\lib\\Debug\\zlibstat.lib")
int main (void)
{
BYTE strSrc[64]={"嘿嘿呵呵1234567"};
BYTE strDest1[1024]={0};
BYTE strDest2[1024]={0};
DWORD nstrDest=sizeof(strDest1);
//压缩数据
compress(strDest1,&nstrDest,strSrc,sizeof(strSrc));
printf("%s\n",strDest1);
//解压数据
uncompress(strDest2,&nstrDest,strDest1,nstrDest);
printf("%s\n",strDest2);
///////////////////////////////////压缩文件///////////////////////////////////////////////////
//创建文件
zipFile hFile=zipOpen64("c:\\123.zip",APPEND_STATUS_CREATE);
if (hFile==NULL)
{
printf("文件创建失败\n");
return 0;
}
zip_fileinfo zi={0};
zipOpenNewFileInZip(hFile,"jjj/",&zi,NULL,0,NULL,0,NULL,Z_DEFLATED,Z_BEST_COMPRESSION);
zipOpenNewFileInZip(hFile,"foo/111.txt",&zi,NULL,0,NULL,0,NULL,Z_DEFLATED,Z_BEST_COMPRESSION);
//写入数据
zipWriteInFileInZip(hFile,"111111",strlen("111111"));
//加密码
////zlib-1.2.8\contrib\minizip\unzip.c 注释掉#define NOUNCRYPT
//unsigned long crcFile=0;
//int zip64 = 0;
//char strpassword[32]="123456";
//zipOpenNewFileInZip3_64(hFile,"aaa/222.txt",&zi,NULL,0,NULL,0,NULL,Z_DEFLATED,Z_BEST_COMPRESSION,0,-MAX_WBITS,DEF_MEM_LEVEL,Z_DEFAULT_STRATEGY,strpassword,crcFile,zip64);
//写入数据
zipOpenNewFileInZip(hFile,"yyy/222.txt",&zi,NULL,0,NULL,0,NULL,Z_DEFLATED,Z_BEST_COMPRESSION);
zipWriteInFileInZip(hFile,"22233344",strlen("22233344"));
//关闭文件
zipClose(hFile,NULL);
///////////////////////////////////////////////////////////////////////////////////////
//getchar();
//getchar();
///////////////////////////////////解压文件///////////////////////////////////////////////////
//打开文件
unzFile hUnFile=unzOpen64("c:\\123.zip");
if (hUnFile==NULL)
{
printf("文件打开失败\n");
return 0;
}
//得到压缩包信息
unz_global_info64 pglobal_info={0};
unzGetGlobalInfo64(hUnFile,&pglobal_info);
//得到第一个文件
int err=unzGoToFirstFile(hUnFile);
//分配内存
int size_buf = 8192;
void* buf = (void*)malloc(size_buf);
if (buf==NULL)
{
printf("Error allocating memory\n");
return UNZ_INTERNALERROR;
}
memset(buf,0,size_buf);
//依次循环枚举文件
while (err == UNZ_OK)
{
//获取当前文件名
char szCurrentFileName[64]={0};
unz_file_info64 file_info;
err = unzGetCurrentFileInfo64(hUnFile,&file_info,szCurrentFileName,sizeof(szCurrentFileName)-1,NULL,0,NULL,0);
//判断成功
if (err == UNZ_OK)
{
//比较文件名
//if (unzStringFileNameCompare(szCurrentFileName,
// szFileName,iCaseSensitivity)==0)
// return UNZ_OK;
//打开当前文件
unzOpenCurrentFile(hUnFile);
//unzOpenCurrentFilePassword(hUnFile,"123456");
//读取内容
err=unzReadCurrentFile(hUnFile,buf,size_buf);
if (err>0)
{
printf("%s\n",buf);
}
//关闭当前文件
unzCloseCurrentFile(hUnFile);
//指向下一个文件
err = unzGoToNextFile(hUnFile);
}
}
free(buf);
//关闭文件
unzClose(hUnFile);
///////////////////////////////////////////////////////////////////////////////////////
getchar();
getchar();
return 0;
}
|