fileprepare.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "fileprepare.h"
  2. #include "numberreg.h"
  3. #include "sncdatabase.h"
  4. #include <QtCore>
  5. FilePrepare::FilePrepare(QString fileName, QString fileType, bool isMulti, QString moduleID, QString clientID)
  6. {
  7. this->oriFileName = fileName;
  8. this->fileType = fileType;
  9. this->count = 0;
  10. this->isZipFile = false;
  11. this->isMulti = isMulti;
  12. this->minNumbers = SNCDataBase::getJSJCpnumMinSize().toInt();
  13. this->maxNumbers = SNCDataBase::getJSJCpnumMaxSize().toInt();
  14. this->moduleID = moduleID;
  15. this->clientID = clientID;
  16. if(this->moduleID.isEmpty())
  17. {
  18. this->moduleID = "m";
  19. }
  20. if(this->clientID.isEmpty())
  21. {
  22. this->clientID = "c";
  23. }
  24. m_tempPath = QDir::tempPath() + QString("/SNCUnzipFile/%1/%2/").arg(this->moduleID).arg(this->clientID);
  25. QDir dir(m_tempPath);
  26. if(!dir.exists())
  27. {
  28. dir.mkpath(m_tempPath);
  29. }
  30. }
  31. bool FilePrepare::prepare()
  32. {
  33. if(!chkFileInfo())
  34. return false;
  35. if(!chkNumbers())
  36. return false;
  37. return zipFile();
  38. }
  39. QString FilePrepare::getRtnMsg()
  40. {
  41. return this->rtnMsg;
  42. }
  43. bool FilePrepare::chkFileInfo()
  44. {
  45. QFileInfo f(this->oriFileName);
  46. this->fileInfo = f;
  47. QString suffix = fileInfo.suffix().toLower();
  48. int fileSize = fileInfo.size()/1024/1024;
  49. bool isPass = true;
  50. if(suffix=="txt")
  51. {
  52. isZipFile = false;
  53. if(fileSize>TXT_FILE_LIMIT)
  54. {
  55. isPass = false;
  56. rtnMsg = QString("TXT格式的文件大小不能大于%1M").arg(TXT_FILE_LIMIT);
  57. }
  58. if(fileType=="2")
  59. {
  60. if(fileSize>SEG_FILE_LIMIT)
  61. {
  62. isPass = false;
  63. rtnMsg = QString("号段文件不能大于%1M").arg(SEG_FILE_LIMIT);
  64. }
  65. }
  66. }
  67. else if(suffix=="zip" || suffix=="7z" || suffix=="rar")
  68. {
  69. isZipFile = true;
  70. if(fileSize>ZIP_FILE_LIMIT)
  71. {
  72. isPass = false;
  73. rtnMsg = QString("压缩包格式的文件大小不能大于%1M").arg(ZIP_FILE_LIMIT);
  74. }
  75. if(fileType=="2")
  76. {
  77. isPass = false;
  78. rtnMsg = QString("号段文件只能是TXT格式");
  79. }
  80. }
  81. else
  82. {
  83. isPass = false;
  84. rtnMsg = QString("该文件格式无法处理");
  85. }
  86. return isPass;
  87. }
  88. bool FilePrepare::extractNumberFromTxtFile(QStringList fileNames)
  89. {
  90. numbers.clear();
  91. for(int i=0;i<fileNames.size();i++)
  92. {
  93. QString fileName = fileNames.at(i);
  94. QFile file(fileName);
  95. if(!file.open(QIODevice::ReadOnly))
  96. {
  97. rtnMsg = file.errorString();
  98. file.close();
  99. return false;
  100. }
  101. QTextStream in(&file);
  102. int nums = 0;
  103. while(!in.atEnd())
  104. {
  105. QString line = in.readLine().trimmed();
  106. if(NumberReg::getCellPhoneReg().exactMatch(line))
  107. {
  108. numbers<<line;
  109. nums++;
  110. }
  111. }
  112. file.close();
  113. if(nums<minNumbers||nums>maxNumbers)
  114. {
  115. rtnMsg = QString("每个号码文件号码数量至少%1,号码总数最多%2").arg(minNumbers).arg(maxNumbers);
  116. return false;
  117. }
  118. }
  119. this->count = numbers.size();
  120. if(this->count>maxNumbers)
  121. {
  122. rtnMsg = QString("号码总数最多%1").arg(maxNumbers);
  123. return false;
  124. }
  125. return true;
  126. }
  127. bool FilePrepare::extractSegmentFromTxtFile(QStringList fileNames)
  128. {
  129. for(int i=0;i<fileNames.size();i++)
  130. {
  131. QString fileName = fileNames.at(i);
  132. QFile file(fileName);
  133. if(!file.open(QIODevice::ReadOnly))
  134. {
  135. rtnMsg = file.errorString();
  136. file.close();
  137. return false;
  138. }
  139. QTextStream in(&file);
  140. segments.clear();
  141. while(!in.atEnd())
  142. {
  143. QString line = in.readLine().trimmed();
  144. if(NumberReg::getSegmentReg().exactMatch(line))
  145. segments<<line;
  146. }
  147. file.close();
  148. }
  149. this->count = segments.size()*10000;
  150. if(count<minNumbers||count>maxNumbers)
  151. {
  152. rtnMsg = QString("文件号码数量至少%1,号码总数最多%2").arg(minNumbers).arg(maxNumbers);
  153. return false;
  154. }
  155. return true;
  156. }
  157. bool FilePrepare::chkNumbers()
  158. {
  159. if(isZipFile)
  160. {
  161. if(!unzipFile())
  162. {
  163. return false;
  164. }
  165. }
  166. else
  167. {
  168. this->txtFileNames.clear();
  169. this->txtFileNames.append(this->oriFileName);
  170. }
  171. if(this->txtFileNames.size()==0)
  172. {
  173. rtnMsg = "要处理的号码文件不存在" ;
  174. return false;
  175. }
  176. if(fileType=="2")
  177. {
  178. return extractSegmentFromTxtFile(this->txtFileNames);
  179. }
  180. else
  181. {
  182. return extractNumberFromTxtFile(this->txtFileNames);
  183. }
  184. }
  185. bool FilePrepare::unzipFile()
  186. {
  187. //准备目录并删除该目录下的所有文件
  188. QDir dir(m_tempPath);
  189. QFileInfoList list = dir.entryInfoList();
  190. for(int i=0;i<list.size();i++)
  191. {
  192. if(list.at(i).isFile())
  193. {
  194. qDebug()<<"delete file :"<<list.at(i).absoluteFilePath();
  195. QFile::remove(list.at(i).absoluteFilePath());
  196. }
  197. }
  198. //调用7z进行解压缩
  199. QProcess process;
  200. process.setStandardOutputFile(m_tempPath+"output.log");
  201. process.setWorkingDirectory(QCoreApplication::applicationDirPath());
  202. process.start("\"" + QCoreApplication::applicationDirPath() + "/7ze.exe\" e \"" + this->oriFileName + "\" -o\"" + m_tempPath +"\" -y");
  203. process.waitForFinished();
  204. dir.refresh();
  205. list = dir.entryInfoList();
  206. QStringList fileList;
  207. for(int i=0;i<list.size();i++)
  208. {
  209. if(list.at(i).isFile())
  210. {
  211. if(list.at(i).suffix().toLower()=="txt")
  212. fileList<<list.at(i).absoluteFilePath();
  213. }
  214. }
  215. if(fileList.size()==0)
  216. {
  217. rtnMsg = "压缩包无法解压或者没有包含有效的TXT文件";
  218. return false;
  219. }
  220. this->txtFileNames = fileList;
  221. return true;
  222. }
  223. bool FilePrepare::zipFile()
  224. {
  225. QString newZipfile = m_tempPath + fileInfo.completeBaseName() + ".zip";
  226. if(QFile::exists(newZipfile))
  227. QFile::remove(newZipfile);
  228. QProcess process;
  229. process.setWorkingDirectory(QCoreApplication::applicationDirPath());
  230. if(isZipFile)
  231. process.start("\"" + QCoreApplication::applicationDirPath() + "/7ze.exe\" a \"" + newZipfile + "\" \"" +m_tempPath +"*.txt\"");
  232. else
  233. process.start("\"" + QCoreApplication::applicationDirPath() + "/7ze.exe\" a \"" + newZipfile + "\" \"" + this->oriFileName +"\"");
  234. process.waitForFinished();
  235. this->zipFileName = newZipfile;
  236. return true;
  237. }
  238. QString FilePrepare::getLastUploadFileName()
  239. {
  240. if(fileType=="2")//如果是号段文件,则仍然取原始文件,因为原始文件之前已经做过判断肯定是txt文件
  241. {
  242. return this->oriFileName;
  243. }
  244. else
  245. {
  246. return this->zipFileName;
  247. }
  248. }
  249. int FilePrepare::getCount()
  250. {
  251. return this->count;
  252. }