Chinaunix首页 | 论坛 | 博客
  • 博客访问: 144021
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 218
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:02
文章分类
文章存档

2017年(1)

2016年(20)

我的朋友

分类: C/C++

2017-08-22 11:30:06

zlib的下载以及编译就不多说了,网上搜了一番对zlib的使用大多都是转帖的,还有几个有错误的。
所以从zlib源码提供的minizip/unzip的源码精简一番,只保留最简单的使用,没有做过多的错误检查。
miniunzip:

点击(此处)折叠或打开

  1. int main(argc,argv)
  2.     int argc;
  3.     char *argv[];
  4. {
  5.     const char *zipfilename=NULL;
  6.     unzFile zfile=NULL;
  7.     unz_global_info64 zGlobalInfo;
  8.     unz_file_info64 zFileInfo;
  9.     unsigned int filenum = 256;
  10.     char filename[256] = {0};
  11.     char *filedata = NULL;

  12.     zipfilename = argv[1];
  13.     printf("filename:[%s] will unziped\n" ,zipfilename);
  14.     zfile = unzOpen64(zipfilename);
  15.     if(!zfile){
  16.         printf("unzOpen64 [%s] failed\n" ,zipfilename);
  17.         goto out;
  18.     }

  19.     if(UNZ_OK != unzGetGlobalInfo64(zfile, &zGlobalInfo)){
  20.         printf("unzGetGlobalInfo64 filename:[%s] failed\n" ,zipfilename);
  21.         goto out;
  22.     }

  23.     printf("zip file:[%s] have [%d] files\n" ,zipfilename , zGlobalInfo.number_entry);

  24.     if (UNZ_OK != unzGetCurrentFileInfo64(zfile, &zFileInfo, filename, filenum, NULL, 0, NULL, 0)){
  25.         printf("unzGetCurrentFileInfo64 filename:[%s] failed\n" ,zipfilename);
  26.         goto out;
  27.     }

  28.     printf("zip file [%s] have [%d] file is [%s]\n" ,zipfilename ,zGlobalInfo.number_entry ,filename);
  29.     printf("filename:[%s] org size is:[%d]KB compress size is:[%d]KB\n" ,filename ,zFileInfo.uncompressed_size/1024 ,zFileInfo.compressed_size/1024);

  30.     if (UNZ_OK != unzOpenCurrentFile(zfile)){
  31.         printf("unzOpenCurrentFile [%s] failed\n" ,filename);
  32.         goto out;
  33.     }
  34.     filedata = (char *)malloc(zFileInfo.uncompressed_size);
  35.     if(!filedata){
  36.         printf("malloc file umcompressed failed\n");
  37.         goto out;
  38.     }
  39.     memset(filedata ,0x00 ,zFileInfo.uncompressed_size);

  40.     filenum = unzReadCurrentFile(zfile ,filedata ,zFileInfo.uncompressed_size);
  41.     printf("read file:[%s] %d buffer\n" ,filename ,filenum);
  42.     unzCloseCurrentFile(zfile);

  43.     int fd;
  44.     fd = open("Zsensor.exe" ,O_CREAT|O_RDWR);
  45.     if(fd < 0){
  46.         perror("open failed\n");
  47.         goto out;
  48.     }

  49.     write(fd ,filedata ,zFileInfo.uncompressed_size);
  50.     close(fd);
  51. out:
  52.         if(filedata){
  53.             free(filedata);
  54.         }
  55.     if(zfile){
  56.         unzClose(zfile);
  57.     }
  58.     return 0;
  59. }

minizip:

点击(此处)折叠或打开

  1. int main(argc,argv)
  2.     int argc;
  3.     char *argv[];
  4. {
  5.     char *zipname ,*filename ,*filedata;
  6.     struct stat st;
  7.     int fd ,err;
  8.     zipFile zf;
  9.     zip_fileinfo zi;

  10.     if(argc != 3){
  11.         printf("too few args\n");
  12.         goto out;
  13.     }

  14.     zipname = argv[1];
  15.     filename = argv[2];
  16.     zf = zipOpen64(zipname ,0);
  17.     if (zf == NULL){
  18.         printf("zipOpen64 failed\n");
  19.         goto out;
  20.     }else{
  21.         printf("creating %s\n",zipname);
  22.     }

  23.     zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
  24.         zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
  25.     zi.dosDate = 0;
  26.     zi.internal_fa = 0;
  27.     zi.external_fa = 0;

  28.     //filename is the name of the compressed file
  29.     err = zipOpenNewFileInZip(zf,filename,&zi,
  30.             NULL,0,NULL,0,NULL /* comment*/,
  31.             Z_DEFLATED,
  32.             Z_DEFAULT_COMPRESSION);
  33.     if (err != ZIP_OK){
  34.         printf("zipOpenNewFileInZip zip file:[%s] failed\n" ,zipname);
  35.         goto out;
  36.     }
  37.     fd = open(filename ,O_RDONLY);
  38.     if(fd < 0){
  39.         perror("open failed\n");
  40.         goto out;
  41.     }
  42.     fstat(fd ,&st);
  43.     filedata = (char *)malloc(st.st_size);
  44.     if(!filedata){
  45.         perror("malloc failed\n");
  46.         goto out;
  47.     }
  48.     memset(filedata ,0x00 ,st.st_size);

  49.     read(fd ,filedata ,st.st_size);
  50.     close(fd);

  51.     err = zipWriteInFileInZip (zf ,filedata ,st.st_size);
  52.     if (err < 0){
  53.         printf("error in writing %s in the zipfile\n",
  54.                 filename);
  55.         goto out;
  56.     }
  57. out:
  58.     if(filedata){
  59.         free(filedata);
  60.     }
  61.     if(zf){
  62.         zipCloseFileInZip(zf);
  63.     }
  64.     if(zf){
  65.         zipClose(zf,NULL);
  66.     }
  67.     return 0;
  68. }



阅读(3539) | 评论(0) | 转发(0) |
0

上一篇:[计算机的那些事]算法-KMP简单解析

下一篇:没有了

给主人留下些什么吧!~~