static int compress_recursive(zipFile archive, const string& dir, const string& prefix) { vector<string> files ,dirs; int r = FILE_get_directory(dir, &files,&dirs); if(r < 0) return r; for(vector<string>::iterator f = files.begin(); f != files.end(); ++f) { string sf = dir + *f; string df = prefix + *f; r = compress_one_file(archive, sf, df); if (r != 0) return r; } for(vector<string>::iterator d = dirs.begin(); d != dirs.end(); ++d) { string sd = dir + *d + DIR_STR; string dd = prefix + *d + "/"; // FORCE unix / or Mac loses its mind on decompress. r = compress_recursive(archive, sd, dd); if (r != 0) return r; } return 0; }
INT32 ProtocolGZip::EncodeData(INT8 *ps, INT32 sLen, INT8 *pd, INT32 *dLen, void *pNote, string &strErr) { INT32 ret=0; #ifndef WIN32 //½«psÖÐÊý¾Ý´æΪÁÙʱÎļþ ret = writefile(m_gzipReqFile.c_str(), pd, sLen, strErr); if (ret != SUCCESS) { return FAILURE; } //Ö´ÐÐgzѹËõ ret = compress_one_file(m_gzipReqFile.c_str(), (INT8 *)m_gzipRspFile.c_str()); if (ret != 0) { strErr = "GZipѹËõ´íÎó"; return FAILURE; } //¶ÁȡѹËõÎļþÖÁpData ret = readfile(m_gzipRspFile.c_str(), pd, dLen, strErr); if (ret != SUCCESS) { return FAILURE; } #endif DBG_PRINT(("GZip::EncodeData() complete.")) return SUCCESS; }
int main() { int m; printf("Do you want to: \n1.compress\n2.decompress"); scanf("%d",&m); if(m==1){ char input[50]; char output[50]; printf( "Give file to be compressed name along with address\n"); fgets(input,50,stdin); input[strlen(input)-1]= '\0'; printf( "Give file name of compressed file along with address\n"); fgets(output,50,stdin); input[strlen(output)-1]= '\0'; compress_one_file(input,output); } }
static int packageSourceFolder(char **source, char *desintation, char *extensions[], int extCount) { FTS *ftsp; FTSENT *p, *chp; int fts_options = FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR; int rval = 0; // remove if unused! char line[512]; FILE *pakFile; FILE *file; unsigned int start_offset= 0; unsigned int f_index=0; // file index if (file = fopen(desintation, "r")) { printf("File exists, would you like to overwrite it y/n ? "); int answer; int loop=1; fclose(file); while (loop){ answer = getchar(); switch(answer){ case 'y': loop=0; break; case 'n': printf("Please run Bundle again with the correct output path.\n"); exit(1); case 10: // \n break; default: printf("not sure ? ;) \n (y/n) >"); } } }else { fclose(file); } pakFile = fopen(desintation,"wb+"); printf("\n----------------\n"); // initialize header with nuber of files if ((start_offset=header_init(pakFile, fileCountForHeader)) == -1){ // perror("bundler_header"); fprintf(stderr, "Cannot initialize header...exiting\n"); return 0; } size_t header_size = fileCountForHeader* HEADER_OFFSET_SIZE + sizeof(int); if ((ftsp = fts_open(source, fts_options, NULL)) == NULL) { warn("fts_open"); return -1; } /* Initialize ftsp with as many argv[] parts as possible. */ chp = fts_children(ftsp, 0); if (chp == NULL) { return 0; /* no files to traverse */ } printf("\nCreating the Bundle\n\n"); while ((p = fts_read(ftsp)) != NULL) { int tempFileExists = 0; //used to flag temp file for deletion switch (p->fts_info) { case FTS_D: printf(">dir: %s\n\n", p->fts_path); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"); printData(pakFile, p->fts_path); break; case FTS_F: if((strstr(p->fts_path, ".DS_Store") != NULL)//ignore DS_Store and exe || (strstr(p->fts_path, ".exe") != NULL)) { printf("Found file to ignore: %s\n", p->fts_path); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"); } else { printf("\t>file: %s\n", p->fts_path); printData(pakFile, p->fts_path); FILE *tempFile; if(shouldCompressFileType(p->fts_path, extensions, extCount) == 1) { /* compress the fts_path file and write the compressed data one to pak file */ compress_one_file(p->fts_path, "temp.txt"); tempFile = fopen("temp.txt","rb"); tempFileExists = 1; } else { tempFile = fopen(p->fts_path,"rb"); } char byte; fseek(pakFile, 0L, SEEK_END); off_t offset = ftell(pakFile); //get the size of the file fseek(tempFile, 0L, SEEK_END); long size = ftell(tempFile); fseek(tempFile, 0L, SEEK_SET); offset= offset;//-HEADER_OFFSET_SIZE; char *fileName = p->fts_path; char *tempFileName = strdup(fileName); //update header //offset_p off= malloc(HEADER_OFFSET_SIZE); header_offset off; off.hash = __ac_X31_hash_string( filename(fileName) ); off.size= size; off.offset_start= offset; header_write_offset(pakFile, &off, f_index++); // print the file info if(tempFileName) { printf("\t>The offset for %s is %d\n", tempFileName, (unsigned int)offset); printf("\t>The written size of %s is %lu\n", basename(tempFileName), size); printf("\t>%s was added to the bundle\n\n", basename(tempFileName)); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n"); free(tempFileName); } // copy the data from the temp compressed file to the pak file // fseek(pakFile, start_offset, SEEK_SET); //fseek(pakFile, 0L, SEEK_SET); fseek(pakFile, 0L, SEEK_END); int d= 0; while (!feof(tempFile)) { fread(&byte, sizeof(char), 1, tempFile); fwrite(&byte, sizeof(char), 1, pakFile); } fclose(tempFile); // done! //delete the temporary file if(tempFileExists == 1) { if (remove("temp.txt") == -1) perror("Error in deleting temp file"); } break; } default: break; } } fclose(pakFile); fts_close(ftsp); return 0; }