-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiupxmanager.cpp
246 lines (233 loc) · 7.16 KB
/
multiupxmanager.cpp
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
#include "multiupxmanager.h"
#include "upxui.h"
#include <QMap>
#include <QDebug>
#include <QProcess>
MultiUpxManager::MultiUpxManager()
{
/* default value */
argLv.clear();
argBuckup = "-k";
}
/**
* @brief MultiUpxManager::setBackupEnable 设定备份文件
* @param enable
*/
void MultiUpxManager::setBackupEnable(bool enable)
{
argBuckup.clear();
if( enable ){
argBuckup = "-k";
}
}
/**
* @brief MultiUpxManager::setCompressionLv 设定压缩级别
* @param value 压缩级别
*/
void MultiUpxManager::setCompressionLv(const quint32 &value)
{
argLv.clear();
switch( value ){
case 10: argLv = "--best";
break;
default:
if( (value <= 9) && (value >= 1) )
argLv = "-" + QString::number(value);
break;
}
}
/**
* @brief MultiUpxManager::compress 压缩文件
* @param filePath
*/
void MultiUpxManager::compress(QStringList filePath , qint32 multiThread)
{
createMap(filePath);
fileCount = filePath.count();
upx.clear();
if( fileCount < multiThread ){ // 创建多个线程
multiThread = fileCount;
}
while(multiThread -- ){
upx<<new QProcess();
}
foreach(auto process, upx){
connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus exitStatus){
Q_UNUSED(exitStatus)
QString file = process->arguments().last();
switch( exitCode ){
case 0:
fileStaMap[file] = Success;
emit signals_fileStatus(file, Success); // 压缩成功
break;
case 1:
fileStaMap[file] = FileNotFound;
emit signals_fileStatus(file, FileNotFound); // 未找到文件
break;
case 2:
fileStaMap[file] = HasCompress;
emit signals_fileStatus(file, HasCompress); // 压缩失败(已经被压缩)
break;
default:
fileStaMap[file] = Error;
emit signals_fileStatus(file, Error); // 压缩失败(未知错误)
qDebug()<<"file err:"<<file;
qDebug()<<"exitCode:"<<exitCode;
break;
}
file = process->readAllStandardError();
if( !file.isEmpty() )
emit signals_errMsg(file);
fileCount--;
if(fileCount <= 0){
emit signals_finishCompress();
}
/* next file */
QString fileName = getIdleFile();
if( fileName.isEmpty() ){
return;
}
QStringList arg;
if( !argBuckup.isEmpty() ){
arg << argBuckup ;
}
if( !argLv.isEmpty() ){
arg << argLv;
}
arg << fileName;
process->start( "upx.exe", arg );
emit signals_fileStatus(fileName, Compressing);// 启动信号
});
/* 启动多个线程 */
QString fileName = getIdleFile();
if( fileName.isEmpty() ){
return;
}
QStringList arg;
if( !argBuckup.isEmpty() ){
arg << argBuckup ;
}
if( !argLv.isEmpty() ){
arg << argLv;
}
arg << fileName;
process->start( "upx.exe", arg );
emit signals_fileStatus(fileName, Compressing);
}
}
/**
* @brief MultiUpxManager::createMap 建立文件与状态量的映射
* @param fIlePath
*/
void MultiUpxManager::createMap( QStringList &filePath )
{
fileStaMap.clear();
foreach (auto path, filePath ) {
fileStaMap[path] = Idle;
}
}
/**
* @brief MultiUpxManager::getIdleFile 获取未压缩的文件路径,并且标记未压缩中
* @return
*/
QString MultiUpxManager::getIdleFile()
{
if( fileStaMap.isEmpty() ){
return QString();
}
QList<QString> keys = fileStaMap.keys( Idle );
if( keys.empty() ){
return QString();
}else{
fileStaMap[keys.at(0)] = Compressing;
return keys.at(0);
}
}
/**
* @brief MultiUpxManager::compress 压缩文件
* @param filePath
*/
void MultiUpxManager::decompress(QStringList filePath , qint32 multiThread)
{
createMap(filePath);
fileCount = filePath.count();
upx.clear();
if( fileCount < multiThread ){ // 创建多个线程
multiThread = fileCount;
}
while(multiThread -- ){
upx<<new QProcess();
}
foreach(auto process, upx){
connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus exitStatus){
Q_UNUSED(exitStatus)
QString file = process->arguments().last();
switch( exitCode ){
case 0:
fileStaMap[file] = Success;
emit signals_fileStatus(file, Success); // 解压成功
break;
case 1:
fileStaMap[file] = FileNotFound;
emit signals_fileStatus(file, FileNotFound); // 未找到文件
break;
case 2:
fileStaMap[file] = NotCompress;
emit signals_fileStatus(file, NotCompress); // 压缩失败(已经被压缩)
break;
default:
fileStaMap[file] = Error;
emit signals_fileStatus(file, Error); // 压缩失败(未知错误)
qDebug()<<"file err:"<<file;
qDebug()<<"exitCode:"<<exitCode;
break;
}
file = process->readAllStandardError();
if( !file.isEmpty() )
emit signals_errMsg(file);
fileCount--;
if(fileCount <= 0){
emit signals_finishCompress();
}
/* next file */
QString fileName = getIdleFile();
if( fileName.isEmpty() ){
return;
}
QStringList arg;
arg<<"-d"; // 解压
arg << fileName;
process->start( "upx.exe", arg );
emit signals_fileStatus(fileName, Decompressing);// 启动
});
/* 启动多个线程 */
QString fileName = getIdleFile();
if( fileName.isEmpty() ){
return;
}
QStringList arg;
arg<<"-d"; // 解压
arg << fileName;
process->start( "upx.exe", arg );
emit signals_fileStatus(fileName, Decompressing);
}
}
/**
* @brief MultiUpxManager::test
* @param filePath
*/
void MultiUpxManager::test(QString filePath )
{
QProcess *upx_ = new QProcess();
QStringList arg;
arg<<"-t"; // 测试
arg<<filePath;
upx_->start( "upx.exe", arg);
upx_->waitForStarted(100);
upx_->waitForFinished(100);
filePath = upx_->readAllStandardError();
emit signals_errMsg(filePath);
delete upx_;
}