记录一下在cocos2dx中读取zip和解压zip
1.读取zip
获取可读写入路径,把zip文件拷到可读写路径下,如下
bool ResourcesDecode::loadZIP(const std::string &zipFilename,const std::string &password/*""*/)
{
std::string filename = zipFilename;
std::string dataFilePath = FileUtils::getInstance()->getWritablePath() + filename;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
if(access(dataFilePath.c_str(), 0) != 0)
{
std::string strPath = FileUtils::getInstance()->fullPathForFilename(filename);
ssize_t len = 0;
unsigned char *data = 0;
CCLOG("strPath:%s",strPath.c_str());
data = FileUtils::getInstance()->getFileData(strPath.c_str(), "r", &len);
CCLOG("file:%s, len:%zd", dataFilePath.c_str(), len);
FILE* fp = fopen(dataFilePath.c_str(), "w+");
if(!fp)
{
CCLOG("file not found!");
}
fwrite(data, sizeof(char), len, fp);
fclose(fp);
delete []data;
data = 0;
}
#endif
//解压
unCompress(dataFilePath.c_str(),password);
return true;
}
上面参数中password为zip压缩文件密码,在使用资源之前调用loadZIP即可
2.解压zip
读取zip到内存,并且解压zip,如下