#ifdefined(__APPLE__) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions #define FOPEN_FUNC(filename, mode) fopen(filename, mode) #define FTELLO_FUNC(stream) ftello(stream) #define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin) #else #define FOPEN_FUNC(filename, mode) fopen64(filename, mode) #define FTELLO_FUNC(stream) ftello64(stream) #define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin) #endif
list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT if it exists
*/
/* change_file_date : change the date/time of a file filename : the filename of the file where date/time must be modified dosdate : the new date at the MSDos format (4 bytes)
tmu_date : the SAME new date at the tm_unz format */ staticvoid change_file_date(constchar *filename, uLong dosdate, tm_unz tmu_date) { #ifdef _WIN32
HANDLE hFile;
FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
staticvoid do_help(void) {
printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \ " -e Extract without pathname (junk paths)\n" \ " -x Extract with pathname\n" \ " -v list files\n" \ " -l list files\n" \ " -d directory to extract into\n" \ " -o overwrite files without prompting\n" \ " -p extract encrypted file using password\n\n");
}
staticvoid Display64BitsSize(ZPOS64_T n, int size_char) { /* to avoid compatibility problem , we do here the conversion */ char number[21]; int offset=19; int pos_string = 19;
number[20]=0; for (;;) {
number[offset]=(char)((n%10)+'0'); if (number[offset] != '0')
pos_string=offset;
n/=10; if (offset==0) break;
offset--;
}
{ int size_display_string = 19-pos_string; while (size_char > size_display_string)
{
size_char--;
printf(" ");
}
}
printf("%s",&number[pos_string]);
}
staticint do_list(unzFile uf) {
uLong i;
unz_global_info64 gi; int err;
err = unzGetGlobalInfo64(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_info64 file_info;
uLong ratio=0; constchar *string_method = ""; char charCrypt=' ';
err = unzGetCurrentFileInfo64(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 = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
/* display a '*' if the file is encrypted */ if ((file_info.flag & 1) != 0)
charCrypt='*';
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"; elseif (iLevel==1)
string_method="Defl:X"; elseif ((iLevel==2) || (iLevel==3))
string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
} else if (file_info.compression_method==Z_BZIP2ED)
{
string_method="BZip2 ";
} else
string_method="Unkn. ";
Display64BitsSize(file_info.uncompressed_size,7);
printf(" %6s%c",string_method,charCrypt);
Display64BitsSize(file_info.compressed_size,7);
printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
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;
}
}
}
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
{ constchar* 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_FUNC(write_filename,"rb"); if (ftestexist!=NULL)
{
fclose(ftestexist); do
{ char answer[128]; int ret;
printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
ret = scanf("%1s",answer); if (ret != 1)
{ exit(EXIT_FAILURE);
}
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_FUNC(write_filename,"wb"); /* some zipfile don't contain directory alone before file */ if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
(filename_withoutpath!=(char*)filename_inzip))
{ char c=*(filename_withoutpath-1);
*(filename_withoutpath-1)='\0';
makedir(write_filename);
*(filename_withoutpath-1)=c;
fout=FOPEN_FUNC(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,(unsigned)err,1,fout)!=1)
{
printf("error in writing extracted file\n");
err=UNZ_ERRNO; 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);
}
} else
unzCloseCurrentFile(uf); /* don't lose the error */
}
free(buf); return err;
}
staticint do_extract(unzFile uf, int opt_extract_without_path, int opt_overwrite, constchar* password) {
uLong i;
unz_global_info64 gi; int err;
err = unzGetGlobalInfo64(uf,&gi); if (err!=UNZ_OK)
printf("error %d with zipfile in unzGetGlobalInfo \n",err);
for (i=0;i<gi.number_entry;i++)
{ if (do_extract_currentfile(uf,&opt_extract_without_path,
&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;
}
staticint do_extract_onefile(unzFile uf, constchar* filename, int opt_extract_without_path, int opt_overwrite, constchar* password) { if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
{
printf("file %s not found in the zipfile\n",filename); return 2;
}
int main(int argc, char *argv[]) { constchar *zipfilename=NULL; constchar *filename_to_extract=NULL; constchar *password=NULL; char filename_try[MAXFILENAME+16] = ""; int i; int ret_value=0; int opt_do_list=0; int opt_do_extract=1; int opt_do_extract_withoutpath=0; int opt_overwrite=0; int opt_extractdir=0; constchar *dirname=NULL;
unzFile uf=NULL;
do_banner(); if (argc==1)
{
do_help(); return 0;
} else
{ for (i=1;i<argc;i++)
{ if ((*argv[i])=='-')
{ constchar *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=='d') || (c=='D'))
{
opt_extractdir=1;
dirname=argv[i+1];
}
strncpy(filename_try, zipfilename,MAXFILENAME-1); /* strncpy doesn't append the trailing NULL, of the string is too long. */
filename_try[ MAXFILENAME ] = '\0';
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.