bool ExtractFile( char const* mpq_name, std::string const& filename ) { for(ArchiveSet::const_reverse_iterator i=gOpenArchives.rbegin(); i!=gOpenArchives.rend();++i) { HANDLE fileHandle; if (!SFileOpenFileEx(*i, mpq_name, SFILE_OPEN_PATCHED_FILE, &fileHandle)) continue; if (SFileGetFileSize(fileHandle, NULL) == 0) // some files removed in next updates and its reported size 0 { SFileCloseFile(fileHandle); return true; } SFileCloseFile(fileHandle); if (!SFileExtractFile(*i, mpq_name, filename.c_str(), SFILE_OPEN_PATCHED_FILE)) { printf("Can't extract file: %s\n", mpq_name); return false; } return true; } printf("Extracting file not found: %s\n", filename.c_str()); return false; }
bool MpkManip::cleanupFragment(const char* mpkFilename) { bool bOk = false; HANDLE hMpk = 0; char tempFilename[MAX_PATH]; if(SFileCreateArchiveEx(mpkFilename,OPEN_EXISTING,8000,&hMpk)) { uint nTotal = SFileGetFileInfo(hMpk,SFILE_INFO_NUM_FILES); uint nFiles = 0; char tempPath[MAX_PATH]; getTempPath(MAX_PATH, tempPath); getTempFileName(tempPath,tempFilename); HANDLE hTargetMpk = 0; if(SFileCreateArchiveEx(tempFilename,CREATE_ALWAYS,8000,&hTargetMpk)) { SFILE_FIND_DATA fileFindData; HANDLE hFind = SFileFindFirstFile(hMpk,"*",&fileFindData,0); if(hFind) { do { const char* pFilename = fileFindData.cFileName; char tempFilename[MAX_PATH]; getTempFileName(tempPath,tempFilename); if(SFileExtractFile(hMpk,pFilename,tempFilename)) { if(SFileAddFile(hTargetMpk,tempFilename,pFilename,MPK_FILE_REPLACEEXISTING | MPK_FILE_COMPRESS_PKWARE)) { nFiles++; } } } while(SFileFindNextFile(hFind,&fileFindData)); if(nTotal == nFiles)bOk = true; } SFileCloseArchive(hTargetMpk); } SFileCloseArchive(hMpk); } if(bOk && copyFile(tempFilename,mpkFilename,FALSE)) { bOk = true; } return bOk; }
//source和target都是文件名 bool MpkManip::extractFile(const char* source,const char* targetFilename,bool replaceExist) { bool bOk = false; CPathA sourceFilename; sourceFilename = source; CPathA path; path = targetFilename; CPathA parent(path.getParentDir()); if (!parent.empty()) createDirectoryRecursive(parent.c_str()); if(isFile(targetFilename) && !replaceExist)return true; return SFileExtractFile(m_hMpk,source,targetFilename) != FALSE; }