1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include<time.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/stat.h> #include <string.h> #define BUF_LEN 100 #define PERSON_BUF_LEN 300 #define FILE_NAME_LEN 300
struct FileStruct { char fileName[300]; long long fileSize; }; long long getFileSize(char * fileName){ FILE* fp=fopen(fileName,"r"); fseek(fp, 0, SEEK_END); size_t size = ftell(fp); fclose(fp); return size; } char * getFileName(char *pathName){ while(*pathName!='\0')pathName++; while(*pathName!='/')pathName--; return pathName+1; }
void Unpack(struct FileStruct* fileStruct,char* file_content,char* folder_name){ char* Full_name[FILE_NAME_LEN]; sprintf(Full_name,"%s/%s",folder_name,fileStruct->fileName);
FILE* file=fopen(Full_name,"wb"); fwrite(file_content,sizeof(char),fileStruct->fileSize,file); return; }
void add_suffix(char* new_filename,int occur_time,char* old_filename){ char* tem=old_filename; while(*tem!='\0')tem++; while(*tem!='.'&&tem!=old_filename)tem--; if(tem!=old_filename){ *tem='\0'; sprintf(new_filename,"%s(%d).%s",old_filename,occur_time,tem+1); *tem='.'; return; } else{ sprintf(new_filename,"%s(%d)",old_filename,occur_time); return; } }
char text_buf[BUF_LEN]; char person_buf[PERSON_BUF_LEN]; char file_NAME[FILE_NAME_LEN]; char dest_folder[FILE_NAME_LEN]; char* file_content; char file_names[BUF_LEN][FILE_NAME_LEN]; int occur_times[BUF_LEN]; int file_name_count=0;
int main(void){ printf("输入要目标文件夹:"); scanf("%s",dest_folder); printf("输入要解压的文件:"); scanf("%s",file_NAME); FILE* f=fopen(file_NAME,"rb"); struct FileStruct fileStruct; while(fread(&fileStruct,sizeof(struct FileStruct),1,f)!=0){ file_content=(char*)malloc(fileStruct.fileSize); printf("%d",fileStruct.fileSize); fread(file_content,sizeof(char),fileStruct.fileSize,f); int ok=0; for(int i=0;i<file_name_count;i++){ if(strcmp(file_names[i],fileStruct.fileName)==0){ occur_times[i]++; int occur_time=occur_times[i]; add_suffix(file_NAME,occur_time,fileStruct.fileName); strcpy(fileStruct.fileName,file_NAME); ok=1; break; } } if(ok==0){ strcpy(file_names[file_name_count],fileStruct.fileName); occur_times[file_name_count++]++; } Unpack(&fileStruct,file_content,dest_folder); }
}
|