filesplitter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "filesplitter.h"
  2. FileSplitter::FileSplitter(QString fileName,QString fileID,int unit)
  3. {
  4. this->fileName = fileName;
  5. this->unit = unit;
  6. this->outputDir = QDir::tempPath()+"/sncsplit";
  7. this->fileID = fileID;
  8. }
  9. QStringList FileSplitter::getSplitResult()
  10. {
  11. QStringList results;
  12. if(!prepare())
  13. return results;
  14. QDir dir(this->outputDir);
  15. QFileInfoList list = dir.entryInfoList(QDir::Files,QDir::Name);
  16. for(int i=0;i<list.size();i++)
  17. {
  18. if(list.at(i).isFile())
  19. {
  20. results<<list.at(i).absoluteFilePath();
  21. }
  22. }
  23. if(results.size()==0)
  24. {
  25. results = split();
  26. }
  27. return results;
  28. }
  29. QStringList FileSplitter::split()
  30. {
  31. QStringList results;
  32. int size = this->rawData.length();
  33. int pos = 0;
  34. int len = this->unit;
  35. int index = 0;
  36. QString packkey = getRandPackKey();
  37. QFileInfo fileinfo(this->fileName);
  38. QString basename = fileinfo.fileName();
  39. int count = (size % this->unit==0)? (size / this->unit) : size / this->unit + 1;
  40. while(1)
  41. {
  42. index++;
  43. QByteArray packData = this->rawData.mid(pos,len);
  44. QString outname = QString("%1/%2.%3_%4_%5_%6")
  45. .arg(this->outputDir)
  46. .arg(basename)
  47. .arg(packkey)
  48. .arg(QString::number(count))
  49. .arg(QString("000000000"+QString::number(index)).right(10))
  50. .arg(this->checksum);
  51. QFile packFile(outname);
  52. if(packFile.open(QIODevice::WriteOnly))
  53. {
  54. if(packFile.write(packData)==packData.size())
  55. {
  56. results<<outname;
  57. }
  58. packFile.close();
  59. }
  60. pos = pos + len;
  61. if(pos >= size)
  62. break;
  63. }
  64. if(results.size()!=index)
  65. {
  66. for(int i=0;i<results.size();i++)
  67. {
  68. QFile::remove(results.at(i));
  69. }
  70. results.clear();
  71. }
  72. return results;
  73. }
  74. QString FileSplitter::merge(QString curdir)
  75. {
  76. QDir dir(curdir);
  77. if(!dir.exists())
  78. {
  79. dir.mkpath(curdir);
  80. }
  81. QByteArray mergeData;
  82. QFileInfoList list = dir.entryInfoList(QDir::Files,QDir::Name);
  83. for(int i=0;i<list.size();i++)
  84. {
  85. if(list.at(i).isFile())
  86. {
  87. QFile pack(list.at(i).absoluteFilePath());
  88. pack.open(QIODevice::ReadOnly);
  89. mergeData.append(pack.readAll());
  90. pack.close();
  91. }
  92. }
  93. QFile output(curdir + "/merge.dat");
  94. if(!output.open(QIODevice::WriteOnly))
  95. {
  96. return "";
  97. }
  98. output.write(mergeData);
  99. output.close();
  100. return curdir + "/merge.dat";
  101. }
  102. bool FileSplitter::prepare()
  103. {
  104. QFile file(this->fileName);
  105. if(!file.open(QIODevice::ReadOnly))
  106. {
  107. return false;
  108. }
  109. this->rawData = file.readAll();
  110. this->checksum = QCryptographicHash::hash ( this->rawData, QCryptographicHash::Md5 ).toHex();
  111. file.close();
  112. QDir dir;
  113. if(!dir.exists(this->outputDir + "/" + this->checksum + "_" + this->fileID))
  114. {
  115. if(!dir.mkpath(this->outputDir + "/" + this->checksum + "_" + this->fileID))
  116. {
  117. return false;
  118. }
  119. }
  120. this->outputDir += "/" + this->checksum + "_" + this->fileID;
  121. return true;
  122. }
  123. QString FileSplitter::getRandPackKey()
  124. {
  125. QTime time;
  126. time = QTime::currentTime();
  127. qsrand(time.msec()+time.second()*10000);
  128. int v = 100000 + qrand()%899999;
  129. return QString::number(v);
  130. }
  131. void FileSplitter::cleanup()
  132. {
  133. qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
  134. <<"FileSplitter cleanup called."<<this->outputDir;
  135. if(this->checksum.isEmpty())
  136. return ;
  137. QDir dir(this->outputDir);
  138. QFileInfoList list = dir.entryInfoList(QDir::Files,QDir::Name);
  139. for(int i=0;i<list.size();i++)
  140. {
  141. if(list.at(i).isFile())
  142. {
  143. QFile::remove(list.at(i).absoluteFilePath());
  144. }
  145. }
  146. dir.rmdir(this->outputDir);
  147. }