⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decode.c

📁 用哈夫曼编码实现文件压缩和解压缩. 压缩过程的实现:1创建Haffman树&#61664 2打开需压缩文件&#61664 3将需压缩文件中的每个ascii码对应的haffman编码按bit单位输出&
💻 C
字号:
#include "ECBTree.h"
#include "MyAssert.h"
#include "Ulti.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LENGTH 128
#define DEBUG 1

char dotTxt[]=".txt";
char dotRer[]=".rer";
char cmpStr[]=".rer";

void main(int argc,char* argv[])
{
	//core data.
	long wList[LENGTH];
	PHtTree myHtTree;
	
	//file data.
	char inputFileName[LENGTH],outputFileName[LENGTH];
	FILE* inputFile,* outputFile,* keyFile;
	int fileNameLen;
	
	//binary opeartion data.
	char inData,outputData;
	unsigned	long curCode,tmpBinData;
	int curLen,realLen,curIndex;
	int i,nodeIndex,count;
	
		
	//open the files.
	if (argc<=1)
	{
		printf("please enter your file address.\n");
		return;
	}
	else 
	{
		//make up the files' name.
		strcpy(inputFileName,argv[1]);
		strcpy(outputFileName,argv[1]);
		
		fileNameLen=strlen(argv[1]);
		
		for(i=0;i<4;i++)
			cmpStr[i]=inputFileName[fileNameLen-4+i];
		cmpStr[4]='\0';

		if(strcmp(cmpStr,dotRer)!=0)
		{
			printf("file not recognized ,should be fileName.rer");
			return;
		}
		//only could zip the file appendix as .***,*** have to be 3 char length.
		outputFileName[fileNameLen-4]='\0';
		strcat(outputFileName,dotTxt);
		
		inputFileName[fileNameLen]='\0';
		outputFileName[fileNameLen+4]='\0';
		
		
		if((inputFile=fopen(inputFileName,"rb"))==NULL)
		{
			printf("file path not found\n");
			return;
		}
		
		if (DEBUG)
			printf("input file open success\n");
		/*outpout file open*/
		assertF((outputFile=fopen(outputFileName,"wb"))!=NULL,"output file error");
		
		if (DEBUG)
			printf("output file open success\n");
	}
	
	if((keyFile=fopen("KEY.txt","rb"))==NULL)
		{
			printf(">--keyFile not founded--<\n");
			return;
		}
	
	for(i=0;i<LENGTH-1;i++)
		fscanf(keyFile,"%d,",&wList[i]);
	fscanf(keyFile,"%d;",&wList[i]);
		
		
	myHtTree=haffmanAlgorithm(LENGTH,wList);
	
	
	//starting parse.
	count=8;
	nodeIndex=myHtTree->rootIndex;
	while(!feof(inputFile))
	{
				/*----------------------------*/
				if(count==8)
				{
					//1.get data.
				
					inData=fgetc(inputFile);//get 8 len bin haff code.
					
					if(inData==-1&&feof(inputFile))
								break;

			//		curIndex=7;
					count=0;
				}
				
				/*
					when its the child node search a unuseful step.
					  tmpBinData=(inData>>curIndex)&0x01;
				*/	
				if(myHtTree->ht[nodeIndex].llinkIndex==-1&&myHtTree->ht[nodeIndex].rlinkIndex==-1)
				{
					//send out data.
					fputc(myHtTree->ht[nodeIndex].info,outputFile);
					nodeIndex=myHtTree->rootIndex;
				}
				else
				{
					tmpBinData=(inData>>(7-count))&0x01;

					if(tmpBinData==0x00)
						nodeIndex=myHtTree->ht[nodeIndex].llinkIndex;
					else if(tmpBinData==0x01)
						nodeIndex=myHtTree->ht[nodeIndex].rlinkIndex;
					else 
						printf("error happen in read bin!\n");

				//	curIndex--;
					count++;
				}
	}		
	
	if(DEBUG)	
		printf("\ntest ends.\n");

	//step3:end of code.
	fclose(inputFile);	
	fclose(outputFile);
	getchar();
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -