zlib的下载以及编译就不多说了,网上搜了一番对zlib的使用大多都是转帖的,还有几个有错误的。
所以从zlib源码提供的minizip/unzip的源码精简一番,只保留最简单的使用,没有做过多的错误检查。
miniunzip:
-
int main(argc,argv)
-
int argc;
-
char *argv[];
-
{
-
const char *zipfilename=NULL;
-
unzFile zfile=NULL;
-
unz_global_info64 zGlobalInfo;
-
unz_file_info64 zFileInfo;
-
unsigned int filenum = 256;
-
char filename[256] = {0};
-
char *filedata = NULL;
-
-
zipfilename = argv[1];
-
printf("filename:[%s] will unziped\n" ,zipfilename);
-
zfile = unzOpen64(zipfilename);
-
if(!zfile){
-
printf("unzOpen64 [%s] failed\n" ,zipfilename);
-
goto out;
-
}
-
-
if(UNZ_OK != unzGetGlobalInfo64(zfile, &zGlobalInfo)){
-
printf("unzGetGlobalInfo64 filename:[%s] failed\n" ,zipfilename);
-
goto out;
-
}
-
-
printf("zip file:[%s] have [%d] files\n" ,zipfilename , zGlobalInfo.number_entry);
-
-
if (UNZ_OK != unzGetCurrentFileInfo64(zfile, &zFileInfo, filename, filenum, NULL, 0, NULL, 0)){
-
printf("unzGetCurrentFileInfo64 filename:[%s] failed\n" ,zipfilename);
-
goto out;
-
}
-
-
printf("zip file [%s] have [%d] file is [%s]\n" ,zipfilename ,zGlobalInfo.number_entry ,filename);
-
printf("filename:[%s] org size is:[%d]KB compress size is:[%d]KB\n" ,filename ,zFileInfo.uncompressed_size/1024 ,zFileInfo.compressed_size/1024);
-
-
if (UNZ_OK != unzOpenCurrentFile(zfile)){
-
printf("unzOpenCurrentFile [%s] failed\n" ,filename);
-
goto out;
-
}
-
filedata = (char *)malloc(zFileInfo.uncompressed_size);
-
if(!filedata){
-
printf("malloc file umcompressed failed\n");
-
goto out;
-
}
-
memset(filedata ,0x00 ,zFileInfo.uncompressed_size);
-
-
filenum = unzReadCurrentFile(zfile ,filedata ,zFileInfo.uncompressed_size);
-
printf("read file:[%s] %d buffer\n" ,filename ,filenum);
-
unzCloseCurrentFile(zfile);
-
-
int fd;
-
fd = open("Zsensor.exe" ,O_CREAT|O_RDWR);
-
if(fd < 0){
-
perror("open failed\n");
-
goto out;
-
}
-
-
write(fd ,filedata ,zFileInfo.uncompressed_size);
-
close(fd);
-
out:
-
if(filedata){
-
free(filedata);
-
}
-
if(zfile){
-
unzClose(zfile);
-
}
-
return 0;
-
}
minizip:
-
int main(argc,argv)
-
int argc;
-
char *argv[];
-
{
-
char *zipname ,*filename ,*filedata;
-
struct stat st;
-
int fd ,err;
-
zipFile zf;
-
zip_fileinfo zi;
-
-
if(argc != 3){
-
printf("too few args\n");
-
goto out;
-
}
-
-
zipname = argv[1];
-
filename = argv[2];
-
zf = zipOpen64(zipname ,0);
-
if (zf == NULL){
-
printf("zipOpen64 failed\n");
-
goto out;
-
}else{
-
printf("creating %s\n",zipname);
-
}
-
-
zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
-
zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
-
zi.dosDate = 0;
-
zi.internal_fa = 0;
-
zi.external_fa = 0;
-
-
//filename is the name of the compressed file
-
err = zipOpenNewFileInZip(zf,filename,&zi,
-
NULL,0,NULL,0,NULL /* comment*/,
-
Z_DEFLATED,
-
Z_DEFAULT_COMPRESSION);
-
if (err != ZIP_OK){
-
printf("zipOpenNewFileInZip zip file:[%s] failed\n" ,zipname);
-
goto out;
-
}
-
fd = open(filename ,O_RDONLY);
-
if(fd < 0){
-
perror("open failed\n");
-
goto out;
-
}
-
fstat(fd ,&st);
-
filedata = (char *)malloc(st.st_size);
-
if(!filedata){
-
perror("malloc failed\n");
-
goto out;
-
}
-
memset(filedata ,0x00 ,st.st_size);
-
-
read(fd ,filedata ,st.st_size);
-
close(fd);
-
-
err = zipWriteInFileInZip (zf ,filedata ,st.st_size);
-
if (err < 0){
-
printf("error in writing %s in the zipfile\n",
-
filename);
-
goto out;
-
}
-
out:
-
if(filedata){
-
free(filedata);
-
}
-
if(zf){
-
zipCloseFileInZip(zf);
-
}
-
if(zf){
-
zipClose(zf,NULL);
-
}
-
return 0;
-
}
阅读(3539) | 评论(0) | 转发(0) |