| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- #include "fileprepare.h"
- #include "numberreg.h"
- #include "sncdatabase.h"
- #include <QtCore>
- FilePrepare::FilePrepare(QString fileName, QString fileType, bool isMulti, QString moduleID, QString clientID)
- {
- this->oriFileName = fileName;
- this->fileType = fileType;
- this->count = 0;
- this->isZipFile = false;
- this->isMulti = isMulti;
- this->minNumbers = SNCDataBase::getJSJCpnumMinSize().toInt();
- this->maxNumbers = SNCDataBase::getJSJCpnumMaxSize().toInt();
- this->moduleID = moduleID;
- this->clientID = clientID;
- if(this->moduleID.isEmpty())
- {
- this->moduleID = "m";
- }
- if(this->clientID.isEmpty())
- {
- this->clientID = "c";
- }
- m_tempPath = QDir::tempPath() + QString("/SNCUnzipFile/%1/%2/").arg(this->moduleID).arg(this->clientID);
- QDir dir(m_tempPath);
- if(!dir.exists())
- {
- dir.mkpath(m_tempPath);
- }
- }
- bool FilePrepare::prepare()
- {
- if(!chkFileInfo())
- return false;
- if(!chkNumbers())
- return false;
- return zipFile();
- }
- QString FilePrepare::getRtnMsg()
- {
- return this->rtnMsg;
- }
- bool FilePrepare::chkFileInfo()
- {
- QFileInfo f(this->oriFileName);
- this->fileInfo = f;
- QString suffix = fileInfo.suffix().toLower();
- int fileSize = fileInfo.size()/1024/1024;
- bool isPass = true;
- if(suffix=="txt")
- {
- isZipFile = false;
- if(fileSize>TXT_FILE_LIMIT)
- {
- isPass = false;
- rtnMsg = QString("TXT格式的文件大小不能大于%1M").arg(TXT_FILE_LIMIT);
- }
- if(fileType=="2")
- {
- if(fileSize>SEG_FILE_LIMIT)
- {
- isPass = false;
- rtnMsg = QString("号段文件不能大于%1M").arg(SEG_FILE_LIMIT);
- }
- }
- }
- else if(suffix=="zip" || suffix=="7z" || suffix=="rar")
- {
- isZipFile = true;
- if(fileSize>ZIP_FILE_LIMIT)
- {
- isPass = false;
- rtnMsg = QString("压缩包格式的文件大小不能大于%1M").arg(ZIP_FILE_LIMIT);
- }
- if(fileType=="2")
- {
- isPass = false;
- rtnMsg = QString("号段文件只能是TXT格式");
- }
- }
- else
- {
- isPass = false;
- rtnMsg = QString("该文件格式无法处理");
- }
- return isPass;
- }
- bool FilePrepare::extractNumberFromTxtFile(QStringList fileNames)
- {
- numbers.clear();
- for(int i=0;i<fileNames.size();i++)
- {
- QString fileName = fileNames.at(i);
- QFile file(fileName);
- if(!file.open(QIODevice::ReadOnly))
- {
- rtnMsg = file.errorString();
- file.close();
- return false;
- }
- QTextStream in(&file);
- int nums = 0;
- while(!in.atEnd())
- {
- QString line = in.readLine().trimmed();
- if(NumberReg::getCellPhoneReg().exactMatch(line))
- {
- numbers<<line;
- nums++;
- }
- }
- file.close();
- if(nums<minNumbers||nums>maxNumbers)
- {
- rtnMsg = QString("每个号码文件号码数量至少%1,号码总数最多%2").arg(minNumbers).arg(maxNumbers);
- return false;
- }
- }
- this->count = numbers.size();
- if(this->count>maxNumbers)
- {
- rtnMsg = QString("号码总数最多%1").arg(maxNumbers);
- return false;
- }
- return true;
- }
- bool FilePrepare::extractSegmentFromTxtFile(QStringList fileNames)
- {
- for(int i=0;i<fileNames.size();i++)
- {
- QString fileName = fileNames.at(i);
- QFile file(fileName);
- if(!file.open(QIODevice::ReadOnly))
- {
- rtnMsg = file.errorString();
- file.close();
- return false;
- }
- QTextStream in(&file);
- segments.clear();
- while(!in.atEnd())
- {
- QString line = in.readLine().trimmed();
- if(NumberReg::getSegmentReg().exactMatch(line))
- segments<<line;
- }
- file.close();
- }
- this->count = segments.size()*10000;
- if(count<minNumbers||count>maxNumbers)
- {
- rtnMsg = QString("文件号码数量至少%1,号码总数最多%2").arg(minNumbers).arg(maxNumbers);
- return false;
- }
- return true;
- }
- bool FilePrepare::chkNumbers()
- {
- if(isZipFile)
- {
- if(!unzipFile())
- {
- return false;
- }
- }
- else
- {
- this->txtFileNames.clear();
- this->txtFileNames.append(this->oriFileName);
- }
- if(this->txtFileNames.size()==0)
- {
- rtnMsg = "要处理的号码文件不存在" ;
- return false;
- }
- if(fileType=="2")
- {
- return extractSegmentFromTxtFile(this->txtFileNames);
- }
- else
- {
- return extractNumberFromTxtFile(this->txtFileNames);
- }
- }
- bool FilePrepare::unzipFile()
- {
- //准备目录并删除该目录下的所有文件
- QDir dir(m_tempPath);
- QFileInfoList list = dir.entryInfoList();
- for(int i=0;i<list.size();i++)
- {
- if(list.at(i).isFile())
- {
- qDebug()<<"delete file :"<<list.at(i).absoluteFilePath();
- QFile::remove(list.at(i).absoluteFilePath());
- }
- }
- //调用7z进行解压缩
- QProcess process;
- process.setStandardOutputFile(m_tempPath+"output.log");
- process.setWorkingDirectory(QCoreApplication::applicationDirPath());
- process.start("\"" + QCoreApplication::applicationDirPath() + "/7ze.exe\" e \"" + this->oriFileName + "\" -o\"" + m_tempPath +"\" -y");
- process.waitForFinished();
- dir.refresh();
- list = dir.entryInfoList();
- QStringList fileList;
- for(int i=0;i<list.size();i++)
- {
- if(list.at(i).isFile())
- {
- if(list.at(i).suffix().toLower()=="txt")
- fileList<<list.at(i).absoluteFilePath();
- }
- }
- if(fileList.size()==0)
- {
- rtnMsg = "压缩包无法解压或者没有包含有效的TXT文件";
- return false;
- }
- this->txtFileNames = fileList;
- return true;
- }
- bool FilePrepare::zipFile()
- {
- QString newZipfile = m_tempPath + fileInfo.completeBaseName() + ".zip";
- if(QFile::exists(newZipfile))
- QFile::remove(newZipfile);
- QProcess process;
- process.setWorkingDirectory(QCoreApplication::applicationDirPath());
- if(isZipFile)
- process.start("\"" + QCoreApplication::applicationDirPath() + "/7ze.exe\" a \"" + newZipfile + "\" \"" +m_tempPath +"*.txt\"");
- else
- process.start("\"" + QCoreApplication::applicationDirPath() + "/7ze.exe\" a \"" + newZipfile + "\" \"" + this->oriFileName +"\"");
- process.waitForFinished();
- this->zipFileName = newZipfile;
- return true;
- }
- QString FilePrepare::getLastUploadFileName()
- {
- if(fileType=="2")//如果是号段文件,则仍然取原始文件,因为原始文件之前已经做过判断肯定是txt文件
- {
- return this->oriFileName;
- }
- else
- {
- return this->zipFileName;
- }
- }
- int FilePrepare::getCount()
- {
- return this->count;
- }
|