decompress.c

来自「此为压缩与解压缩程序的文本文件,里面是用C语言编写的程序源代码.此程序运用了hu」· C语言 代码 · 共 105 行

C
105
字号
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
   unsigned long  weight;
   short left, right, parent;
   unsigned short bit_code;
   short code_length;
}hufnode;

hufnode huftree[511];

    FILE*infile,*outfile;
    unsigned long total;
    char *infile_name;
    char origfile_name[20];
    short root_pos;

int main(int argc, char *argv[])
{
    short read_header();
    short isleaf(short);
    infile_name=argv[1];

    if ((infile=fopen(infile_name, "rb")) == NULL)
    {
        printf("Unable to open input file %s.\n", infile_name);
        return 1;
    }

    read_header();

    if ((outfile=fopen(origfile_name, "wb")) == NULL)
    {
        printf("Unable to open output file %s.\n",origfile_name);
        return 1;
    }

    unsigned long count=0;
    short current_pos=root_pos;
    char ch;
    while(!feof(infile))
    {
        ch=fgetc(infile);
        short i;
        for(i=0; i<8; i++)
        {
            if((ch & 0x80) != 0)
                current_pos = huftree[current_pos].left;
            else
                current_pos = huftree[current_pos].right;
            if(isleaf(current_pos))   // 已到叶子结点,输出一个字符
            {
                fputc(current_pos, outfile);
                count++;
                if(count == total)
                    return;
                current_pos = root_pos;
            }
            ch <<= 1;
        }
    }
    fclose(infile);
    fclose(outfile);
  system("PAUSE");
  return 0;
}

short read_header()
{
    if(fread((void *)origfile_name,sizeof(char),20,infile) <20)
    {
        printf("Unable to read original filename from %s\n",infile_name);
        return 1;
    }
    printf("原文件为%s\n",origfile_name);
    if(fread((void *)&total,sizeof(unsigned long),1,infile) <1)
    {
        printf("Unable to read original byte count from %s\n",infile_name);
        return 1;
    }
    printf("原文件大小为%d\n",total);
    if(fread((void *)&root_pos,sizeof(short),1,infile) <1)
    {
        printf("Unable to read root_pos from %s\n",infile_name);
        return 1;
    }
    if(fread((void *)huftree,sizeof(hufnode),511,infile) < 511)
    {
        printf("Unable to read huftable from %s\n",infile_name);
        return 1;
    }
    return 0;
}

short isleaf(short i)
{
    if(huftree[i].parent!=-1&&huftree[i].left==-1&&huftree[i].right==-1)
    {
        return 1;
    }
    return 0;
}

⌨️ 快捷键说明

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