time 
设为首页】【收藏本站
当前位置: 主页 > 程序设计 > C\C++\VC > C语言 > C语言编写程序实现Zip或者Rar无损压缩

C语言编写程序实现Zip或者Rar无损压缩

时间:2009-09-20 23:31 点击:1263次 字体:[ ]




 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DNUM 64  //define data number 8*8
#define LOOP 10000 //times of compression
typedef struct
{
  unsigned short weight, data;
  unsigned short parent, lchild, rchild;
} HuffNode;
typedef struct
{
  unsigned char code;
  unsigned short codelength;
} HuffCode;
unsigned int fCount[256] = {0};
unsigned int data_num;
unsigned int code_size;
unsigned int last_bit;
void FrequencyCount(unsigned char*);     //频率统计
void HuffSelect(HuffNode*, int, int*, int*); //从结点中选出权最小的两个节点
void HuffmanCodeTable(HuffNode*, HuffCode*); //构造huffman树,生成huffman编码表
void HuffmanCompress(unsigned char*, unsigned char *, HuffCode*); //压缩数据
void BitPrint(unsigned char*);        //按位打印结果,用于调试
void main()
{
  int i, j, loop;                //variable for loop
  HuffNode hfdata[2*DNUM] = {{0, 0, 0, 0, 0}}; //Huffman node
  HuffCode code_table[256] = {{0, 0}};     //code table will be searched by subscript
  unsigned char hfcode[2*DNUM];         //output code
  time_t time1, time2;
/* unsigned char pixel[DNUM] = {
  1,2,3,4, 1,2,3,4, 1,2,3,4, 1,1,1,1};
*/
/* unsigned char pixel[DNUM] = {
    139,144,149,153,155,155,155,155,
    144,151,153,156,159,156,156,156,
    150,155,160,163,158,156,156,156,
    159,161,162,160,160,159,159,159,
    159,160,161,162,162,155,155,155,
    161,161,161,161,160,157,157,157,
    162,162,161,163,162,157,157,157,
    162,162,161,161,163,158,158,158};
*/
  unsigned char pixel[DNUM] = { //random data
    141, 101, 126, 111, 163, 112, 133, 156,
    103, 144, 111, 176, 117, 120, 188, 187,
    175, 164, 190, 156, 112, 179, 142, 119,
    140, 111, 127, 186, 196, 190, 189, 127,
    185, 103, 185, 110, 192, 139, 159, 104,
    151, 193, 178, 198, 114, 170, 179, 149,
    124, 149, 165, 108, 141, 176, 113, 164,
    101, 140, 120, 126, 173, 189, 158, 184};
/* unsigned char pixel[DNUM] = {
    202, 221, 159, 183, 41, 136, 247, 66,
    146, 29, 101, 108, 45, 61, 210, 236,
    90, 130, 54, 66, 132, 206, 119, 232,
    184, 135, 96, 78, 120, 41, 231, 203,
    150, 94, 172, 142, 122, 180, 150, 204,
    232, 121, 180, 221, 3, 207, 115, 147,
    72, 149, 169, 121, 76, 208, 235, 43,
    107, 58, 0, 237, 197, 7, 210, 89};
*/
  FrequencyCount(pixel);
  time1 = time(NULL);
  for (loop=0; loop<LOOP; loop++) {
    //set huffman nodes data and weight, i=0:255, j=1:64
    for (i=0, j=1, data_num=0; i<256; i++) {
      if (fCount[i]) {
        hfdata[j].weight = fCount[i];
        hfdata[j++].data = i;
        data_num ++;
      }
    }
    //build huffman tree and generate huffman code table
    HuffmanCodeTable(hfdata, code_table);
    //compress source data to huffman code using code table
    HuffmanCompress(pixel, hfcode, code_table);
    //initial hfdata and code_table
    for (j=0; j<2*DNUM; j++) {
      hfdata[j].data=0;
      hfdata[j].lchild=0;
      hfdata[j].parent=0;
      hfdata[j].rchild=0;
      hfdata[j].weight=0;
    }
  }
  time2 = time(NULL);
  //conclude
  printf("n哈夫曼编码压缩图块,压缩报告n华中科技大学力学系:李美之n");
  printf("n◎源数据(%d字节):n ", DNUM);
  for (i=0; i<DNUM; i++) {
    printf(i%8==7 ? "%02Xn " : "%02X ", pixel[i]);
  }
  printf("n◎压缩数据(%d字节):n ", code_size);
  for (i=0; i<code_size; i++) {
    printf(i%8==7 ? "%02Xn " : "%02X ", hfcode[i]);
  }
  //打印码表
  printf("nn◎码表-编码字典(%d项)n", data_num);
  for (i=0; i<256; i++) {
    if (code_table[i].codelength) {
      printf("%3d|%02X: ", i, i);
      for (j=0; j<code_table[i].codelength; j++) {
        printf("%d", ((code_table[i].code << j)&0x80)>>7);
      }
      printf("t");
    }
  }
  printf("nn◎压缩率:%2.0f%% t压缩时间:%.3f毫秒n",(float)code_size/DNUM * 100, 1E3*(time2-time1)/LOOP);
}
void BitPrint(unsigned char *hfcode)
{
  int i, j;
  int endbit = last_bit;
  unsigned char thebyte;
  for (i=0; i < code_size-1; i++) {
    thebyte = hfcode[i];
    for (j=0; j<8; j++) {
      printf("%d", ((thebyte<<j)&0x80)>>7);
    }
  }
  if (last_bit == 7) {
    endbit = -1;
  }
  thebyte = hfcode[i];
  for (j=7; j>endbit; j--) {
    printf("%d", ((thebyte<<(7-j))&0x80)>>7);
  }
}

 



本文地址 : http://www.fengfly.com/plus/view-77337-1.html
标签: ZIP 无损压缩 rar C编程
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:
本栏分类