zlib、minizip的安装和使用
1.zlib安装和测试
git clone https://github.com/madler/zlib.git
cd zlib
./configure --prefix=xxx # 设置安装路径为xxx
make test # 编译
make install # 将头文件和库文件安装到/home/myname/softwares/zlib中
测试代码test.cpp(参考:链接):
#include "zconf.h"
#include "zlib.h"
#include <iostream>
#include <cstring>
using namespace std;
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;
}
编译运行:
g++ test.cpp -o test -I xxx/include/ -L xxx/lib/ -lz
2.minizip安装和测试
直接使用zlib似乎有些困难,minizip封装了zlib,在zlib项目的contrib/minizip中。
cd contrib/minizip
make
编译生成的ioapi.o、zip.o和unzip.o,是我们使用minizip所需的。我将ioapi.o、zip.o和unzip.o改名为libioapi.a、libzip.a和libunzip.a,并放入xxx/lib。将contrib/minizip下所有.h文件放入xxx/include。使用如下编译命令链接minizip所需的库文件:
编译命令:
g++ test.cpp -o test -I xxx/include/ -L xxx/lib/ -l ioapi -l unzip -l zip -lz
-lz
要放在最后面,这样ioapi、unzip、zip才可以找到zlib,如果不放在最后面编译的时候会出现很多变量未定义的情况。
测试程序test.cpp(参考:链接):
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "unzip.h"
#define dir_delimter '/'
#define MAX_FILENAME 512
#define READ_SIZE 8192
int main( int argc, char **argv )
{
if ( argc < 2 )
{
printf( "usage:\n%s {file to unzip}\n", argv[ 0 ] );
return -1;
}
// Open the zip file
unzFile zipfile = unzOpen64( argv[ 1 ] );
if ( zipfile == NULL )
{
printf( "%s: not found\n" );
return -1;
}
// Get info about the zip file
unz_global_info global_info;
if ( unzGetGlobalInfo( zipfile, &global_info ) != UNZ_OK )
{
printf( "could not read file global info\n" );
unzClose( zipfile );
return -1;
}
// Buffer to hold data read from the zip file.
char read_buffer[ READ_SIZE ];
// Loop to extract all files
uLong i;
for ( i = 0; i < global_info.number_entry; ++i )
{
// Get info about current file.
unz_file_info file_info;
char filename[ MAX_FILENAME ];
if ( unzGetCurrentFileInfo(
zipfile,
&file_info,
filename,
MAX_FILENAME,
NULL, 0, NULL, 0 ) != UNZ_OK )
{
printf( "could not read file info\n" );
unzClose( zipfile );
return -1;
}
// Check if this entry is a directory or file.
const size_t filename_length = strlen( filename );
if ( filename[ filename_length-1 ] == dir_delimter )
{
// Entry is a directory, so create it.
printf( "dir:%s\n", filename );
mkdir( filename ,0755);
}
else
{
// Entry is a file, so extract it.
printf( "file:%s\n", filename );
if ( unzOpenCurrentFile( zipfile ) != UNZ_OK )
{
printf( "could not open file\n" );
unzClose( zipfile );
return -1;
}
// Open a file to write out the data.
FILE *out = fopen( filename, "wb" );
if ( out == NULL )
{
printf( "could not open destination file\n" );
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return -1;
}
int error = UNZ_OK;
do
{
error = unzReadCurrentFile( zipfile, read_buffer, READ_SIZE );
if ( error < 0 )
{
printf( "error %d\n", error );
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return -1;
}
// Write data to file.
if ( error > 0 )
{
fwrite( read_buffer, error, 1, out ); // You should check return of fwrite...
}
} while ( error > 0 );
fclose( out );
}
unzCloseCurrentFile( zipfile );
// Go the the next entry listed in the zip file.
if ( ( i+1 ) < global_info.number_entry )
{
if ( unzGoToNextFile( zipfile ) != UNZ_OK )
{
printf( "cound not read next file\n" );
unzClose( zipfile );
return -1;
}
}
}
unzClose( zipfile );
return 0;
}
编译和运行:
g++ test.cpp -o test -I xxx/include/ -L xxx/lib/ -l ioapi -l unzip -l zip -lz
./test hello.zip