Menu

[r40]: / trunk / workthread.cpp  Maximize  Restore  History

Download this file

107 lines (91 with data), 3.1 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
#include "workthread.h"
WorkThread::WorkThread() {
FStop = false;
}
WorkThread::~WorkThread() {
}
// http://www.cnblogs.com/zhaox583132460/p/3436942.html
void WorkThread::EncodeFile(QString inputFile, QString outputFile) {
QFile inputStream(inputFile);
QFile outputStream(outputFile);
int bufsize = 3 * 1024;
//QDateTime startTime = QDateTime::currentDateTime();
if(inputStream.open(QIODevice::ReadOnly)) {
outputStream.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&outputStream);
int c = 0;
while(!inputStream.atEnd()) {
if(FStop) break;
QByteArray ind = inputStream.read(bufsize);
out << ind.toBase64();
if(c == 200 || inputStream.pos() == inputStream.size()) {
emit UpdateProgress(inputStream.pos(),inputStream.size());
c = 0;
}
c++;
}
outputStream.close();
inputStream.close();
}
//QDateTime endTime = QDateTime::currentDateTime();
//qDebug() << startTime.secsTo(endTime);
}
void WorkThread::DecodeFile(QString inputFile, QString outputFile) {
QFile inputStream(inputFile);
QFile outputStream(outputFile);
int bufsize = 4 * 1024;
if(inputStream.open(QIODevice::ReadOnly | QIODevice::Text)) {
outputStream.open(QIODevice::WriteOnly);
int c = 0;
while(!inputStream.atEnd()) {
if(FStop) break;
QByteArray ind = inputStream.read(bufsize);
outputStream.write(QByteArray::fromBase64(ind));
if(c == 200 || inputStream.pos() == inputStream.size()) {
emit UpdateProgress(inputStream.pos(),inputStream.size());
c = 0;
}
c++;
}
outputStream.close();
inputStream.close();
}
}
void WorkThread::Process() {
int i;
for(i = 0; i < FFileList.count(); i++) {
if(FStop) break;
QFileInfo fi(FFileList.at(i));
if(!FOutputPath.endsWith("/") && !FOutputPath.endsWith("\\")) {
FOutputPath.append("/");
}
if(FMode == wmEncode) {
QString OutputFileName = FOutputPath + fi.fileName() + ".enc";
EncodeFile(FFileList.at(i),OutputFileName);
}
else {
QString OutputFileName;
if(fi.fileName().endsWith(".enc")) {
OutputFileName = FOutputPath + fi.fileName().replace(".enc","");
}
else {
OutputFileName = FOutputPath + fi.fileName() + ".decoded";
}
DecodeFile(FFileList.at(i),OutputFileName);
}
emit FileComplete((i+1),FFileList.count());
}
emit Finished();
}
void WorkThread::SetMode(WorkMode mode) {
FMode = mode;
}
void WorkThread::SetOutputPath(QString path) {
FOutputPath = path;
}
void WorkThread::AddFile(QString filename) {
FFileList << filename;
}
void WorkThread::DoStop() {
FStop = true;
}
MongoDB Logo MongoDB