#include "fileprepare.h" #include "numberreg.h" #include "sncdatabase.h" #include 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;imaxNumbers) { 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;icount = segments.size()*10000; if(countmaxNumbers) { 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;ioriFileName + "\" -o\"" + m_tempPath +"\" -y"); process.waitForFinished(); dir.refresh(); list = dir.entryInfoList(); QStringList fileList; for(int i=0;itxtFileNames = 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; }