void ForkSplit(char* fileName, long chunksize, int nchunks, int numOfProc){ FILE* srcFile; FILE* destFile; int currentChunk; int numberActive = 0; pid_t process; process = 1; int status; for (currentChunk=0; currentChunk < nchunks; currentChunk++, numberActive++){ for (;numberActive>=numOfProc;--numberActive) wait(&status); if (process!=0) //only for from parent process = fork(); //child if (process == 0){ srcFile = fopen(fileName, "r"); if (!srcFile){ perror("Can't open file"); exit(EXIT_FAILURE); } fseek(srcFile, currentChunk*chunksize, SEEK_SET); char destFileName[MAX_FILENAME_LENGTH]; snprintf(destFileName, MAX_FILENAME_LENGTH, "%s.0%d", fileName, currentChunk); destFile = fopen(destFileName, "w+"); if (!destFile){ perror("Can't open file"); exit(EXIT_FAILURE); } readAndWrite(srcFile,destFile, chunksize); fclose(srcFile); fclose(destFile); exit(0); } } while (numberActive>0){ wait(&status); numberActive--; } }
//Read the file and extract its contents to //the directory of the zipfile. int do_extract(char *inputzip) { char *extract_dir = dirname(inputzip); if ( (extract_dir != NULL) && (strlen(extract_dir) > 0) ) { // ensure directory is created first char *dir = (char *) malloc(strlen(extract_dir) + 2); if(!dir) { return 0; } sprintf(dir, "%s/", extract_dir); mkdirs(dir); free(dir); chdir(extract_dir); free(extract_dir); } if (! openZipFileReader(inputzip)) { return 0; } bool next = isNext(); // We must have at least one entry if (!next) { //sprintf(message,"Error: no entries in zip file.\n"); return 0; } while (next) { readAndWrite(); next = isNext(); } closeZipFileReader(); return 1; }
//Read the file and extract its contents to the //to the directory of the zipfile. void do_read(char *inputzip, char* outputdir) { char *extract_dir = NULL; if (outputdir != NULL) extract_dir = dirname(outputdir); else extract_dir = dirname(inputzip); if ( (extract_dir != NULL) && (strlen(extract_dir) > 0) ) { // ensure directory is created first char *dir = (char *) malloc(strlen(extract_dir) + 2); sprintf(dir, "%s/", extract_dir); mkdirs(dir); free(dir); chdir(extract_dir); } openZipFileReader(inputzip); fprintf(stderr,"Archive: %s\n",inputzip); bool next = isNext(); // We must have atleast one entry if (!next) { sprintf(message,"Error: no entries in zip file.\n"); l_abort(message); } while (next) { readAndWrite(); next = isNext(); } closeZipFileReader(); if (remove_input_zip_file) remove(inputzip); }