| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #include "filesplitter.h"
- FileSplitter::FileSplitter(QString fileName,QString fileID,int unit)
- {
- this->fileName = fileName;
- this->unit = unit;
- this->outputDir = QDir::tempPath()+"/sncsplit";
- this->fileID = fileID;
- }
- QStringList FileSplitter::getSplitResult()
- {
- QStringList results;
- if(!prepare())
- return results;
- QDir dir(this->outputDir);
- QFileInfoList list = dir.entryInfoList(QDir::Files,QDir::Name);
- for(int i=0;i<list.size();i++)
- {
- if(list.at(i).isFile())
- {
- results<<list.at(i).absoluteFilePath();
- }
- }
- if(results.size()==0)
- {
- results = split();
- }
- return results;
- }
- QStringList FileSplitter::split()
- {
- QStringList results;
- int size = this->rawData.length();
- int pos = 0;
- int len = this->unit;
- int index = 0;
- QString packkey = getRandPackKey();
- QFileInfo fileinfo(this->fileName);
- QString basename = fileinfo.fileName();
- int count = (size % this->unit==0)? (size / this->unit) : size / this->unit + 1;
- while(1)
- {
- index++;
- QByteArray packData = this->rawData.mid(pos,len);
- QString outname = QString("%1/%2.%3_%4_%5_%6")
- .arg(this->outputDir)
- .arg(basename)
- .arg(packkey)
- .arg(QString::number(count))
- .arg(QString("000000000"+QString::number(index)).right(10))
- .arg(this->checksum);
- QFile packFile(outname);
- if(packFile.open(QIODevice::WriteOnly))
- {
- if(packFile.write(packData)==packData.size())
- {
- results<<outname;
- }
- packFile.close();
- }
- pos = pos + len;
- if(pos >= size)
- break;
- }
- if(results.size()!=index)
- {
- for(int i=0;i<results.size();i++)
- {
- QFile::remove(results.at(i));
- }
- results.clear();
- }
- return results;
- }
- QString FileSplitter::merge(QString curdir)
- {
- QDir dir(curdir);
- if(!dir.exists())
- {
- dir.mkpath(curdir);
- }
- QByteArray mergeData;
- QFileInfoList list = dir.entryInfoList(QDir::Files,QDir::Name);
- for(int i=0;i<list.size();i++)
- {
- if(list.at(i).isFile())
- {
- QFile pack(list.at(i).absoluteFilePath());
- pack.open(QIODevice::ReadOnly);
- mergeData.append(pack.readAll());
- pack.close();
- }
- }
- QFile output(curdir + "/merge.dat");
- if(!output.open(QIODevice::WriteOnly))
- {
- return "";
- }
- output.write(mergeData);
- output.close();
- return curdir + "/merge.dat";
- }
- bool FileSplitter::prepare()
- {
- QFile file(this->fileName);
- if(!file.open(QIODevice::ReadOnly))
- {
- return false;
- }
- this->rawData = file.readAll();
- this->checksum = QCryptographicHash::hash ( this->rawData, QCryptographicHash::Md5 ).toHex();
- file.close();
- QDir dir;
- if(!dir.exists(this->outputDir + "/" + this->checksum + "_" + this->fileID))
- {
- if(!dir.mkpath(this->outputDir + "/" + this->checksum + "_" + this->fileID))
- {
- return false;
- }
- }
- this->outputDir += "/" + this->checksum + "_" + this->fileID;
- return true;
- }
- QString FileSplitter::getRandPackKey()
- {
- QTime time;
- time = QTime::currentTime();
- qsrand(time.msec()+time.second()*10000);
- int v = 100000 + qrand()%899999;
- return QString::number(v);
- }
- void FileSplitter::cleanup()
- {
- qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
- <<"FileSplitter cleanup called."<<this->outputDir;
- if(this->checksum.isEmpty())
- return ;
- QDir dir(this->outputDir);
- QFileInfoList list = dir.entryInfoList(QDir::Files,QDir::Name);
- for(int i=0;i<list.size();i++)
- {
- if(list.at(i).isFile())
- {
- QFile::remove(list.at(i).absoluteFilePath());
- }
- }
- dir.rmdir(this->outputDir);
- }
|