/** * Write an Unitex file content (to system filesystem or filespace) * it write from two buffer (prefix and suffix). This is useful for writing both header and footer (or BOM and text...) */ UNITEX_FUNC int UNITEX_CALL WriteUnitexFile(const char*name,const void*buffer_prefix,size_t size_prefix,const void*buffer_suffix,size_t size_suffix) { ABSTRACTFILE* vfWrite = af_fopen(name, "wb"); if (vfWrite == NULL) { return 1; } int retValue = 0; if (size_prefix > 0) if (size_prefix != af_fwrite(buffer_prefix,1,size_prefix,vfWrite)) retValue = 1; if (retValue==0 && (size_suffix > 0)) if (size_suffix != af_fwrite(buffer_suffix,1,size_suffix,vfWrite)) retValue = 1; af_fclose(vfWrite); return retValue; }
static int do_extract_from_opened_pack_archive_currentfile( unzFile uf, const int* popt_extract_without_path, const char* prefix_extracting_name, int transform_path_separator, int quiet) { char filename_inzip[0x200]; char* filename_withoutpath; char* p; int err=UNZ_OK; ABSTRACTFILE *fout=NULL; void* buf; uInt size_buf; unz_file_info file_info; err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); if (err!=UNZ_OK) { error("error %d with zipfile in unzGetCurrentFileInfo\n",err); return err; } size_buf = UNPACK_WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) { error("Error allocating memory\n"); 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) { { size_t len_prefix = strlen(prefix_extracting_name); size_t len_directory_in_zip = strlen(filename_inzip); size_t total_len = len_prefix + len_directory_in_zip; char* provis_dir_name = NULL; if (len_directory_in_zip > 0) provis_dir_name = (char*)malloc(total_len + 2); if (provis_dir_name) { strcpy(provis_dir_name, prefix_extracting_name); strcpy(provis_dir_name + len_prefix, filename_inzip); transform_fileName_separator(provis_dir_name + len_prefix, transform_path_separator); if (quiet == 0) u_printf("creating directory: %s\n", provis_dir_name); mkDirPortable(provis_dir_name); free(provis_dir_name); } } } } else { char* previousPathCreated = NULL; 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) { error("error %d with zipfile in unzOpenCurrentFilePassword\n",err); } if ((skip==0) && (err==UNZ_OK)) { char* provis_concat_name = NULL; if (prefix_extracting_name == NULL) prefix_extracting_name = ""; if (prefix_extracting_name != NULL) { size_t len_prefix = strlen(prefix_extracting_name); size_t total_len = len_prefix + strlen(write_filename); provis_concat_name = (char*)malloc(total_len+2); if (provis_concat_name != NULL) { strcpy(provis_concat_name,prefix_extracting_name); strcat(provis_concat_name,write_filename); transform_fileName_separator(provis_concat_name + len_prefix, transform_path_separator); } } const char* useFileName = (provis_concat_name != NULL) ? provis_concat_name : write_filename; if ((*popt_extract_without_path)==0) { char* newPathOnlyPortion = (char*)malloc(strlen(useFileName)+1); if (newPathOnlyPortion != NULL) { strcpy(newPathOnlyPortion,useFileName); removeNoPathPortionInFileName(newPathOnlyPortion); int can_skip_creating = 0; if (previousPathCreated != NULL) if (strcmp(previousPathCreated,newPathOnlyPortion) == 0) can_skip_creating = 1; if (can_skip_creating == 0) { if (quiet == 0) u_printf("Creating directory: %s\n",newPathOnlyPortion); mkDirPortable(newPathOnlyPortion); } if (previousPathCreated != NULL) free(previousPathCreated); previousPathCreated = newPathOnlyPortion; } } fout=af_fopen(useFileName,"wb"); if (quiet == 0) u_printf("extracting %s to %s...",filename_inzip,useFileName); if (fout==NULL) { error("error opening %s\n",useFileName); } if (provis_concat_name != NULL) free(provis_concat_name); /* 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'; mkDirPortable(write_filename); *(filename_withoutpath-1)=c; fout=af_fopen(write_filename,"wb"); } if (quiet == 0) u_printf(" done\n"); } if (fout!=NULL) { af_setsizereservation(fout, (long)file_info.uncompressed_size); do { err = unzReadCurrentFile(uf,buf,(unsigned)size_buf); if (err<0) { error("error %d with zipfile in unzReadCurrentFile\n",err); break; } if (err>0) if (af_fwrite(buf,err,1,fout)!=1) { error("error in writing extracted file\n"); err=UNZ_ERRNO; break; } } while (err>0); if (fout) af_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) { error("error %d with zipfile in unzCloseCurrentFile\n",err); } } else unzCloseCurrentFile(uf); /* don't lose the error */ if (previousPathCreated != NULL) free(previousPathCreated); } free(buf); return err; }
/** * Returns 1 if the given file exists and can be read; 0 otherwise. */ int fexists(const char* name) { ABSTRACTFILE* f=af_fopen(name,"rb"); if (f==NULL) return 0; af_fclose(f); return 1; }