利用zlib库进行zip解压

1:到zlib官网上下载zlib,本文下载的是1.2.8的版本。

2:进行./configure,然后make。

3:进入zlib库中的contrib/minizip/路径下make,生成的minizip是进行压缩,miniunz是进行解压zip文件。

下面讲解一下miniunz.c中的代码:

  1 /*
  2    miniunz.c
  3    Version 1.1, February 14h, 2010
  4    sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
  5 
  6          Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
  7 
  8          Modifications of Unzip for Zip64
  9          Copyright (C) 2007-2008 Even Rouault
 10 
 11          Modifications for Zip64 support on both zip and unzip
 12          Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
 13 */
 14 
 15 #if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
 16         #ifndef __USE_FILE_OFFSET64
 17                 #define __USE_FILE_OFFSET64
 18         #endif
 19         #ifndef __USE_LARGEFILE64
 20                 #define __USE_LARGEFILE64
 21         #endif
 22         #ifndef _LARGEFILE64_SOURCE
 23                 #define _LARGEFILE64_SOURCE
 24         #endif
 25         #ifndef _FILE_OFFSET_BIT
 26                 #define _FILE_OFFSET_BIT 64
 27         #endif
 28 #endif
 29 
 30 #ifdef __APPLE__
 31 // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
 32 #define FOPEN_FUNC(filename, mode) fopen(filename, mode)
 33 #define FTELLO_FUNC(stream) ftello(stream)
 34 #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
 35 #else
 36 #define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
 37 #define FTELLO_FUNC(stream) ftello64(stream)
 38 #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
 39 #endif
 40 
 41 
 42 #include <stdio.h>
 43 #include <stdlib.h>
 44 #include <string.h>
 45 #include <time.h>
 46 #include <errno.h>
 47 #include <fcntl.h>
 48 
 49 #ifdef _WIN32
 50 # include <direct.h>
 51 # include <io.h>
 52 #else
 53 # include <unistd.h>
 54 # include <utime.h>
 55 #endif
 56 
 57 
 58 #include "unzip.h"
 59 
 60 #define CASESENSITIVITY (0)
 61 #define WRITEBUFFERSIZE (8192)
 62 #define MAXFILENAME (256)
 63 
 64 #ifdef _WIN32
 65 #define USEWIN32IOAPI
 66 #include "iowin32.h"
 67 #endif
 68 /*
 69   mini unzip, demo of unzip package
 70 
 71   usage :
 72   Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
 73 
 74   list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
 75     if it exists
 76 */
 77 
 78 
 79 /* change_file_date : change the date/time of a file
 80     filename : the filename of the file where date/time must be modified
 81     dosdate : the new date at the MSDos format (4 bytes)
 82     tmu_date : the SAME new date at the tm_unz format */
 83 void change_file_date(filename,dosdate,tmu_date)
 84     const char *filename;
 85     uLong dosdate;
 86     tm_unz tmu_date;
 87 {
 88 #ifdef _WIN32
 89   HANDLE hFile;
 90   FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
 91 
 92   hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
 93                       0,NULL,OPEN_EXISTING,0,NULL);
 94   GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
 95   DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
 96   LocalFileTimeToFileTime(&ftLocal,&ftm);
 97   SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
 98   CloseHandle(hFile);
 99 #else
100 #ifdef unix || __APPLE__
101   struct utimbuf ut;
102   struct tm newdate;
103   newdate.tm_sec = tmu_date.tm_sec;
104   newdate.tm_min=tmu_date.tm_min;
105   newdate.tm_hour=tmu_date.tm_hour;
106   newdate.tm_mday=tmu_date.tm_mday;
107   newdate.tm_mon=tmu_date.tm_mon;
108   if (tmu_date.tm_year > 1900)
109       newdate.tm_year=tmu_date.tm_year - 1900;
110   else
111       newdate.tm_year=tmu_date.tm_year ;
112   newdate.tm_isdst=-1;
113 
114   ut.actime=ut.modtime=mktime(&newdate);
115   utime(filename,&ut);
116 #endif
117 #endif
118 }
119 
120 
121 /* mymkdir and change_file_date are not 100 % portable
122    As I don't know well Unix, I wait feedback for the unix portion */
123 
124 int mymkdir(dirname)
125     const char* dirname;
126 {
127     int ret=0;
128 #ifdef _WIN32
129     ret = _mkdir(dirname);
130 #elif unix
131     ret = mkdir (dirname,0775);
132 #elif __APPLE__
133     ret = mkdir (dirname,0775);
134 #endif
135     return ret;
136 }
137 
138 int makedir (newdir)
139     char *newdir;
140 {
141   char *buffer ;
142   char *p;
143   int  len = (int)strlen(newdir);
144 
145   if (len <= 0)
146     return 0;
147 
148   buffer = (char*)malloc(len+1);
149         if (buffer==NULL)
150         {
151                 printf("Error allocating memory
");
152                 return UNZ_INTERNALERROR;
153         }
154   strcpy(buffer,newdir);
155 
156   if (buffer[len-1] == '/') {
157     buffer[len-1] = '';
158   }
159   if (mymkdir(buffer) == 0)
160     {
161       free(buffer);
162       return 1;
163     }
164 
165   p = buffer+1;
166   while (1)
167     {
168       char hold;
169 
170       while(*p && *p != '\' && *p != '/')
171         p++;
172       hold = *p;
173       *p = 0;
174       if ((mymkdir(buffer) == -1) && (errno == ENOENT))
175         {
176           printf("couldn't create directory %s
",buffer);
177           free(buffer);
178           return 0;
179         }
180       if (hold == 0)
181         break;
182       *p++ = hold;
183     }
184   free(buffer);
185   return 1;
186 }
187 
188 void do_banner()
189 {
190     printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant
");
191     printf("more info at http://www.winimage.com/zLibDll/unzip.html

");
192 }
193 
194 void do_help()
195 {
196     printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]

" 
197            "  -e  Extract without pathname (junk paths)
" 
198            "  -x  Extract with pathname
" 
199            "  -v  list files
" 
200            "  -l  list files
" 
201            "  -d  directory to extract into
" 
202            "  -o  overwrite files without prompting
" 
203            "  -p  extract crypted file using password

");
204 }
205 
206 void Display64BitsSize(ZPOS64_T n, int size_char)
207 {
208   /* to avoid compatibility problem , we do here the conversion */
209   char number[21];
210   int offset=19;
211   int pos_string = 19;
212   number[20]=0;
213   for (;;) {
214       number[offset]=(char)((n%10)+'0');
215       if (number[offset] != '0')
216           pos_string=offset;
217       n/=10;
218       if (offset==0)
219           break;
220       offset--;
221   }
222   {
223       int size_display_string = 19-pos_string;
224       while (size_char > size_display_string)
225       {
226           size_char--;
227           printf(" ");
228       }
229   }
230 
231   printf("%s",&number[pos_string]);
232 }
233 
234 int do_list(uf)
235     unzFile uf;
236 {
237     uLong i;
238     unz_global_info64 gi;
239     int err;
240 
241     err = unzGetGlobalInfo64(uf,&gi);
242     if (err!=UNZ_OK)
243         printf("error %d with zipfile in unzGetGlobalInfo 
",err);
244     printf("  Length  Method     Size Ratio   Date    Time   CRC-32     Name
");
245     printf("  ------  ------     ---- -----   ----    ----   ------     ----
");
246     for (i=0;i<gi.number_entry;i++)
247     {
248         char filename_inzip[256];
249         unz_file_info64 file_info;
250         uLong ratio=0;
251         const char *string_method;
252         char charCrypt=' ';
253         err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
254         if (err!=UNZ_OK)
255         {
256             printf("error %d with zipfile in unzGetCurrentFileInfo
",err);
257             break;
258         }
259         if (file_info.uncompressed_size>0)
260             ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
261 
262         /* display a '*' if the file is crypted */
263         if ((file_info.flag & 1) != 0)
264             charCrypt='*';
265 
266         if (file_info.compression_method==0)
267             string_method="Stored";
268         else
269         if (file_info.compression_method==Z_DEFLATED)
270         {
271             uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
272             if (iLevel==0)
273               string_method="Defl:N";
274             else if (iLevel==1)
275               string_method="Defl:X";
276             else if ((iLevel==2) || (iLevel==3))
277               string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
278         }
279         else
280         if (file_info.compression_method==Z_BZIP2ED)
281         {
282               string_method="BZip2 ";
283         }
284         else
285             string_method="Unkn. ";
286 
287         Display64BitsSize(file_info.uncompressed_size,7);
288         printf("  %6s%c",string_method,charCrypt);
289         Display64BitsSize(file_info.compressed_size,7);
290         printf(" %3lu%%  %2.2lu-%2.2lu-%2.2lu  %2.2lu:%2.2lu  %8.8lx   %s
",
291                 ratio,
292                 (uLong)file_info.tmu_date.tm_mon + 1,
293                 (uLong)file_info.tmu_date.tm_mday,
294                 (uLong)file_info.tmu_date.tm_year % 100,
295                 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
296                 (uLong)file_info.crc,filename_inzip);
297         if ((i+1)<gi.number_entry)
298         {
299             err = unzGoToNextFile(uf);
300             if (err!=UNZ_OK)
301             {
302                 printf("error %d with zipfile in unzGoToNextFile
",err);
303                 break;
304             }
305         }
306     }
307 
308     return 0;
309 }
310 
311 
312 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
313     unzFile uf;
314     const int* popt_extract_without_path;
315     int* popt_overwrite;
316     const char* password;
317 {
318     char filename_inzip[256];
319     char* filename_withoutpath;
320     char* p;
321     int err=UNZ_OK;
322     FILE *fout=NULL;
323     void* buf;
324     uInt size_buf;
325 
326     unz_file_info64 file_info;
327     uLong ratio=0;
328     err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
329 
330     if (err!=UNZ_OK)
331     {
332         printf("error %d with zipfile in unzGetCurrentFileInfo
",err);
333         return err;
334     }
335 
336     size_buf = WRITEBUFFERSIZE;
337     buf = (void*)malloc(size_buf);
338     if (buf==NULL)
339     {
340         printf("Error allocating memory
");
341         return UNZ_INTERNALERROR;
342     }
343 
344     p = filename_withoutpath = filename_inzip;
345     while ((*p) != '')
346     {
347         if (((*p)=='/') || ((*p)=='\'))
348             filename_withoutpath = p+1;
349         p++;
350     }
351 
352     if ((*filename_withoutpath)=='')
353     {    // 文件夹
354         if ((*popt_extract_without_path)==0)
355         { // 创建文件夹
356             printf("creating directory: %s
",filename_inzip);
357             mymkdir(filename_inzip);
358         }
359     }
360     else
361     {
362         const char* write_filename;
363         int skip=0;
364 
365         if ((*popt_extract_without_path)==0)
366             // 带有zip包中的文件路径
367             write_filename = filename_inzip;
368         else
369             // 不带有zip包中的文件路径
370             write_filename = filename_withoutpath;
371 
372         // 通过密码提取当前文件,如果没有密码则password为null
373         err = unzOpenCurrentFilePassword(uf,password);
374         if (err!=UNZ_OK)
375         {
376             printf("error %d with zipfile in unzOpenCurrentFilePassword
",err);
377         }
378 
379         // 提取当前文件成功,如果同名文件存在是否覆盖,popt_overwrite = 1表示无条件覆盖,popt_overwrite = 0表示不覆盖
380         if (((*popt_overwrite)==0) && (err==UNZ_OK))
381         {
382             char rep=0;
383             FILE* ftestexist;
384             // 以只读方式打开文件
385             ftestexist = FOPEN_FUNC(write_filename,"rb");
386             // 如果文件存在
387             if (ftestexist!=NULL)
388             {
389                 fclose(ftestexist);
390                 do
391                 {
392                     char answer[128];
393                     int ret;
394 
395                     // 判断是否覆盖现有文件,Y是,N否,A以后都执行覆盖
396                     printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
397                     ret = scanf("%1s",answer);
398                     if (ret != 1)
399                     {
400                        exit(EXIT_FAILURE);
401                     }
402                     rep = answer[0] ;
403                     if ((rep>='a') && (rep<='z'))
404                         rep -= 0x20;
405                 }
406                 while ((rep!='Y') && (rep!='N') && (rep!='A'));
407             }
408 
409             if (rep == 'N')
410                 skip = 1;
411 
412             if (rep == 'A')
413                 *popt_overwrite=1;
414         }
415 
416         // 要覆盖现有文件
417         if ((skip==0) && (err==UNZ_OK))
418         {
419             fout=FOPEN_FUNC(write_filename,"wb");
420             /* some zipfile don't contain directory alone before file */
421             // 这里是一些zip包中的最上层的文件,这些文件不包含在任何文件夹中
422             if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
423                                 (filename_withoutpath!=(char*)filename_inzip))
424             {
425                 char c=*(filename_withoutpath-1);
426                 *(filename_withoutpath-1)='';
427                 makedir(write_filename);
428                 *(filename_withoutpath-1)=c;
429                 fout=FOPEN_FUNC(write_filename,"wb");
430             }
431 
432             if (fout==NULL)
433             {
434                 printf("error opening %s
",write_filename);
435             }
436         }
437 
438         if (fout!=NULL)
439         {
440             printf(" extracting: %s
",write_filename);
441 
442             // 循环读取当前文件,并写入硬盘
443             do
444             {
445                 err = unzReadCurrentFile(uf,buf,size_buf);
446                 if (err<0)
447                 {
448                     printf("error %d with zipfile in unzReadCurrentFile
",err);
449                     break;
450                 }
451                 if (err>0)
452                     if (fwrite(buf,err,1,fout)!=1)
453                     {
454                         printf("error in writing extracted file
");
455                         err=UNZ_ERRNO;
456                         break;
457                     }
458             }
459             while (err>0);
460             if (fout)
461                     fclose(fout);
462             
463              // 修改文件的时间
464             if (err==0)
465                 change_file_date(write_filename,file_info.dosDate,
466                                  file_info.tmu_date);
467         }
468 
469         if (err==UNZ_OK)
470         {
471             err = unzCloseCurrentFile (uf);
472             if (err!=UNZ_OK)
473             {
474                 printf("error %d with zipfile in unzCloseCurrentFile
",err);
475             }
476         }
477         else
478             unzCloseCurrentFile(uf); /* don't lose the error */
479     }
480 
481     free(buf);
482     return err;
483 }
484 
485 
486 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
487     unzFile uf;
488     int opt_extract_without_path;
489     int opt_overwrite;
490     const char* password;
491 {
492     uLong i;
493     unz_global_info64 gi;
494     int err;
495     FILE* fout=NULL;
496 
497     err = unzGetGlobalInfo64(uf,&gi);
498     if (err!=UNZ_OK)
499         printf("error %d with zipfile in unzGetGlobalInfo 
",err);
500 
501     for (i=0;i<gi.number_entry;i++)
502     {
503         if (do_extract_currentfile(uf,&opt_extract_without_path,
504                                       &opt_overwrite,
505                                       password) != UNZ_OK)
506             break;
507 
508         if ((i+1)<gi.number_entry)
509         {
510             err = unzGoToNextFile(uf);
511             if (err!=UNZ_OK)
512             {
513                 printf("error %d with zipfile in unzGoToNextFile
",err);
514                 break;
515             }
516         }
517     }
518 
519     return 0;
520 }
521 
522 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
523     unzFile uf;
524     const char* filename;
525     int opt_extract_without_path;
526     int opt_overwrite;
527     const char* password;
528 {
529     int err = UNZ_OK;
530     if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
531     {
532         printf("file %s not found in the zipfile
",filename);
533         return 2;
534     }
535 
536     if (do_extract_currentfile(uf,&opt_extract_without_path,
537                                       &opt_overwrite,
538                                       password) == UNZ_OK)
539         return 0;
540     else
541         return 1;
542 }
543 
544 
545 int main(argc,argv)
546     int argc;
547     char *argv[];
548 {
549     const char *zipfilename=NULL;    // zip包名
550     const char *filename_to_extract=NULL;    // 指定文件名,然后在zip包中查找这个指定的文件
551     const char *password=NULL;    // zip包的密码
552     char filename_try[MAXFILENAME+16] = "";    // 当zip包名无效时,则会在zip包名后加上 .zip 标志存放在此变量中,然后深度操作此名
553     int i;
554     int ret_value=0;    // 返回值
555     int opt_do_list=0;    // 此标志表示是否只列出zip包中的文件列表,不解压zip包
556     int opt_do_extract=1;    // 此标志表示是否解压zip包
557     int opt_do_extract_withoutpath=0;    // 此标志表示解压zip包时,是否需要建立zip包中相应的目录路径
558     int opt_overwrite=0;    // 此标志表示解压zip包中的文件时,硬盘上已经存在此文件时,是否进行文件覆盖
559     int opt_extractdir=0;    // 此标志表示是否解压到指定的目录
560     const char *dirname=NULL;
561     unzFile uf=NULL;
562 
563     do_banner();
564     // 如果运行时提供的参数不对,将显示帮助
565     if (argc==1)
566     {
567         do_help();
568         return 0;
569     }
570     else
571     {
572         // 提取运行时输入的参数
573         for (i=1;i<argc;i++)
574         {
575             if ((*argv[i])=='-')
576             {
577                 const char *p=argv[i]+1;
578 
579                 while ((*p)!='')
580                 {
581                     char c=*(p++);
582                     if ((c=='l') || (c=='L'))
583                         opt_do_list = 1;
584                     if ((c=='v') || (c=='V'))
585                         opt_do_list = 1;
586                     if ((c=='x') || (c=='X'))
587                         opt_do_extract = 1;
588                     if ((c=='e') || (c=='E'))
589                         opt_do_extract = opt_do_extract_withoutpath = 1;
590                     if ((c=='o') || (c=='O'))
591                         opt_overwrite=1;
592                     if ((c=='d') || (c=='D'))
593                     {
594                         opt_extractdir=1;
595                         dirname=argv[i+1];
596                     }
597 
598                     if (((c=='p') || (c=='P')) && (i+1<argc))
599                     {
600                         password=argv[i+1];
601                         i++;
602                     }
603                 }
604             }
605             else
606             {
607                 // 提取zip包名
608                 if (zipfilename == NULL)
609                     zipfilename = argv[i];
610                 // 指定解压后的目录路径
611                 else if ((filename_to_extract==NULL) && (!opt_extractdir))
612                         filename_to_extract = argv[i] ;
613             }
614         }
615     }
616 
617     // 打开zip包
618     if (zipfilename!=NULL)
619     {
620 
621 #        ifdef USEWIN32IOAPI
622         zlib_filefunc64_def ffunc;
623 #        endif
624 
625         strncpy(filename_try, zipfilename,MAXFILENAME-1);
626         /* strncpy doesnt append the trailing NULL, of the string is too long. */
627         filename_try[ MAXFILENAME ] = '';
628 
629 #        ifdef USEWIN32IOAPI
630         fill_win32_filefunc64A(&ffunc);
631         uf = unzOpen2_64(zipfilename,&ffunc);
632 #        else
633         uf = unzOpen64(zipfilename);
634 #        endif
635         if (uf==NULL)
636         {
637             strcat(filename_try,".zip");
638 #            ifdef USEWIN32IOAPI
639             uf = unzOpen2_64(filename_try,&ffunc);
640 #            else
641             uf = unzOpen64(filename_try);
642 #            endif
643         }
644     }
645 
646     if (uf==NULL)
647     {
648         printf("Cannot open %s or %s.zip
",zipfilename,zipfilename);
649         return 1;
650     }
651     printf("%s opened
",filename_try);
652 
653     if (opt_do_list==1)
654         ret_value = do_list(uf);
655     else if (opt_do_extract==1)
656     {
657         // 改变当前的工作目录
658 #ifdef _WIN32
659         if (opt_extractdir && _chdir(dirname))
660 #else
661         if (opt_extractdir && chdir(dirname))
662 #endif
663         {
664           printf("Error changing into %s, aborting
", dirname);
665           exit(-1);
666         }
667 
668         if (filename_to_extract == NULL)
669             ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
670         else
671             ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
672     }
673 
674     unzClose(uf);
675 
676     return ret_value;
677 }
原文地址:https://www.cnblogs.com/smallcroco-blog/p/4789162.html