-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPack_files.cpp
More file actions
255 lines (210 loc) · 5.25 KB
/
Pack_files.cpp
File metadata and controls
255 lines (210 loc) · 5.25 KB
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
#include "StdAfx.h"
#include "Pack_files.h"
Pack_file::Pack_file(void)
{
memset(&fh, 0x0, sizeof(fh));
memset(ObjectFilePathName, 0x0, sizeof(ObjectFilePathName));
}
Pack_file::~Pack_file(void)
{
}
void Pack_file:: SetfilePath(char * filePutPath)
{
strcpy(ObjectFilefoldPath, filePutPath);
}
//遍历文件夹下的文件名列表(包括嵌套文件夹)
void Pack_file::get_filelist(char *foldname)
{
HANDLE hFind;
WIN32_FIND_DATAA fileData;
string line;
char fn[MAX_PATH];
char tmpfn[MAX_PATH];
strcpy_s(fn, foldname);
//需要对文件夹名的字符串进行处理
if (fn[strlen(fn) - 1] != '\\')
{
strcat_s(fn, "\\");
}
//留意顺序,此时fn已加入"\\"
strcpy(tmpfn, fn);
//不加*会出错!
strcat_s(fn, "*");
hFind = FindFirstFileA(fn, &fileData);
FindNextFile(hFind, &fileData);
while (FindNextFile(hFind, &fileData))
{
//如果扫描到的当前为文件夹
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (fileData.cFileName[0] != '.')
{
char szFile[MAX_PATH];
strcpy_s(szFile, tmpfn);
strcat_s(szFile, fileData.cFileName);
get_filelist(szFile);
}
}
//扫描到文件
else
{
line = (string)tmpfn;
line += fileData.cFileName;
/*if (line.find(".h",0)!=string::npos)
{
filelist.push_back(line);
}
else
{
continue;
}*/
filelist.push_back(line);
}
//cout<<line<<endl;
}
}
//添加文件到包内
void Pack_file::AddFile()
{
vector<string>::iterator itemFileName;
//添加要打包的文件
for (itemFileName = filelist.begin(); itemFileName < filelist.end(); itemFileName++)
{
if (fh.FileCount >= MAX_FILE_COUNT - 1)
{
cout << "最多支持" << MAX_FILE_COUNT << "个文件" << endl;
return;
}
strcpy_s(fh.FileName[fh.FileCount], (*itemFileName).c_str());
fh.FileCount++;
}
}
//设置打包输出文件
void Pack_file::SetOutPutFile(char * OutFile)
{
memset(ObjectFilePathName, 0x0, sizeof(ObjectFilePathName));
strcpy_s(ObjectFilePathName, OutFile);
}
//获取文件大小(传入以二进制方式打开的文件指针)
long Pack_file::GetFileSize(FILE *pf)
{
//指针移到文件尾
fseek(pf, 0,/*SEEK_END*/ 2);
return ftell(pf);
}
//制作打包文件
void Pack_file::DoMakeCAB()
{
if (fh.FileCount < 1)
{
cout << "没有文件添加到打包" << endl;
return;
}
if (strlen(ObjectFilePathName) < 1)
{
cout << "没有指定打包文件输出位置" << endl;
return;
}
FILE *pOutFile = NULL;
FILE *pWorkFile = NULL;
//获取所有文件大小
for (int i = 0; i < fh.FileCount; i++)
{
pWorkFile = fopen(fh.FileName[i], "rb");
if (NULL == pWorkFile)
{
cout << "文件:" << fh.FileName[i] << "无法读取[" << strerror(errno) << "]" << endl;
return;
}
fh.FileLen[i] = GetFileSize(pWorkFile);
fclose(pWorkFile);
}
//检查是否有对应的文件夹
CheckTargetPath(ObjectFilePathName);
//开始合并写文件
pOutFile = fopen(ObjectFilePathName, "wb");
if (NULL == pOutFile)
{
cout << "输出文件创建失败[" << strerror(errno) << "]" << endl;
return;
}
//写入各文件
for (int i = 0; i < fh.FileCount; i++)
{
unsigned char *pTmpData = NULL;
//cout << "fh.FileName: " << fh.FileName[i] << endl;
pWorkFile = fopen(fh.FileName[i], "rb");
if (NULL == pWorkFile)
{
cout << "文件:" << fh.FileName[i] << "无法读取[" << strerror(errno) << "]" << endl;
fclose(pWorkFile);
fclose(pOutFile);
return;
}
pTmpData = new unsigned char[fh.FileLen[i]];
fread(pTmpData, fh.FileLen[i], 1, pWorkFile);
if (ferror(pWorkFile))
{
cout << "文件:" << fh.FileName[i] << "无法读取[" << strerror(errno) << "]" << endl;
fclose(pWorkFile);
fclose(pOutFile);
return;
}
fwrite(pTmpData, fh.FileLen[i], 1, pOutFile);
if (ferror(pOutFile))
{
cout << "文件:" << ObjectFilePathName << "无法写入[" << strerror(errno) << "]" << endl;
fclose(pWorkFile);
fclose(pOutFile);
return;
}
delete[] pTmpData;
fclose(pWorkFile);
//*fh.FileName[i] += strlen(ObjectFilefoldPath);
string str1 = fh.FileName[i];
str1 = str1.substr(strlen(ObjectFilefoldPath)+1,MAX_PATH);
//cout << "Object: " << ObjectFilefoldPath << endl;
//cout << "strl: " << str1 << endl;
strcpy(fh.FileName[i], str1.c_str());
//cout << "fh.FileName modify: " << fh.FileName[i] << endl;
}
for (int i = 0; i < fh.FileCount; i++)
{
cout << "fh.FileName modify: " << fh.FileName[i] << endl;
}
//写入文件头
fwrite(&fh, sizeof(fh), 1, pOutFile);
fclose(pOutFile);
cout << "打包完成" << endl;
}
//显示打包内文件信息
void Pack_file::printCAB()
{
cout << "文件内信息如下:" << endl;
cout << "文件总数:" << fh.FileCount << endl;
for (int i = 0; i < fh.FileCount; i++)
{
cout << fh.FileName[i] << "\t\t\t\t" << fh.FileLen[i] << "字节" << endl;
}
}
//创建文件夹
void Pack_file::CheckTargetPath(string targetPath)
{
//Log &log = Log::getLog("main", "CheckTargetPath");
int e_pos = targetPath.length();
int f_pos = targetPath.find("\\", 0);
string subdir;
do
{
e_pos = targetPath.find("\\", f_pos + 2);
if (e_pos != -1)
{
subdir = targetPath.substr(0, e_pos);
if (_mkdir(subdir.c_str()) == 0)
printf("creat success %s", subdir.c_str());
else
printf("creat fail %s", subdir.c_str());
}
f_pos = e_pos;
} while (f_pos != -1);
}