《用哈夫曼树实现压缩解压【仅供参考】》由会员分享,可在线阅读,更多相关《用哈夫曼树实现压缩解压【仅供参考】(29页珍藏版)》请在金锄头文库上搜索。
1、用哈夫曼树实现压缩解压程序是用VC+6.0编译完成的,可以完成对任意文件的压缩解压(为方便寻找,压缩出的文件与待压缩文件在同一文件夹中),但压缩文件夹还不可以,另外该程序还能打印出压缩时所建立的哈夫曼树及哈夫曼编码。源代码如下:#include #include #include #include typedef struct node long w; short p,l,r; htnode,*htnp;typedef struct huffman_code unsigned char len; unsigned char *codestr; hufcode;typedef char *huf
2、fmancode;int initial_files(char *source_filename,FILE *inp,char *obj_filename,FILE *outp);char *create_filename(char *source_filename,char* obj_filename);int compress(char *source_filename,char *obj_filename);long frequency_data(FILE *in,long fre);int search_set(htnp ht,int n,int *s1, int *s2);int c
3、reate_hftree(long w,int n,htnode ht);int encode_hftree(htnp htp,int n,hufcode hc);unsigned char chars_to_bits(const unsigned char chars8);int write_compress_file(FILE *in,FILE *out,htnp ht,hufcode hc,char* source_filename,long source_filesize);int decompress(char *source_filename,char *obj_filename)
4、;void get_mini_huffmantree(FILE* in,short mini_ht2);int write_decompress_file(FILE *in,FILE* out,short mini_ht2,long bits_pos,long obj_filesize);int d_initial_files(char *source_filename,FILE *inp,char *obj_filename,FILE *outp);main()int s;char filename10;system(color 3F);printf( *n);printf( * 菜单: *
5、n);printf( * 1.压缩 *n);printf( * 2.-解压缩- *n);printf( * 0.退出 *n);printf( *n);scanf(%d,&s);while(s!=0)getchar();switch(s)case 1:puts(请输入待压缩文件路径:);gets(filename);compress(filename,NULL);break;case 2:puts(请输入待解压文件路径:);gets(filename);decompress(filename,NULL);break;default : printf(指令错误!请重新输入指令:n);puts( )
6、;printf( *n);printf( * 菜单: *n);printf( * 1.压缩 *n);printf( * 2.-解压缩- *n);printf( * 0.退出 *n);printf( *n);scanf(%d,&s);int initial_files(char *source_filename,FILE *inp,char *obj_filename,FILE *outp) if(fopen(source_filename,rb)=NULL) return -1; if(obj_filename=NULL) if(obj_filename=(char*)malloc(256*s
7、izeof(char)=NULL) return -1; create_filename(source_filename,obj_filename); if(strcmp(source_filename,obj_filename)=0) return -1; printf(待压缩文件:%s,压缩文件:%sn,source_filename,obj_filename); if(*outp=fopen(obj_filename,wb)=NULL) return -1; if(*inp=fopen(source_filename,rb)=NULL) return -1; free(obj_filen
8、ame); return 0; char *create_filename(char *source_filename,char* obj_filename) char *temp; if(temp=strrchr(source_filename,.)=NULL) strcpy(obj_filename,source_filename); strcat(obj_filename,.zip); else strncpy(obj_filename,source_filename,temp-source_filename); obj_filenametemp-source_filename=0; s
9、trcat(obj_filename,.zip); return obj_filename;int compress(char *source_filename,char *obj_filename) FILE *in,*out;char ch; int error_code,i,j; float compress_rate; hufcode hc256; htnode ht256*2-1; long frequency256,source_filesize,obj_filesize=0; error_code=initial_files(source_filename,&in,obj_fil
10、ename,&out); if(error_code!=0) puts(文件打开失败!请重新输入文件路径:); return error_code; source_filesize=frequency_data(in,frequency); printf(文件大小 %ld 字节n,source_filesize); error_code=create_hftree(frequency,256,ht); if(error_code!=0) puts(建立哈夫曼树失败!); return error_code; error_code=encode_hftree(ht,256,hc); if(err
11、or_code!=0) puts(建立哈夫曼编码失败!); return error_code; for(i=0;i256;i+) obj_filesize+=frequencyi*hci.len; obj_filesize=obj_filesize%8=0?obj_filesize/8:obj_filesize/8+1; for(i=0;i256-1;i+) obj_filesize+=2*sizeof(short); obj_filesize+=strlen(source_filename)+1; obj_filesize+=sizeof(long); obj_filesize+=sizeof(unsigned int); compress_rate=(float)obj_filesize/source_filesize; printf(压缩文件