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] = '