用c语言写一个哈夫曼编码实现文件压缩与解压
时间: 2024-03-19 10:45:55 浏览: 18
哈夫曼编码是一种无损压缩算法,可以将输入的数据流进行压缩,减小数据存储空间,提高数据传输效率。以下是使用 C 语言实现哈夫曼编码的程序,包括文件压缩和解压两个部分。
#### 哈夫曼编码压缩
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NODES 256
typedef struct {
unsigned char ch;
int freq;
} Node;
typedef struct {
int left_child;
int right_child;
int parent;
int weight;
} HuffmanNode;
void count_frequency(char *filename, int *freq) {
FILE *fp;
unsigned char ch;
if ((fp = fopen(filename, "rb")) == NULL) {
printf("Cannot open file %s\n", filename);
return;
}
while (fread(&ch, sizeof(unsigned char), 1, fp) == 1) {
freq[ch]++;
}
fclose(fp);
}
int select_min(HuffmanNode *huffman_tree, int n, int *min1, int *min2) {
int i, cnt = 0;
for (i = 0; i < n; i++) {
if (huffman_tree[i].parent == -1) cnt++;
}
if (cnt < 2) return 0;
*min1 = -1, *min2 = -1;
for (i = 0; i < n; i++) {
if (huffman_tree[i].parent == -1) {
if (*min1 == -1 || huffman_tree[i].weight < huffman_tree[*min1].weight) {
*min2 = *min1;
*min1 = i;
} else if (*min2 == -1 || huffman_tree[i].weight < huffman_tree[*min2].weight) {
*min2 = i;
}
}
}
return 1;
}
int build_huffman_tree(int *freq, int n, HuffmanNode *huffman_tree) {
int i, j, min1, min2;
for (i = 0; i < n; i++) {
huffman_tree[i].left_child = -1;
huffman_tree[i].right_child = -1;
huffman_tree[i].parent = -1;
huffman_tree[i].weight = freq[i];
}
for (i = n; i < 2 * n - 1; i++) {
if (select_min(huffman_tree, i, &min1, &min2) == 0) break;
huffman_tree[min1].parent = i;
huffman_tree[min2].parent = i;
huffman_tree[i].left_child = min1;
huffman_tree[i].right_child = min2;
huffman_tree[i].weight = huffman_tree[min1].weight + huffman_tree[min2].weight;
}
return i;
}
int encode(char *filename, char *filename_out, HuffmanNode *huffman_tree, int n) {
FILE *fp_in, *fp_out;
unsigned char ch, byte = 0;
int i, bit_cnt = 0;
if ((fp_in = fopen(filename, "rb")) == NULL) {
printf("Cannot open file %s\n", filename);
return 0;
}
if ((fp_out = fopen(filename_out, "wb")) == NULL) {
printf("Cannot open file %s\n", filename_out);
return 0;
}
while (fread(&ch, sizeof(unsigned char), 1, fp_in) == 1) {
i = n - 1;
while (i >= 0) {
if (huffman_tree[i].left_child != -1 && huffman_tree[huffman_tree[i].left_child].weight <= bit_cnt) {
byte |= (1 << (7 - bit_cnt));
i = huffman_tree[i].left_child;
bit_cnt++;
} else if (huffman_tree[i].right_child != -1 && huffman_tree[huffman_tree[i].right_child].weight <= bit_cnt) {
i = huffman_tree[i].right_child;
bit_cnt++;
} else {
break;
}
if (bit_cnt == 8) {
fwrite(&byte, sizeof(unsigned char), 1, fp_out);
byte = 0;
bit_cnt = 0;
}
}
}
if (bit_cnt > 0) {
fwrite(&byte, sizeof(unsigned char), 1, fp_out);
}
fclose(fp_in);
fclose(fp_out);
return 1;
}
int decode(char *filename_out, char *filename, HuffmanNode *huffman_tree, int root, int size) {
FILE *fp_in, *fp_out;
unsigned char ch, byte = 0;
int i, bit_cnt = 0, node = root;
if ((fp_in = fopen(filename_out, "rb")) == NULL) {
printf("Cannot open file %s\n", filename_out);
return 0;
}
if ((fp_out = fopen(filename, "wb")) == NULL) {
printf("Cannot open file %s\n", filename);
return 0;
}
while (fread(&ch, sizeof(unsigned char), 1, fp_in) == 1) {
for (i = 0; i < 8; i++) {
if (ch & (1 << (7 - i))) {
node = huffman_tree[node].left_child;
} else {
node = huffman_tree[node].right_child;
}
if (node < size && node >= 0) {
fwrite(&huffman_tree[node].weight, sizeof(unsigned char), 1, fp_out);
node = root;
}
}
}
fclose(fp_in);
fclose(fp_out);
return 1;
}
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage: %s [-c/-d] input_file output_file\n", argv[0]);
return 0;
}
if (strcmp(argv[1], "-c") == 0) {
int freq[MAX_NODES] = {0};
count_frequency(argv[2], freq);
HuffmanNode huffman_tree[2 * MAX_NODES - 1];
int root = build_huffman_tree(freq, MAX_NODES, huffman_tree);
encode(argv[2], argv[3], huffman_tree, root);
} else if (strcmp(argv[1], "-d") == 0) {
int freq[MAX_NODES] = {0};
count_frequency(argv[3], freq);
HuffmanNode huffman_tree[2 * MAX_NODES - 1];
int root = build_huffman_tree(freq, MAX_NODES, huffman_tree);
decode(argv[2], argv[3], huffman_tree, root, MAX_NODES);
} else {
printf("Usage: %s [-c/-d] input_file output_file\n", argv[0]);
return 0;
}
return 0;
}
```
程序中首先定义了两个结构体 `Node` 和 `HuffmanNode`,分别用于存储字符频率和哈夫曼树节点信息。在 `count_frequency` 函数中,程序读入输入文件中的每个字符,并统计每个字符出现的频率,存储在 `freq` 数组中。接着,在 `build_huffman_tree` 函数中,程序构建哈夫曼树,并返回根节点的位置。在构建哈夫曼树的过程中,程序使用了 `select_min` 函数来选择权值最小的两个节点进行合并。最后,在 `encode` 函数中,程序读入输入文件中的每个字符,并使用哈夫曼树进行编码,并将编码后的结果输出到输出文件中。
#### 哈夫曼编码解压
在解压过程中,程序使用 `decode` 函数来读入输入文件中的每个编码,并使用哈夫曼树进行解码,并将解码后的结果输出到输出文件中。需要注意的是,在解码过程中,程序需要记录当前所在的哈夫曼树节点位置,以便进行下一位的解码。
以上是使用 C 语言实现的哈夫曼编码程序,包括文件压缩和解压两个部分。