bool VideoMetadataImp::removeDir(const QString &dirName) { QDir d(dirName); QFileInfoList contents = d.entryInfoList(); if (!contents.size()) { return d.rmdir(dirName); } for (QFileInfoList::iterator p = contents.begin(); p != contents.end(); ++p) { if (p->fileName() == "." || p->fileName() == "..") { continue; } if (p->isDir()) { QString fileName = p->fileName(); if (!removeDir(fileName)) return false; } else { if (!QFile(p->fileName()).remove()) return false; } } return d.rmdir(dirName); }
/*递归删除文件夹内容 ** **dir :文件夹的名字,绝对路径和相对路径均可 ** **返回值 :成功后返回true;否则返回false */ bool removeFileWithDir(const QString &dirName) { static QVector<QString> dirNames; QDir dir; QFileInfoList filst; QFileInfoList::iterator curFi; //初始化 dirNames.clear(); if(dir.exists()){ dirNames<<dirName; } else{ return true; } //遍历各级文件夹,并将这些文件夹中的文件删除 for(int i=0;i<dirNames.size();++i) { dir.setPath(dirNames[i]); filst=dir.entryInfoList(QDir::Dirs|QDir::Files |QDir::Readable|QDir::Writable |QDir::Hidden|QDir::NoDotAndDotDot ,QDir::Name); if(filst.size()>0) { curFi=filst.begin(); while(curFi!=filst.end()) { //遇到文件夹,则添加至文件夹列表dirs尾部 if(curFi->isDir()) { dirNames.push_back(curFi->filePath()); } else if(curFi->isFile()) { //遇到文件,则删除之 if(!dir.remove(curFi->fileName())) { //return false; } } curFi++; }//end of while } } #if 0 //删除文件夹 for(int i=dirNames.size()-1;i>=0;--i) { if(!dir.rmdir(dirNames[i])) { //return false; } } #endif return true; }
int PathManager::ReplacePakInDec(QString &path, QString &oldName, QString &newName) { QVector<QString> dirNames; QDir dir(path); QFileInfoList filst; QFileInfoList::iterator curFi; //初始化 dirNames.clear(); if (dir.exists()){ dirNames << path; } else{ return 0; } //遍历各级文件夹,并将这些文件夹中的文件删除 for (int i = 0; i<dirNames.size(); ++i) { dir.setPath(dirNames[i]); filst = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::NoDotAndDotDot , QDir::Name); if (filst.size()>0){ curFi = filst.begin(); while (curFi != filst.end()) { //遇到文件夹,则添加至文件夹列表dirs尾部 if (curFi->isDir()){ dirNames.push_back(curFi->filePath()); } else if (curFi->isFile()){ //遇到文件,则删除之 if (curFi->absoluteFilePath().toLower().endsWith(".smali")){ if (!ReplacePakNameInSmali(curFi->absoluteFilePath(), oldName, newName)){ return 1; } } else if (curFi->absoluteFilePath().toLower().endsWith(".xml")){ if (!ReplacePakNameInXml(path, curFi->absoluteFilePath(), oldName, newName)){ return 1; } } } curFi++; }//end of while } } int ret = 0; ret = RenamePakInDec(path, oldName, newName); if (ret != 0){ return ret; } return 0; }
void UniboardSankoreTransition::rollbackDocumentsTransition(QFileInfoList& fileInfoList) { QFileInfoList::iterator fileInfo; for (fileInfo = fileInfoList.begin(); fileInfo != fileInfoList.end(); fileInfo += 1) { if (fileInfo->isDir() && fileInfo->fileName().startsWith("Uniboard Document ")){ QString sankoreDocumentName = fileInfo->fileName(); sankoreDocumentName.replace("Uniboard","Sankore"); QString sankoreDocumentDirectoryPath = UBSettings::userDocumentDirectory() + "/" + sankoreDocumentName; if (QFileInfo(sankoreDocumentDirectoryPath).exists()){ UBFileSystemUtils::deleteDir(sankoreDocumentDirectoryPath); } } } }
static void recursiveFileInfoList(const QDir &dir, const QStringList &nameFilters, QDir::Filters filter, bool recursive, QFileInfoList *fileinfolist) { if (recursive) filter |= QDir::AllDirs; QFileInfoList entries = dir.entryInfoList(nameFilters, filter); QFileInfoList::iterator it; for (it = entries.begin(); it != entries.end(); ++it) { QString fname = it->fileName(); if (fname != QLatin1String(".") && fname != QLatin1String("..")) { if (it->isDir()) recursiveFileInfoList(QDir(it->absoluteFilePath()), nameFilters, filter, recursive, fileinfolist); else fileinfolist->append(*it); } } }
bool PathManager::SearchDirContianSuffix(const QString &dirFrom, QStringList &result, QString &suffix) { QVector<QString> dirNames; QDir dir(dirFrom); QFileInfoList filst; QFileInfoList::iterator curFi; //初始化 dirNames.clear(); if (dir.exists()){ dirNames << dirFrom; } else{ return true; } //遍历各级文件夹,并将这些文件夹中的文件删除 for (int i = 0; i < dirNames.size(); ++i) { dir.setPath(dirNames[i]); filst = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::NoDotAndDotDot , QDir::DirsFirst); if (filst.size()>0){ curFi = filst.begin(); while (curFi != filst.end()) { //遇到文件夹,则添加至文件夹列表dirs尾部 if (curFi->isDir()){ dirNames.push_back(curFi->filePath()); } else if (curFi->isFile()){ if (curFi->fileName().endsWith(suffix)){ result.push_back(curFi->absolutePath()); break; } } curFi++; }//end of while } } return true; }
QStringList StorageGroup::GetFileInfoList(QString Path) { QStringList files; QString relPath; bool badPath = true; if (Path.isEmpty() || Path == "/") { for (QStringList::Iterator it = m_dirlist.begin(); it != m_dirlist.end(); ++it) files << QString("sgdir::%1").arg(*it); return files; } for (QStringList::Iterator it = m_dirlist.begin(); it != m_dirlist.end(); ++it) { if (Path.startsWith(*it)) { relPath = Path; relPath.replace(*it,""); if (relPath.startsWith("/")) relPath.replace(0,1,""); badPath = false; } } LOG(VB_FILE, LOG_INFO, LOC + QString("GetFileInfoList: Reading '%1'").arg(Path)); if (badPath) return files; QDir d(Path); if (!d.exists()) return files; QFileInfoList list = d.entryInfoList(); if (!list.size()) return files; for (QFileInfoList::iterator p = list.begin(); p != list.end(); ++p) { if (p->fileName() == "." || p->fileName() == ".." || p->fileName() == "Thumbs.db") { continue; } QString tmp; if (p->isDir()) tmp = QString("dir::%1::0").arg(p->fileName()); else tmp = QString("file::%1::%2::%3%4").arg(p->fileName()).arg(p->size()) .arg(relPath).arg(p->fileName()); LOG(VB_FILE, LOG_DEBUG, LOC + QString("GetFileInfoList: (%1)").arg(tmp)); files.append(tmp); } return files; }
bool PathManager::CopyDir(const QString &source, const QString &destination, bool isCover) { /* 递归实现 */ /*QDir directory(source); if (!directory.exists()){ return false; } QString srcPath = QDir::toNativeSeparators(source); if (!srcPath.endsWith(QDir::separator())){ srcPath += QDir::separator(); } QString dstPath = QDir::toNativeSeparators(destination); if (!dstPath.endsWith(QDir::separator())){ dstPath += QDir::separator(); } QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden); for (QStringList::size_type i = 0; i != fileNames.size(); ++i) { QString fileName = fileNames.at(i); QString srcFilePath = srcPath + fileName; QString dstFilePath = dstPath + fileName; QFileInfo fileInfo(srcFilePath); if (fileInfo.isFile() || fileInfo.isSymLink()){ if (isCover){ QFile::setPermissions(dstFilePath, QFile::WriteOwner); } if (!QFile::copy(srcFilePath, dstFilePath)){ return false; } } else if (fileInfo.isDir()){ QDir dstDir(dstFilePath); dstDir.mkpath(dstFilePath); if (!CopyDir(srcFilePath, dstFilePath, isCover)){ return false; } } } return true;*/ QVector<QString> dirNames; QVector<QString> fileNames; QDir dir(source); if (!dir.exists()){ return false; } dirNames.clear(); dirNames << source; QFileInfoList filst; QFileInfoList::iterator curFi; for (int i = 0; i < dirNames.size(); ++i) { dir.setPath(dirNames[i]); filst = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::NoDotAndDotDot , QDir::DirsFirst); if (filst.size() > 0){ curFi = filst.begin(); while (curFi != filst.end()) { if (curFi->isDir()){ dirNames.push_back(curFi->filePath()); } else if (curFi->isFile()){ fileNames.push_back(curFi->absoluteFilePath()); } curFi++; } } } for (QVector<QString>::Iterator ite = dirNames.begin(); ite != dirNames.end(); ite++) { QDir dirSrc(source); QDir dirDest(destination); ite->replace(dirSrc.absolutePath(), dirDest.absolutePath()); QDir dirTmp(*ite); if (!dirTmp.exists() && !dirTmp.mkpath(*ite)){ return false; } } for (QVector<QString>::Iterator ite = fileNames.begin(); ite != fileNames.end(); ite++) { QDir dirSrc(source); QDir dirDest(destination); QString srcPath = *ite; QString tar = ite->replace(dirSrc.absolutePath(), dirDest.absolutePath()); QFile file(tar); if (file.exists()){ if (isCover){ if (!file.remove()){ return false; } } else{ continue; } } if (!QFile::copy(srcPath, tar)){ return false; } } return true; }
/********** *返回值:0成功 *1原包名不存在 *2创建包出错 *3目的包名已经存在 *4替换包名过程出错 *5替换app包名出错 ***********/ int PathManager::ReplaceAppPakInSrc(QString &path, QString &oldName, QString &newName) { QVector<QString> dirNames; QDir dir(path); QFileInfoList filst; QFileInfoList::iterator curFi; //初始化 dirNames.clear(); if (dir.exists()){ dirNames << path; } else{ return 0; } bool isTopDir = true; //遍历各级文件夹,并将这些文件夹中的文件删除 for (int i = 0; i<dirNames.size(); ++i) { dir.setPath(dirNames[i]); filst = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::NoDotAndDotDot , QDir::Name); if (isTopDir){ isTopDir = false; for (int i = 0; i < filst.size(); i++) { if (filst[i].fileName() == "assets" || filst[i].fileName() == "bin" || filst[i].fileName() == "libs"){ filst.removeAt(i); i--; } } } if (filst.size()>0){ curFi = filst.begin(); while (curFi != filst.end()) { //遇到文件夹,则添加至文件夹列表dirs尾部 if (curFi->isDir()){ dirNames.push_back(curFi->filePath()); } else if (curFi->isFile()){ if (curFi->absoluteFilePath().toLower().endsWith(".java")){ if (!ReplaceAppPakNameInJava(path, curFi->absoluteFilePath(), oldName, newName)){ return 1; } } else if (curFi->absoluteFilePath().toLower().endsWith(".xml")){ if (!ReplaceAppPakNameInXml(path, curFi->absoluteFilePath(), oldName, newName)){ return 1; } } } curFi++; }//end of while } } if (!ReplaceAppPakNameInManifest(path, oldName, newName)){ return 5; } return 0; }
qint64 Dialog::countSize(QString filePath) { qint64 fileSize = 0; QList<QString> fileList; QFileInfo fileInfo(filePath); QString strTopDir; if(fileInfo.isDir()) {//文件夹加入堆栈 m_bFolder = true; if (filePath.contains('/')) { QStringList strList = filePath.split("/"); int iSize = strList.size(); strTopDir = strList.at(iSize-1); } else if(filePath.contains('\\')) { QStringList strList = filePath.split("\\"); int iSize = strList.size(); strTopDir = strList.at(iSize-1); } fileList.push_back(filePath); } else {//如果是文件的话就取文件的特特征码,文件特征码用文件的创建,最后的修改时间字符串和文件的路径取MD5 m_bFolder = false; m_fileList.push_front(filePath); fileSize = fileInfo.size(); QString createTime = fileInfo.created().toString(); QString modifiedTime = fileInfo.lastModified().toString(); QString checkString = createTime + modifiedTime; checkString += filePath; //m_fileCheck = MD5String(checkString.toLocal8Bit().data()); } while(!fileList.isEmpty()){//判断堆栈中是否有文件夹 QString filePath = fileList.value(0);//取堆栈的顶部位置 QDir dir(filePath);//文件夹信息 dir.setFilter(QDir::Dirs|QDir::Files|QDir::Hidden|QDir::System); dir.setSorting(QDir::DirsFirst); QFileInfoList list = dir.entryInfoList();//去得文件夹的文件信息 QFileInfoList::iterator iter; if (2 == list.size()) { qDebug() << "empty dir : " << filePath; m_emptyFolderList.push_back(filePath); } for(iter=list.begin(); iter!=list.end(); iter++) {//遍历文件夹内部 //m_stopMutex.lock(); // if(m_bStop) { // m_stopMutex.unlock(); // return -1; // } // m_stopMutex.unlock(); //qDebug() << "filename : " << iter->fileName(); if(iter->fileName() == "." || iter->fileName() == "..") { continue; } if(iter->isDir()) {//如果为文件夹的话加入到堆栈 fileList.push_back(iter->filePath()); } else {//如果是文件的话,取到文件的大小,并且把文件加入到发送list中 fileSize += iter->size(); //QString str m_fileList.push_front(iter->filePath()); } } fileList.pop_front();//堆栈顶部的元素已经处理完毕,出堆栈 } // if(m_fileCheck.size() == 0) {//文件夹的特征码是文件的所有路径取MD5 // QString checkString; // QList<QString>::iterator iter; // for(iter=m_fileList.begin(); iter!=m_fileList.end(); iter++) { // checkString += (*iter); // } // m_fileCheck = MD5String(checkString.toLocal8Bit().data()); // } qDebug() << "file or director size : " << fileSize; return fileSize; }