1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h>
typedef struct { unsigned char uch; unsigned long weight; } TmpNode;
typedef struct { unsigned char uch; unsigned long weight; char *code; int parent, lchild, rchild; } HufNode, *HufTree;
void select(HufNode *huf_tree, unsigned int n, int *s1, int *s2) { unsigned int i; unsigned long min = ULONG_MAX; for(i = 0; i < n; ++i) if(huf_tree[i].parent == 0 && huf_tree[i].weight < min) { min = huf_tree[i].weight; *s1 = i; } huf_tree[*s1].parent=1; min=ULONG_MAX; for(i = 0; i < n; ++i) if(huf_tree[i].parent == 0 && huf_tree[i].weight < min) { min = huf_tree[i].weight; *s2 = i; } }
void CreateTree(HufNode *huf_tree, unsigned int char_kinds, unsigned int node_num) { unsigned int i; int s1, s2; for(i = char_kinds; i < node_num; ++i) { select(huf_tree, i, &s1, &s2); huf_tree[s1].parent = huf_tree[s2].parent = i; huf_tree[i].lchild = s1; huf_tree[i].rchild = s2; huf_tree[i].weight = huf_tree[s1].weight + huf_tree[s2].weight; } }
void HufCode(HufNode *huf_tree, unsigned char_kinds) { unsigned int i; int cur, next, index; char *code_tmp = (char *)malloc(256*sizeof(char)); code_tmp[256-1] = '\0'; for(i = 0; i < char_kinds; ++i) { index = 256-1; for(cur = i, next = huf_tree[i].parent; next != 0; cur = next, next = huf_tree[next].parent) if(huf_tree[next].lchild == cur) code_tmp[--index] = '0'; else code_tmp[--index] = '1'; huf_tree[i].code = (char *)malloc((256-index)*sizeof(char)); strcpy(huf_tree[i].code, &code_tmp[index]); } free(code_tmp); }
int compress(char *ifname, char *ofname) { unsigned int i, j; unsigned int char_kinds; unsigned char char_temp; unsigned long file_len = 0; FILE *infile, *outfile; TmpNode node_temp; unsigned int node_num; HufTree huf_tree; char code_buf[256] = "\0"; unsigned int code_len;
TmpNode *tmp_nodes =(TmpNode *)malloc(256*sizeof(TmpNode)); for(i = 0; i < 256; ++i) { tmp_nodes[i].weight = 0; tmp_nodes[i].uch = (unsigned char)i; } infile = fopen(ifname, "rb"); if (infile == NULL) return -1; fread((char *)&char_temp, sizeof(unsigned char), 1, infile); while(!feof(infile)) { ++tmp_nodes[char_temp].weight; ++file_len; fread((char *)&char_temp, sizeof(unsigned char), 1, infile); } fclose(infile); for(i = 0; i < 256-1; ++i) for(j = i+1; j < 256; ++j) if(tmp_nodes[i].weight < tmp_nodes[j].weight) { node_temp = tmp_nodes[i]; tmp_nodes[i] = tmp_nodes[j]; tmp_nodes[j] = node_temp; } for(i = 0; i < 256; ++i) if(tmp_nodes[i].weight == 0) break; char_kinds = i; if (char_kinds == 1) { outfile = fopen(ofname, "wb"); fwrite((char *)&char_kinds, sizeof(unsigned int), 1, outfile); fwrite((char *)&tmp_nodes[0].uch, sizeof(unsigned char), 1, outfile); fwrite((char *)&tmp_nodes[0].weight, sizeof(unsigned long), 1, outfile); free(tmp_nodes); fclose(outfile); } else { node_num = 2 * char_kinds - 1; huf_tree = (HufNode *)malloc(node_num*sizeof(HufNode)); for(i = 0; i < char_kinds; ++i) { huf_tree[i].uch = tmp_nodes[i].uch; huf_tree[i].weight = tmp_nodes[i].weight; huf_tree[i].parent = 0; } free(tmp_nodes); for(; i < node_num; ++i) huf_tree[i].parent = 0; CreateTree(huf_tree, char_kinds, node_num); HufCode(huf_tree, char_kinds); outfile = fopen(ofname, "wb"); fwrite((char *)&char_kinds, sizeof(unsigned int), 1, outfile); for(i = 0; i < char_kinds; ++i) { fwrite((char *)&huf_tree[i].uch, sizeof(unsigned char), 1, outfile); fwrite((char *)&huf_tree[i].weight, sizeof(unsigned long), 1, outfile); } fwrite((char *)&file_len, sizeof(unsigned long), 1, outfile); infile = fopen(ifname, "rb"); fread((char *)&char_temp, sizeof(unsigned char), 1, infile); while(!feof(infile)) { for(i = 0; i < char_kinds; ++i) if(char_temp == huf_tree[i].uch) strcat(code_buf, huf_tree[i].code); while(strlen(code_buf) >= 8) { char_temp = '\0'; for(i = 0; i < 8; ++i) { char_temp <<= 1; if(code_buf[i] == '1') char_temp = 1; } fwrite((char *)&char_temp, sizeof(unsigned char), 1, outfile); strcpy(code_buf, code_buf+8); } fread((char *)&char_temp, sizeof(unsigned char), 1, infile); } code_len = strlen(code_buf); if(code_len > 0) { char_temp = '\0'; for(i = 0; i < code_len; ++i) { char_temp <<= 1; if(code_buf[i] == '1') char_temp = 1; } char_temp <<= 8-code_len; fwrite((char *)&char_temp, sizeof(unsigned char), 1, outfile); } fclose(infile); fclose(outfile); for(i = 0; i < char_kinds; ++i) free(huf_tree[i].code); free(huf_tree); } }
int extract(char *ifname, char *ofname) { unsigned int i; unsigned long file_len; unsigned long writen_len = 0; FILE *infile, *outfile; unsigned int char_kinds; unsigned int node_num; HufTree huf_tree; unsigned char code_temp; unsigned int root; infile = fopen(ifname, "rb"); if (infile == NULL){ return -1; } fread((char *)&char_kinds, sizeof(unsigned int), 1, infile); if (char_kinds == 1) { fread((char *)&code_temp, sizeof(unsigned char), 1, infile); fread((char *)&file_len, sizeof(unsigned long), 1, infile); outfile = fopen(ofname, "wb"); while (file_len--) fwrite((char *)&code_temp, sizeof(unsigned char), 1, outfile); fclose(infile); fclose(outfile); } else { node_num = 2 * char_kinds - 1; huf_tree = (HufNode *)malloc(node_num*sizeof(HufNode)); for(i = 0; i < char_kinds; ++i) { fread((char *)&huf_tree[i].uch, sizeof(unsigned char), 1, infile); fread((char *)&huf_tree[i].weight, sizeof(unsigned long), 1, infile); huf_tree[i].parent = 0; } for(; i < node_num; ++i) huf_tree[i].parent = 0; CreateTree(huf_tree, char_kinds, node_num); fread((char *)&file_len, sizeof(unsigned long), 1, infile); outfile = fopen(ofname, "wb"); root = node_num-1; while(1) { fread((char *)&code_temp, sizeof(unsigned char), 1, infile); for(i = 0; i < 8; ++i) { if(code_temp & 128) root = huf_tree[root].rchild; else root = huf_tree[root].lchild; if(root < char_kinds) { fwrite((char *)&huf_tree[root].uch, sizeof(unsigned char), 1, outfile); ++writen_len; if (writen_len == file_len) break; root = node_num-1; } code_temp <<= 1; } if (writen_len == file_len) break; } fclose(infile); fclose(outfile); free(huf_tree); } } int main() { while(1) { printf("朱安杰 194020215\n"); int opt, flag = 0; char ifname[256], ofname[256]; printf("请输入操作所对应的数字:\n 1: 压缩\n 2: 解压\n 3: 退出\n"); scanf("%d", &opt); if (opt == 3) break; else { printf("请输入输入文件名: "); fflush(stdin); gets(ifname); printf("请输入输出文件名: "); fflush(stdin); gets(ofname); } switch(opt) { case 1: printf("压缩中……\n"); flag = compress(ifname, ofname); break; case 2: printf("解压中……\n"); flag = extract(ifname, ofname); break; } if (flag == -1) printf("抱歉, 文件 \"%s\" 不存在!\n", ifname); else printf("操作完成!\n"); } return 0;
|