We are trying to download assets from the server and want to unzip the Zip file, it was working before but not it’s not working here is the sample code can anyone help us, thanks
void HelloWorld::startDownload() {
if(_downloadRunning){
log("One Download is in progress\n");
return;
}
//todo -> url of Zip file
std::string url = "http://slotgames.vetron.in//app/ph.zip";
filePath = FileUtils::getInstance()->getWritablePath() + "ph.zip";
_downloader = new (std::nothrow) network::Downloader();
_downloader->onTaskProgress = ([this](const network::DownloadTask& task, int64_t bytesReceived, int64_t totalBytesReceived, int64_t totalBytesExpected) {
CCLOG("download progress %.0f%%", (float) totalBytesReceived * 100.0 / totalBytesExpected );
});
_downloader->onFileTaskSuccess = ([this](const network::DownloadTask& task) {
CCLOG("downloaded success");
auto def = UserDefault::getInstance();
def->setBoolForKey(std::to_string(downloadingGameNumber).c_str(), true);
def->flush();
upZip();
delete _downloader;
_downloader = nullptr;
_task = nullptr;
_downloadRunning = false;
});
_downloader->onTaskError = ([](const network::DownloadTask& task, int errorCode, int errorCodeInternal, const std::string& errorStr) {
CCLOG("downloading error %s", errorStr.c_str());
});
_task = _downloader->createDownloadFileTask(url, filePath);
log("file storage at : %s\n",_task->storagePath.c_str());
_downloadRunning = true;
CCLOG("download started");
}
void HelloWorld::upZip()
{
cocos2d::ZipFile zFile = cocos2d::ZipFile(filePath);
std::string fileName = zFile.getFirstFilename();
std::string file = fileName;
ssize_t filesize;
unsigned char* data = zFile.getFileData(fileName, &filesize);
std::string directoryName = cocos2d::FileUtils::getInstance()->getWritablePath() + "data/slotgame/";
if (!cocos2d::FileUtils::getInstance()->isDirectoryExist(directoryName))
{
cocos2d::FileUtils::getInstance()->createDirectory(directoryName);
}
while (data != nullptr)
{
std::string fullFileName = directoryName + file;
log("filename DIR : %s\n",fullFileName.c_str());
if(fullFileName[fullFileName.size()-1] == '/'){
cocos2d::FileUtils::getInstance()->createDirectory(fullFileName);
free(data);
fileName = zFile.getNextFilename();
file = fileName;
data = zFile.getFileData(fileName, &filesize);
continue;
}
FILE *fp = fopen(fullFileName.c_str(), "wb");
if (fp)
{
fwrite(data, 1, filesize, fp);
fclose(fp);
}
free(data);
fileName = zFile.getNextFilename();
file = fileName;
data = zFile.getFileData(fileName, &filesize);
}
}