diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:21:57 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:21:57 -0700 |
commit | 13a294f044ef0a89b2dcbfbb5d4d4c792673348e (patch) | |
tree | d9b377b4d8c00633c3da4e96659bfba9b08287f2 /contrib/minizip/miniunz.c | |
parent | 7c2a874e50b871d04fbd19501f7b42cff55e5abc (diff) | |
download | zlib-13a294f044ef0a89b2dcbfbb5d4d4c792673348e.tar.gz |
zlib 1.2.0.1v1.2.0.1
Diffstat (limited to 'contrib/minizip/miniunz.c')
-rw-r--r-- | contrib/minizip/miniunz.c | 609 |
1 files changed, 322 insertions, 287 deletions
diff --git a/contrib/minizip/miniunz.c b/contrib/minizip/miniunz.c index f3b7832..938d4ef 100644 --- a/contrib/minizip/miniunz.c +++ b/contrib/minizip/miniunz.c @@ -17,7 +17,12 @@ #define CASESENSITIVITY (0) #define WRITEBUFFERSIZE (8192) +#define MAXFILENAME (256) +#ifdef WIN32 +#define USEWIN32IOAPI +#include "iowin32.h" +#endif /* mini unzip, demo of unzip package @@ -34,9 +39,9 @@ dosdate : the new date at the MSDos format (4 bytes) tmu_date : the SAME new date at the tm_unz format */ void change_file_date(filename,dosdate,tmu_date) - const char *filename; - uLong dosdate; - tm_unz tmu_date; + const char *filename; + uLong dosdate; + tm_unz tmu_date; { #ifdef WIN32 HANDLE hFile; @@ -75,17 +80,17 @@ void change_file_date(filename,dosdate,tmu_date) As I don't know well Unix, I wait feedback for the unix portion */ int mymkdir(dirname) - const char* dirname; + const char* dirname; { int ret=0; #ifdef WIN32 - ret = mkdir(dirname); + ret = mkdir(dirname); #else #ifdef unix - ret = mkdir (dirname,0775); + ret = mkdir (dirname,0775); #endif #endif - return ret; + return ret; } int makedir (newdir) @@ -93,14 +98,14 @@ int makedir (newdir) { char *buffer ; char *p; - int len = strlen(newdir); + int len = (int)strlen(newdir); - if (len <= 0) + if (len <= 0) return 0; buffer = (char*)malloc(len+1); strcpy(buffer,newdir); - + if (buffer[len-1] == '/') { buffer[len-1] = '\0'; } @@ -135,104 +140,105 @@ int makedir (newdir) void do_banner() { - printf("MiniUnz 0.15, demo of zLib + Unz package written by Gilles Vollant\n"); - printf("more info at http://wwww.winimage/zLibDll/unzip.htm\n\n"); + printf("MiniUnz 0.15, demo of zLib + Unz package written by Gilles Vollant\n"); + printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n"); } void do_help() -{ - printf("Usage : miniunz [-exvlo] file.zip [file_to_extract]\n\n") ; +{ + printf("Usage : miniunz [-exvlo] file.zip [file_to_extract]\n\n") ; } int do_list(uf) - unzFile uf; + unzFile uf; { - uLong i; - unz_global_info gi; - int err; + uLong i; + unz_global_info gi; + int err; - err = unzGetGlobalInfo (uf,&gi); - if (err!=UNZ_OK) - printf("error %d with zipfile in unzGetGlobalInfo \n",err); + err = unzGetGlobalInfo (uf,&gi); + if (err!=UNZ_OK) + printf("error %d with zipfile in unzGetGlobalInfo \n",err); printf(" Length Method Size Ratio Date Time CRC-32 Name\n"); printf(" ------ ------ ---- ----- ---- ---- ------ ----\n"); - for (i=0;i<gi.number_entry;i++) - { - char filename_inzip[256]; - unz_file_info file_info; - uLong ratio=0; - const char *string_method; - err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); - if (err!=UNZ_OK) - { - printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); - break; - } - if (file_info.uncompressed_size>0) - ratio = (file_info.compressed_size*100)/file_info.uncompressed_size; - - if (file_info.compression_method==0) - string_method="Stored"; - else - if (file_info.compression_method==Z_DEFLATED) - { - uInt iLevel=(uInt)((file_info.flag & 0x6)/2); - if (iLevel==0) - string_method="Defl:N"; - else if (iLevel==1) - string_method="Defl:X"; - else if ((iLevel==2) || (iLevel==3)) - string_method="Defl:F"; /* 2:fast , 3 : extra fast*/ - } - else - string_method="Unkn. "; - - printf("%7lu %6s %7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", - file_info.uncompressed_size,string_method,file_info.compressed_size, - ratio, - (uLong)file_info.tmu_date.tm_mon + 1, + for (i=0;i<gi.number_entry;i++) + { + char filename_inzip[256]; + unz_file_info file_info; + uLong ratio=0; + const char *string_method; + err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); + break; + } + if (file_info.uncompressed_size>0) + ratio = (file_info.compressed_size*100)/file_info.uncompressed_size; + + if (file_info.compression_method==0) + string_method="Stored"; + else + if (file_info.compression_method==Z_DEFLATED) + { + uInt iLevel=(uInt)((file_info.flag & 0x6)/2); + if (iLevel==0) + string_method="Defl:N"; + else if (iLevel==1) + string_method="Defl:X"; + else if ((iLevel==2) || (iLevel==3)) + string_method="Defl:F"; /* 2:fast , 3 : extra fast*/ + } + else + string_method="Unkn. "; + + printf("%7lu %6s %7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", + file_info.uncompressed_size,string_method,file_info.compressed_size, + ratio, + (uLong)file_info.tmu_date.tm_mon + 1, (uLong)file_info.tmu_date.tm_mday, - (uLong)file_info.tmu_date.tm_year % 100, - (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min, - (uLong)file_info.crc,filename_inzip); - if ((i+1)<gi.number_entry) - { - err = unzGoToNextFile(uf); - if (err!=UNZ_OK) - { - printf("error %d with zipfile in unzGoToNextFile\n",err); - break; - } - } - } - - return 0; + (uLong)file_info.tmu_date.tm_year % 100, + (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min, + (uLong)file_info.crc,filename_inzip); + if ((i+1)<gi.number_entry) + { + err = unzGoToNextFile(uf); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzGoToNextFile\n",err); + break; + } + } + } + + return 0; } -int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite) - unzFile uf; - const int* popt_extract_without_path; +int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password) + unzFile uf; + const int* popt_extract_without_path; int* popt_overwrite; + const char* password; { - char filename_inzip[256]; - char* filename_withoutpath; - char* p; + char filename_inzip[256]; + char* filename_withoutpath; + char* p; int err=UNZ_OK; FILE *fout=NULL; void* buf; uInt size_buf; - - unz_file_info file_info; - uLong ratio=0; - err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); - if (err!=UNZ_OK) - { - printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); - return err; - } + unz_file_info file_info; + uLong ratio=0; + err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); + + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); + return err; + } size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); @@ -242,71 +248,71 @@ int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite) return UNZ_INTERNALERROR; } - p = filename_withoutpath = filename_inzip; - while ((*p) != '\0') - { - if (((*p)=='/') || ((*p)=='\\')) - filename_withoutpath = p+1; - p++; - } - - if ((*filename_withoutpath)=='\0') - { - if ((*popt_extract_without_path)==0) - { - printf("creating directory: %s\n",filename_inzip); - mymkdir(filename_inzip); - } - } - else - { - const char* write_filename; - int skip=0; - - if ((*popt_extract_without_path)==0) - write_filename = filename_inzip; - else - write_filename = filename_withoutpath; - - err = unzOpenCurrentFile(uf); - if (err!=UNZ_OK) - { - printf("error %d with zipfile in unzOpenCurrentFile\n",err); - } - - if (((*popt_overwrite)==0) && (err==UNZ_OK)) - { - char rep; - FILE* ftestexist; - ftestexist = fopen(write_filename,"rb"); - if (ftestexist!=NULL) - { - fclose(ftestexist); - do - { - char answer[128]; - printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename); - scanf("%1s",answer); - rep = answer[0] ; - if ((rep>='a') && (rep<='z')) - rep -= 0x20; - } - while ((rep!='Y') && (rep!='N') && (rep!='A')); - } - - if (rep == 'N') - skip = 1; - - if (rep == 'A') - *popt_overwrite=1; - } - - if ((skip==0) && (err==UNZ_OK)) - { - fout=fopen(write_filename,"wb"); + p = filename_withoutpath = filename_inzip; + while ((*p) != '\0') + { + if (((*p)=='/') || ((*p)=='\\')) + filename_withoutpath = p+1; + p++; + } + + if ((*filename_withoutpath)=='\0') + { + if ((*popt_extract_without_path)==0) + { + printf("creating directory: %s\n",filename_inzip); + mymkdir(filename_inzip); + } + } + else + { + const char* write_filename; + int skip=0; + + if ((*popt_extract_without_path)==0) + write_filename = filename_inzip; + else + write_filename = filename_withoutpath; + + err = unzOpenCurrentFilePassword(uf,password); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); + } + + if (((*popt_overwrite)==0) && (err==UNZ_OK)) + { + char rep=0; + FILE* ftestexist; + ftestexist = fopen(write_filename,"rb"); + if (ftestexist!=NULL) + { + fclose(ftestexist); + do + { + char answer[128]; + printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename); + scanf("%1s",answer); + rep = answer[0] ; + if ((rep>='a') && (rep<='z')) + rep -= 0x20; + } + while ((rep!='Y') && (rep!='N') && (rep!='A')); + } + + if (rep == 'N') + skip = 1; + + if (rep == 'A') + *popt_overwrite=1; + } + + if ((skip==0) && (err==UNZ_OK)) + { + fout=fopen(write_filename,"wb"); /* some zipfile don't contain directory alone before file */ - if ((fout==NULL) && ((*popt_extract_without_path)==0) && + if ((fout==NULL) && ((*popt_extract_without_path)==0) && (filename_withoutpath!=(char*)filename_inzip)) { char c=*(filename_withoutpath-1); @@ -316,95 +322,100 @@ int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite) fout=fopen(write_filename,"wb"); } - if (fout==NULL) - { - printf("error opening %s\n",write_filename); - } - } - - if (fout!=NULL) - { - printf(" extracting: %s\n",write_filename); - - do - { - err = unzReadCurrentFile(uf,buf,size_buf); - if (err<0) - { - printf("error %d with zipfile in unzReadCurrentFile\n",err); - break; - } - if (err>0) - if (fwrite(buf,err,1,fout)!=1) - { - printf("error in writing extracted file\n"); + if (fout==NULL) + { + printf("error opening %s\n",write_filename); + } + } + + if (fout!=NULL) + { + printf(" extracting: %s\n",write_filename); + + do + { + err = unzReadCurrentFile(uf,buf,size_buf); + if (err<0) + { + printf("error %d with zipfile in unzReadCurrentFile\n",err); + break; + } + if (err>0) + if (fwrite(buf,err,1,fout)!=1) + { + printf("error in writing extracted file\n"); err=UNZ_ERRNO; - break; - } - } - while (err>0); - fclose(fout); - if (err==0) - change_file_date(write_filename,file_info.dosDate, - file_info.tmu_date); - } + break; + } + } + while (err>0); + if (fout) + fclose(fout); + + if (err==0) + change_file_date(write_filename,file_info.dosDate, + file_info.tmu_date); + } if (err==UNZ_OK) { - err = unzCloseCurrentFile (uf); - if (err!=UNZ_OK) - { - printf("error %d with zipfile in unzCloseCurrentFile\n",err); - } + err = unzCloseCurrentFile (uf); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzCloseCurrentFile\n",err); + } } else - unzCloseCurrentFile(uf); /* don't lose the error */ - } + unzCloseCurrentFile(uf); /* don't lose the error */ + } - free(buf); + free(buf); return err; } -int do_extract(uf,opt_extract_without_path,opt_overwrite) - unzFile uf; - int opt_extract_without_path; +int do_extract(uf,opt_extract_without_path,opt_overwrite,password) + unzFile uf; + int opt_extract_without_path; int opt_overwrite; + const char* password; { - uLong i; - unz_global_info gi; - int err; - FILE* fout=NULL; + uLong i; + unz_global_info gi; + int err; + FILE* fout=NULL; - err = unzGetGlobalInfo (uf,&gi); - if (err!=UNZ_OK) - printf("error %d with zipfile in unzGetGlobalInfo \n",err); + err = unzGetGlobalInfo (uf,&gi); + if (err!=UNZ_OK) + printf("error %d with zipfile in unzGetGlobalInfo \n",err); - for (i=0;i<gi.number_entry;i++) - { + for (i=0;i<gi.number_entry;i++) + { if (do_extract_currentfile(uf,&opt_extract_without_path, - &opt_overwrite) != UNZ_OK) + &opt_overwrite, + password) != UNZ_OK) break; - if ((i+1)<gi.number_entry) - { - err = unzGoToNextFile(uf); - if (err!=UNZ_OK) - { - printf("error %d with zipfile in unzGoToNextFile\n",err); - break; - } - } - } - - return 0; + if ((i+1)<gi.number_entry) + { + err = unzGoToNextFile(uf); + if (err!=UNZ_OK) + { + printf("error %d with zipfile in unzGoToNextFile\n",err); + break; + } + } + } + + return 0; } -int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite) - unzFile uf; - const char* filename; - int opt_extract_without_path; +int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password) + unzFile uf; + const char* filename; + int opt_extract_without_path; int opt_overwrite; + const char* password; { int err = UNZ_OK; if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK) @@ -414,7 +425,8 @@ int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite) } if (do_extract_currentfile(uf,&opt_extract_without_path, - &opt_overwrite) == UNZ_OK) + &opt_overwrite, + password) == UNZ_OK) return 0; else return 1; @@ -422,87 +434,110 @@ int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite) int main(argc,argv) - int argc; - char *argv[]; + int argc; + char *argv[]; { - const char *zipfilename=NULL; + const char *zipfilename=NULL; const char *filename_to_extract=NULL; - int i; - int opt_do_list=0; - int opt_do_extract=1; - int opt_do_extract_withoutpath=0; - int opt_overwrite=0; - char filename_try[512]; - unzFile uf=NULL; - - do_banner(); - if (argc==1) - { - do_help(); - exit(0); - } - else - { - for (i=1;i<argc;i++) - { - if ((*argv[i])=='-') - { - const char *p=argv[i]+1; - - while ((*p)!='\0') - { - char c=*(p++);; - if ((c=='l') || (c=='L')) - opt_do_list = 1; - if ((c=='v') || (c=='V')) - opt_do_list = 1; - if ((c=='x') || (c=='X')) - opt_do_extract = 1; - if ((c=='e') || (c=='E')) - opt_do_extract = opt_do_extract_withoutpath = 1; - if ((c=='o') || (c=='O')) - opt_overwrite=1; - } - } - else + const char *password=NULL; + char filename_try[MAXFILENAME+16] = ""; + int i; + int opt_do_list=0; + int opt_do_extract=1; + int opt_do_extract_withoutpath=0; + int opt_overwrite=0; + unzFile uf=NULL; + + do_banner(); + if (argc==1) + { + do_help(); + return 0; + } + else + { + for (i=1;i<argc;i++) + { + if ((*argv[i])=='-') + { + const char *p=argv[i]+1; + + while ((*p)!='\0') + { + char c=*(p++);; + if ((c=='l') || (c=='L')) + opt_do_list = 1; + if ((c=='v') || (c=='V')) + opt_do_list = 1; + if ((c=='x') || (c=='X')) + opt_do_extract = 1; + if ((c=='e') || (c=='E')) + opt_do_extract = opt_do_extract_withoutpath = 1; + if ((c=='o') || (c=='O')) + opt_overwrite=1; + if (((c=='p') || (c=='P')) && (i+1<argc)) + { + password=argv[i+1]; + i++; + } + } + } + else { - if (zipfilename == NULL) - zipfilename = argv[i]; + if (zipfilename == NULL) + zipfilename = argv[i]; else if (filename_to_extract==NULL) filename_to_extract = argv[i] ; } - } - } - - if (zipfilename!=NULL) - { - strcpy(filename_try,zipfilename); - uf = unzOpen(zipfilename); - if (uf==NULL) - { - strcat(filename_try,".zip"); - uf = unzOpen(filename_try); - } - } - - if (uf==NULL) - { - printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename); - exit (1); - } + } + } + + if (zipfilename!=NULL) + { + + #ifdef USEWIN32IOAPI + zlib_filefunc_def ffunc; + #endif + + strncpy(filename_try, zipfilename,MAXFILENAME-1); + /* strncpy doesnt append the trailing NULL, of the string is too long. */ + filename_try[ MAXFILENAME ] = '\0'; + + #ifdef USEWIN32IOAPI + fill_win32_filefunc(&ffunc); + uf = unzOpen2(zipfilename,&ffunc); + #else + uf = unzOpen(zipfilename); + #endif + if (uf==NULL) + { + strcat(filename_try,".zip"); + #ifdef USEWIN32IOAPI + uf = unzOpen2(filename_try,&ffunc); + #else + uf = unzOpen(filename_try); + #endif + } + } + + if (uf==NULL) + { + printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename); + return 1; + } printf("%s opened\n",filename_try); - if (opt_do_list==1) - return do_list(uf); - else if (opt_do_extract==1) + if (opt_do_list==1) + return do_list(uf); + else if (opt_do_extract==1) { if (filename_to_extract == NULL) - return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite); + return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite,password); else return do_extract_onefile(uf,filename_to_extract, - opt_do_extract_withoutpath,opt_overwrite); + opt_do_extract_withoutpath,opt_overwrite,password); } - unzCloseCurrentFile(uf); + unzCloseCurrentFile(uf); - return 0; /* to avoid warning */ + return 0; } |