作者:HU
轉載請注明,原文鏈接:http://www.cnblogs.com/xioapingguo/p/4037323.html
cocos2dx中沒有直接解壓文件的,自己網上找了個,記錄一下。
static unsigned long _maxUnzipBufSize = 0x500000;
static bool unzip(const char *zipPath,const char *dirpath,const char *passwd)
{
CCLOG("unzip fullpath =%s",zipPath);
unzFile pFile = unzOpen(zipPath);
if(!pFile)
{
return false;
}
int err = unzGoToFirstFile(pFile);
bool ret = true;
while (err == UNZ_OK)
{
int nRet = 0;
int openRet = 0;
do
{
if(passwd)
{
openRet = unzOpenCurrentFilePassword( pFile,passwd);
CCLOG("openRet %d",openRet);
}
else
{
openRet = unzOpenCurrentFile(pFile);
}
CC_BREAK_IF(UNZ_OK != openRet);
unz_file_info FileInfo;
char szFilePathA[260];
nRet = unzGetCurrentFileInfo(pFile, &FileInfo, szFilePathA, sizeof(szFilePathA), NULL, 0, NULL, 0);
CC_BREAK_IF(UNZ_OK != nRet);
//如果szFilePathA為中文的話,請使用iocnv轉碼後再使用。
std::string newName = std::string(dirpath)+"/"+szFilePathA;
if (newName[newName.length()-1]=='/')
{
FileUtils::getInstance()->createDir(newName.c_str());
continue;
}
FILE* pFile2 = fopen(newName.c_str(), "w");
if (pFile2)
{
fclose(pFile2);
}
else
{
CCLOG("unzip can not create file");
return false;
}
unsigned long savedSize = 0;
while(pFile2 != NULL && FileInfo.uncompressed_size > savedSize)
{
unsigned char *pBuffer = NULL;
unsigned long once = FileInfo.uncompressed_size - savedSize;
if(once > _maxUnzipBufSize)
{
once = _maxUnzipBufSize;
pBuffer = new unsigned char[once];
}
else
{
pBuffer = new unsigned char[once];
}
int nSize = unzReadCurrentFile(pFile, pBuffer, once);
fwrite(pBuffer, once, 1, pFile2);
savedSize += nSize;
delete []pBuffer;
}
if (pFile2)
{
fclose(pFile2);
}
} while (0);
if(nRet != UNZ_OK)
{
ret = false;
}
else
{
unzCloseCurrentFile(pFile);
}
err = unzGoToNextFile(pFile);
}
if(err != UNZ_END_OF_LIST_OF_FILE)
{
ret = false;
}
unzClose(pFile);
return ret;
}
這個方法 壓縮文件中不能有中文文件名的文件,因為中文在window下zip後裡面的中文是用的gb2312編碼,而IOS中用的是utf8格式,這時unzip時文件名會出錯,導致解壓失敗。
如果非得一定要使用的話,要使用iconv庫進行轉碼。具體使用方法請參考http://www.cnblogs.com/hewei2012/p/3374147.html
創建目錄的方法:在FileUtils中加個方法
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#endif
void FileUtils::createDir(const char* p)
{
std::string path = p;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
mode_t processMask = umask(0);
int ret = false;
ret = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
umask(processMask);
if (ret != 0 && (errno != EEXIST))
{
CCLOG("create dir fail");
}
#else
BOOL ret = false;
ret = CreateDirectoryA(path.c_str(), NULL);
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
CCLOG("create dir fail");
}
#endif
}
有一個插件你還沒有運行
在目錄cocos2d-2.1beta3-x-2.1.1\template\msvc下找到文件InstallWizardForVS2010.js
運行即可!
精靈的動作由你控制
你把精靈addchild到layer裡面去
想要精靈做動作直接 用runAction函數