cocos2d-x遍历zip某个目录(可用于android中apk包的文件访问)

利用cocos2d-x里封装的unZip进行遍历文件,取出每一个文件

 1 vector<string> ToolUtils::getFiles( const char* zipFile, const char* floder )
 2 {
 3     vector<string> files;
 4     unsigned char * pBuffer = NULL;
 5     unzFile pFile = NULL;
 6     string rejustFloder(floder);
    //floder必须以/结尾
7 if(rejustFloder.find_last_not_of("/")) { 8 rejustFloder = rejustFloder + "/"; 9 floder = rejustFloder.c_str(); 10 } 11 do 12 { 13 CC_BREAK_IF(!zipFile || !floder); 14 CC_BREAK_IF(strlen(zipFile) == 0); 15 16 pFile = unzOpen(zipFile); 17 CC_BREAK_IF(!pFile); 18 19 int nRet = unzLocateFile(pFile, floder, 1); 20 CC_BREAK_IF(UNZ_OK != nRet); 21 22 while (unzGoToNextFile(pFile) == UNZ_OK) 23 { 24 char szFilePathA[260]; 25 unz_file_info FileInfo; 26 nRet = unzGetCurrentFileInfo(pFile, &FileInfo, szFilePathA, sizeof(szFilePathA), NULL, 0, NULL, 0); 27 CC_BREAK_IF(UNZ_OK != nRet); 28 string path(szFilePathA); //最后一个为/为目录过滤掉,且开头必须以文件开头 29 if(path[path.size() - 1] != '/' && path.find(floder) == 0) { 30 files.insert(files.begin(), string(szFilePathA)); 31 } 32 } 33 } while (0); 34 35 if (pFile) 36 { 37 unzClose(pFile); 38 } 39 40 return files; 41 }

这样我们zipFile就是某一个zip,在android平台上的就是getApkPath(),floder就是取zip里的相对路径。

 

posted @ 2013-05-25 22:51  TickDream  阅读(1752)  评论(2编辑  收藏  举报