int main(int argc, char *argv[]) { zipFile zf = NULL; #ifdef USEWIN32IOAPI zlib_filefunc64_def ffunc = {0}; #endif char *zipfilename = NULL; const char* password = NULL; void* buf = NULL; int size_buf = WRITEBUFFERSIZE; int zipfilenamearg = 0; int errclose = 0; int err = 0; int i = 0; int opt_overwrite = APPEND_STATUS_CREATE; int opt_compress_level = Z_DEFAULT_COMPRESSION; int opt_exclude_path = 0; do_banner(); if (argc == 1) { do_help(); return 0; } /* Parse command line options */ for (i = 1; i < argc; i++) { if ((*argv[i]) == '-') { const char *p = argv[i]+1; while ((*p) != '\0') { char c = *(p++);; if ((c == 'o') || (c == 'O')) opt_overwrite = APPEND_STATUS_CREATEAFTER; if ((c == 'a') || (c == 'A')) opt_overwrite = APPEND_STATUS_ADDINZIP; if ((c >= '0') && (c <= '9')) opt_compress_level = (c - '0'); if ((c == 'j') || (c == 'J')) opt_exclude_path = 1; if (((c == 'p') || (c == 'P')) && (i+1 < argc)) { password=argv[i+1]; i++; } } } else { if (zipfilenamearg == 0) zipfilenamearg = i; } } if (zipfilenamearg == 0) { do_help(); return 0; } zipfilename = argv[zipfilenamearg]; buf = (void*)malloc(size_buf); if (buf == NULL) { printf("Error allocating memory\n"); return ZIP_INTERNALERROR; } if (opt_overwrite == 2) { /* If the file don't exist, we not append file */ if (check_file_exists(zipfilename) == 0) opt_overwrite = 1; } else if (opt_overwrite == 0) { /* If ask the user what to do because append and overwrite args not set */ //if (check_file_exists(zipfilename) != 0) //{ // char rep = 0; // do // { // char answer[128]; // printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", zipfilename); // if (scanf("%1s", answer) != 1) // exit(EXIT_FAILURE); // rep = answer[0]; // if ((rep >= 'a') && (rep <= 'z')) // rep -= 0x20; // } // while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); // if (rep == 'A') // opt_overwrite = 2; // else if (rep == 'N') // { // do_help(); // free(buf); // return 0; // } //} } #ifdef USEWIN32IOAPI fill_win32_filefunc64A(&ffunc); zf = zipOpen2_64(zipfilename, opt_overwrite, NULL, &ffunc); #else zf = zipOpen64(zipfilename, opt_overwrite); #endif if (zf == NULL) { printf("error opening %s\n", zipfilename); err = ZIP_ERRNO; } else printf("creating %s\n", zipfilename); /* Go through command line args looking for files to add to zip */ for (i = zipfilenamearg + 1; (i < argc) && (err == ZIP_OK); i++) { FILE *fin = NULL; int size_read = 0; const char* filenameinzip = argv[i]; const char *savefilenameinzip; zip_fileinfo zi = {0}; unsigned long crcFile = 0; int zip64 = 0; /* Skip command line options */ if ((((*(argv[i])) == '-') || ((*(argv[i])) == '/')) && (strlen(argv[i]) == 2) && ((argv[i][1] == 'o') || (argv[i][1] == 'O') || (argv[i][1] == 'a') || (argv[i][1] == 'A') || (argv[i][1] == 'p') || (argv[i][1] == 'P') || ((argv[i][1] >= '0') && (argv[i][1] <= '9')))) continue; /* Get information about the file on disk so we can store it in zip */ filetime(filenameinzip, &zi.tmz_date, &zi.dosDate); if ((password != NULL) && (err == ZIP_OK)) err = get_file_crc(filenameinzip, buf, size_buf, &crcFile); zip64 = is_large_file(filenameinzip); /* Construct the filename that our file will be stored in the zip as. The path name saved, should not include a leading slash. If it did, windows/xp and dynazip couldn't read the zip file. */ savefilenameinzip = filenameinzip; while (savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/') savefilenameinzip++; /* Should the file be stored with any path info at all? */ if (opt_exclude_path) { const char *tmpptr = NULL; const char *lastslash = 0; for (tmpptr = savefilenameinzip; *tmpptr; tmpptr++) { if (*tmpptr == '\\' || *tmpptr == '/') lastslash = tmpptr; } if (lastslash != NULL) savefilenameinzip = lastslash + 1; /* base filename follows last slash. */ } /* Add to zip file */ err = zipOpenNewFileInZip3_64(zf, savefilenameinzip, &zi, NULL, 0, NULL, 0, NULL /* comment*/, (opt_compress_level != 0) ? Z_DEFLATED : 0, opt_compress_level,0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password, crcFile, zip64); if (err != ZIP_OK) printf("error in opening %s in zipfile (%d)\n", filenameinzip, err); else { fin = FOPEN_FUNC(filenameinzip, "rb"); if (fin == NULL) { err = ZIP_ERRNO; printf("error in opening %s for reading\n", filenameinzip); } } if (err == ZIP_OK) { /* Read contents of file and write it to zip */ do { size_read = (int)fread(buf, 1, size_buf, fin); if ((size_read < size_buf) && (feof(fin) == 0)) { printf("error in reading %s\n",filenameinzip); err = ZIP_ERRNO; } if (size_read > 0) { err = zipWriteInFileInZip(zf, buf, size_read); if (err < 0) printf("error in writing %s in the zipfile (%d)\n", filenameinzip, err); } } while ((err == ZIP_OK) && (size_read > 0)); } if (fin) fclose(fin); if (err < 0) err = ZIP_ERRNO; else { err = zipCloseFileInZip(zf); if (err != ZIP_OK) printf("error in closing %s in the zipfile (%d)\n", filenameinzip, err); } } errclose = zipClose(zf, NULL); if (errclose != ZIP_OK) printf("error in closing %s (%d)\n", zipfilename, errclose); free(buf); return err; }
int main(int argc,char *argv[]) { char zipfilename[MAXFILENAME]= {0}; char filename_to_extract[MAXFILENAME]= {0}; const char *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; char dirname[MAXFILENAME]= {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=='d') || (c=='D')) { opt_extractdir=1; strcat(dirname,__DIR__); strcat(dirname,"/"); strcat(dirname,argv[i+1]); } if (((c=='p') || (c=='P')) && (i+1<argc)) { password=argv[i+1]; i++; } } } else { if (!strlen(zipfilename)) { strcat(zipfilename,__DIR__); strcat(zipfilename,"/"); strcat(zipfilename,argv[i]); } else if ((!strlen(filename_to_extract)) && (!opt_extractdir)) { strcat(filename_to_extract,__DIR__); strcat(filename_to_extract,"/"); strcat(filename_to_extract,argv[i]); } } } } if (strlen(zipfilename)) { # ifdef USEWIN32IOAPI zlib_filefunc64_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_filefunc64A(&ffunc); uf = unzOpen2_64(zipfilename,&ffunc); # else uf = unzOpen64(zipfilename); # endif if (uf==NULL) { strcat(filename_try,".zip"); # ifdef USEWIN32IOAPI uf = unzOpen2_64(filename_try,&ffunc); # else uf = unzOpen64(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) ret_value = do_list(uf); else if (opt_do_extract==1) { chdir(__DIR__); #ifdef _WIN32 if (opt_extractdir && _chdir(dirname)) #else if (opt_extractdir && chdir(dirname)) #endif { printf("Error changing into %s, aborting\n", dirname); exit(-1); } if (!strlen(filename_to_extract)) ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password); else ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password); } unzClose(uf); return ret_value; }
/* int main(int argc, char *argv[], char *environ[]) */ int main(int argc, char *argv[]) { char tmpbuf[MAX_OBJNAMELEN + 1]; obj_t *tobj; int i; char *p; char c; short intstatus = 0; short preload = 1; char *fn=NULL; setvbuf(stdout, NULL, _IONBF, 0); if (argc < 2) { do_banner(); do_help(argv[0]); return -1; } if ((N = nsp_newstate()) == NULL) return -1; setsigs(); N->debug = 0; //nspbase_register_all(N); /* add env */ tobj = nsp_settable(N, &N->g, "_ENV"); for (i = 0;environ[i] != NULL;i++) { strncpy(tmpbuf, environ[i], MAX_OBJNAMELEN); p = strchr(tmpbuf, '='); if (!p) continue; *p = '\0'; p = strchr(environ[i], '=') + 1; nsp_setstr(N, tobj, tmpbuf, p, -1); } /* add args */ tobj = nsp_settable(N, &N->g, "_ARGS"); for (i = 0;i < argc;i++) { n_ntoa(N, tmpbuf, i, 10, 0); nsp_setstr(N, tobj, tmpbuf, argv[i], -1); } tobj = nsp_settable(N, &N->g, "io"); nsp_setcfunc(N, tobj, "gets", (NSP_CFUNC)neslib_io_gets); for (i = 1;i < argc;i++) { if (argv[i] == NULL) break; if (argv[i][0] == '-') { c = argv[i][1]; if (!c) { break; } else if ((c == 'd') || (c == 'D')) { N->debug = 1; } else if ((c == 's') || (c == 'S')) { intstatus = 1; } else if ((c == 'b') || (c == 'B')) { preload = 0; } else if ((c == 'e') || (c == 'E')) { if (++i < argc) { if (preload) do_preload(N); nsp_exec(N, argv[i]); if (N->err) goto err; } } else if ((c == 'f') || (c == 'F')) { if (++i < argc) { preppath(N, argv[i]); set_console_title(N); if (preload) do_preload(N); fn=argv[i]; nsp_execfile(N, fn); if (N->err) goto err; } } else if ((c == 'v') || (c == 'V')) { printf(NSP_VERSION "\r\n"); return 0; } else { do_help(argv[0]); return -1; } } else { preppath(N, argv[i]); set_console_title(N); if (preload) do_preload(N); fn=argv[i]; nsp_execfile(N, fn); if (N->err) goto err; } } err: if (N->err) printf("%s\r\n", N->errbuf); nsp_freestate(N); if (intstatus || N->allocs != N->frees) printstate(N, fn); nsp_endstate(N); return 0; }
int main( int argc, char *argv[]) { int i; int opt_overwrite=0; int opt_compress_level=Z_DEFAULT_COMPRESSION; int opt_exclude_path=0; int zipfilenamearg = 0; char filename_try[MAXFILENAME+16]; int zipok; int err=0; int size_buf=0; void* buf=NULL; const char* password=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=='o') || (c=='O')) opt_overwrite = 1; if ((c=='a') || (c=='A')) opt_overwrite = 2; if ((c>='0') && (c<='9')) opt_compress_level = c-'0'; if ((c=='j') || (c=='J')) opt_exclude_path = 1; if (((c=='p') || (c=='P')) && (i+1<argc)) { password=argv[i+1]; i++; } } } else { if (zipfilenamearg == 0) { zipfilenamearg = i ; } } } } size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) { printf("Error allocating memory\n"); return ZIP_INTERNALERROR; } if (zipfilenamearg==0) { zipok=0; } else { int i,len; int dot_found=0; zipok = 1 ; strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1); /* strncpy doesnt append the trailing NULL, of the string is too long. */ filename_try[ MAXFILENAME ] = '\0'; len=(int)strlen(filename_try); for (i=0;i<len;i++) if (filename_try[i]=='.') dot_found=1; if (dot_found==0) strcat(filename_try,".zip"); if (opt_overwrite==2) { /* if the file don't exist, we not append file */ if (check_exist_file(filename_try)==0) opt_overwrite=1; } else if (opt_overwrite==0) if (check_exist_file(filename_try)!=0) { char rep=0; do { char answer[128]; int ret; printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try); 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') zipok = 0; if (rep=='A') opt_overwrite = 2; } } if (zipok==1) { zipFile zf; int errclose; # ifdef USEWIN32IOAPI zlib_filefunc64_def ffunc; fill_win32_filefunc64A(&ffunc); zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc); # else zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0); # endif if (zf == NULL) { printf("error opening %s\n",filename_try); err= ZIP_ERRNO; } else printf("creating %s\n",filename_try); for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++) { if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) && ((argv[i][1]=='o') || (argv[i][1]=='O') || (argv[i][1]=='a') || (argv[i][1]=='A') || (argv[i][1]=='p') || (argv[i][1]=='P') || ((argv[i][1]>='0') || (argv[i][1]<='9'))) && (strlen(argv[i]) == 2))) { FILE * fin; int size_read; const char* filenameinzip = argv[i]; const char *savefilenameinzip; zip_fileinfo zi; unsigned long crcFile=0; int zip64 = 0; zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; zi.dosDate = 0; zi.internal_fa = 0; zi.external_fa = 0; filetime(filenameinzip,&zi.tmz_date,&zi.dosDate); /* err = zipOpenNewFileInZip(zf,filenameinzip,&zi, NULL,0,NULL,0,NULL / * comment * /, (opt_compress_level != 0) ? Z_DEFLATED : 0, opt_compress_level); */ if ((password != NULL) && (err==ZIP_OK)) err = getFileCrc(filenameinzip,buf,size_buf,&crcFile); zip64 = isLargeFile(filenameinzip); /* The path name saved, should not include a leading slash. */ /*if it did, windows/xp and dynazip couldn't read the zip file. */ savefilenameinzip = filenameinzip; while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' ) { savefilenameinzip++; } /*should the zip file contain any path at all?*/ if( opt_exclude_path ) { const char *tmpptr; const char *lastslash = 0; for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++) { if( *tmpptr == '\\' || *tmpptr == '/') { lastslash = tmpptr; } } if( lastslash != NULL ) { savefilenameinzip = lastslash+1; // base filename follows last slash. } } /**/ err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi, NULL,0,NULL,0,NULL /* comment*/, (opt_compress_level != 0) ? Z_DEFLATED : 0, opt_compress_level,0, /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, password,crcFile, zip64); if (err != ZIP_OK) printf("error in opening %s in zipfile\n",filenameinzip); else { fin = FOPEN_FUNC(filenameinzip,"rb"); if (fin==NULL) { err=ZIP_ERRNO; printf("error in opening %s for reading\n",filenameinzip); } } if (err == ZIP_OK) do { err = ZIP_OK; size_read = (int)fread(buf,1,size_buf,fin); if (size_read < size_buf) if (feof(fin)==0) { printf("error in reading %s\n",filenameinzip); err = ZIP_ERRNO; } if (size_read>0) { err = zipWriteInFileInZip (zf,buf,size_read); if (err<0) { printf("error in writing %s in the zipfile\n", filenameinzip); } } } while ((err == ZIP_OK) && (size_read>0)); if (fin) fclose(fin); if (err<0) err=ZIP_ERRNO; else { err = zipCloseFileInZip(zf); if (err!=ZIP_OK) printf("error in closing %s in the zipfile\n", filenameinzip); } } } errclose = zipClose(zf,NULL); if (errclose != ZIP_OK) printf("error in closing %s\n",filename_try); } else { do_help(); } free(buf); return 0; }