forked from 18309220622/item
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileCompress.h
324 lines (286 loc) · 6.43 KB
/
FileCompress.h
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
323
324
#pragma once
#include"HuffmanTree.h"
#include<assert.h>
#include<algorithm>
#include<string>
typedef long long LongType;
struct CharInfo
{
unsigned char _ch; //出现的字符
LongType _count; //字符个数
string _code; //HuffmanCode
CharInfo(LongType count=0)
:_ch(0)
,_count(count) //_code不需用初始化,string会调用默认的构造函数
{}
CharInfo operator+(const CharInfo& c)
{
CharInfo tmp;
tmp._count=_count+c._count;
return tmp;
}
bool operator != (const CharInfo c)
{
return _count != c._count;
}
bool operator < (const CharInfo& c)const
{
return _count < c._count;
}
};
class FileCompress
{
public:
FileCompress()
{
for (int i=0;i<256;++i)
{
_str[i]._ch=i;
//在文本文件中,数据是以字符的ASCII码的形式存放,
//ASCII码的范围是0-255,所以文件压缩中用元素个数
//为256的数组作为底层数据结构,其中元素类型为CharInfo
//包括字符,字符出现的次数,字符编码;
}
}
void Compress(const char* FileName)
{
assert(FileName);
FILE* fout=fopen(FileName,"rb"); //以二进制形式打开,否则读不到汉字
assert(fout);
//unsigned char ch=fgetc(fout);
int ch=fgetc(fout);
while(!feof(fout))
{
_str[ch]._count++; //统计各种字符在文件中出现的次数
ch=fgetc(fout);
}
//根据统计的次数作为权值构建哈弗曼树
//count=0的相当于一个非法值,不用构建到huffmantree中
CharInfo invalid(0);
HuffmanTree<CharInfo> hf(_str,256,invalid);
//生成叶子节点所对应的编码
string code;
_GenerateHuffmanCode(hf.GetRoot(),code);
//将编码写入压缩文件中
string CompressFileName=FileName;
CompressFileName+=".compress";
FILE* Input=fopen(CompressFileName.c_str(),"wb");
assert(Input);
fseek(fout,0,SEEK_SET);
char Inch=0; //要写入压缩文件的编码
int size=0;
ch=fgetc(fout);
while (!feof(fout))
{
string& code=_str[ch]._code;
for (size_t i=0;i < code.size();++i)
{
Inch<<=1;
if (code[i]=='1')
{
Inch |= 1;
}
++size;
if (size==8)
{
fputc(Inch,Input); //满8个字节就写入压缩文件
size=0;
Inch=0;
}
}
ch=fgetc(fout);
}
//最后不满8个字节的单独处理
if (size>0)
{
Inch<<= 8-size;
fputc(Inch,Input);
}
fclose(fout); //关闭源文件
fclose(Input); //关闭压缩文件
//写配置文件
//格式如:
//a,3
//b,2
string ConfigFileName=FileName;
ConfigFileName+=".config";
FILE* finConfig=fopen(ConfigFileName.c_str(),"wb");
string line;
for (size_t i=0;i<256;++i)
{
if (_str[i]._count!=0)
{
line += _str[i]._ch;
line += ',';
char buff[25]={0}; //将次数转换成字符串存储
line += _itoa(_str[i]._count,buff,10); //10为保存到字符串中的数据的进制基数
line +='\n';
fwrite(line.c_str(),1,line.size(),finConfig);
//fputs(line.c_str(),finConfig);
line.clear();
}
}
fclose(finConfig); //关闭配置文件
}
//解压缩文件
void Uncompress(const char* filename)
{
//先从配置文件中读出各种字符出现的个数
string ConfigFile = filename;
ConfigFile += ".config";
FILE* foutconfig = fopen(ConfigFile.c_str(), "rb");
assert(foutconfig);
string line;
while (ReadLine(foutconfig,line))
{
//空行情况
if(line.empty())
{
line += '\n';
continue;
}
else
{
unsigned char ch=line[0]; //压缩图片时崩溃,调试发现有负数情况要用unsigned char
//char ch=line[0];
//使用string::substr(pos)函数提取字符出现的次数
_str[ch]._count = atoi(line.substr(2).c_str());
line.clear();
}
}
//重新构建Huffman树
CharInfo invalid(0);
HuffmanTree<CharInfo> hf(_str,256,invalid);
//开始解压缩
string uncompressfilename=filename;
uncompressfilename += ".uncompress";
FILE *fin=fopen(uncompressfilename.c_str(),"wb");
assert(fin);
//先读取压缩文件
string compressfilename=filename;
compressfilename += ".compress";
FILE *fout=fopen(compressfilename.c_str(),"rb");
assert(fout);
//根结点的权值是字符出现的次数总和
HuffmanTreeNode<CharInfo> *root=hf.GetRoot();
LongType chLen=root->_weight._count;
HuffmanTreeNode<CharInfo> *cur=root;
//int pos=8;
int pos=7;
//unsigned char ch=fgetc(fout);
int ch=fgetc(fout);
while (chLen>0)
{
//--pos;
if (ch & (1 << pos)) //检查字符的每个位
cur=cur->_right;
else
cur=cur->_left; //0左,1右
--pos;
if(cur->_left == NULL && cur->_right == NULL)
{
fputc(cur->_weight._ch,fin); //将对应字符写入解压缩文件中
cur=root; //一条路径走完再次回到根节点
--chLen;
if (chLen == 0) //所有的字符都处理完成
break;
}
//if(pos == 0)
//{
// ch=fgetc(fout); //一个字节解压完成,再次获取
// pos=8;
//}
if(pos==-1)
{
ch=fgetc(fout);
pos=7;
}
}
fclose(foutconfig);
fclose(fin);
fclose(fout);
}
protected:
void _GenerateHuffmanCode(HuffmanTreeNode<CharInfo>* root,string& code)
{
if (NULL==root)
{
return;
}
if (root->_left==NULL && root->_right==NULL)
{
_str[root->_weight._ch]._code=code;
}
_GenerateHuffmanCode(root->_left,code+'0');
_GenerateHuffmanCode(root->_right,code+'1');
}
bool ReadLine(FILE *fOut, string& line)
{
//unsigned char ch = fgetc(fOut);
int ch = fgetc(fOut);
if (feof(fOut))
return false;
while (!feof(fOut) && ch !='\n')
{
line += ch;
ch = fgetc(fOut);
}
return true;
}
protected:
CharInfo _str[256];
};
//string line;
// unsigned long long chLen = 0;
//
//
//
//
// //重建哈夫曼树
// CharInfo invalid;
// HuffManTree<CharInfo> Tree(_info, 256, invalid);
//
// //读取压缩文件
// string compressfilename = filename;
// compressfilename += ".HuffMan";
// FILE* FileCom = fopen(compressfilename.c_str(), "rb");
// assert(FileCom);
//
// string uncompressfilename = filename;
// uncompressfilename += ".uncomp";
// FILE* FileUnCom = fopen(uncompressfilename.c_str(), "wb");
// assert(FileUnCom);
//
// char ch = fgetc(FileCom);
// HuffManTreeNode<CharInfo>* root = Tree.GetRoot();
// HuffManTreeNode<CharInfo>* cur = root;
//
// int pos = 8;
// while (1)
// {
// if (cur->_left==NULL && cur->_right==NULL)
// {
// fputc(cur->_weight._ch, FileUnCom);
// cur = root;
// if (--chSize==0)
// {
// break;
// }
// }
// --pos;
// if (ch & (1<<pos))
// cur = cur->_right;
// else
// cur = cur->_left;
// if (pos==0)
// {
// ch = fgetc(FileCom);
// pos = 8;
// }
// }
//
// fclose(FileCom);
// fclose(FileUnCom);
// fclose(foutconfig);
// }
// }