Cocos2d-x 3.13.1 Unzip and save a folder downloaded through Downloader

Hello,

I am trying to download the resources of my game from a server in form of a zip file containing various images and spritesheets and use them in the game. I am able to download the .zip file from the server through Downloader class but I am stuck at decompressing and saving the downloaded folder. Can you point me in the right direction where I can find a sample or some documentation regarding the problem?

Thanks in advance…!! :]

you mean this? http://www.cocos2d-x.org/wiki/Assets_manager

Hello @energyy I’m looking for a method to unzip a file as my project would not be containing the version menifest on server side.

can you share code how are you decompressing. I will have a look

Hello @marslan22 by problem I meant I am not able to find any sample of unzipping a zip file.

I downloaded file using HttpRequest and on its response here is my code to unzip and save files. let me know if this helps. You can see this class cocos2d::ZipFile for more details

void FileDownloader::fileRequestCompleted(cocos2d::network::HttpClient* client, cocos2d::network::HttpResponse* response)
{
    std::vector<char> *buffer = response->getResponseData();
    if (buffer->size() == 0)
        Log("error");

    cocos2d::ZipFile *zFile = cocos2d::ZipFile::createWithBuffer(&buffer->front(), buffer->size());

    if(!zFile)
       Log("zip file corrupt");

    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/";

    if (CommonUtils::isDirectory(directoryName) && !cocos2d::FileUtils::getInstance()->isDirectoryExist(directoryName))
    {
        cocos2d::FileUtils::getInstance()->createDirectory(directoryName);
    }

    while (data != nullptr)
    {
    
        std::string fullFileName = directoryName + file;
    
        if (CommonUtils::isDirectory(fullFileName) && !cocos2d::FileUtils::getInstance()->isDirectoryExist(fullFileName))
       {
            cocos2d::FileUtils::getInstance()->createDirectory(fullFileName);
       }
    
       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);
    
    }

}

Hello @marslan22 its done, Thanks…!!

Hello @Durlabh08 . I need same as you did for my game ?
How you accomplished this . will you please guide me
Thanks

Hello @Apoorva,

I have used the uncompressing code from AssetsManager class, please check it out. Let me know if this doesn’t work for you.

Hi @Durlabh08

void HelloWorld::downLoadZIP_From_Server(){

std::string url = "zip file Link";
std::string filePath = FileUtils::getInstance()->getWritablePath() + "newgameimages/";

auto *downloader = new (std::nothrow) cocos2d::network::Downloader();

downloader->onTaskProgress = ([=](const cocos2d::network::DownloadTask& task, int64_t bytesReceived, int64_t totalBytesReceived, int64_t totalBytesExpected) {
	
	CCLOG("Task Progress");

});

downloader->onFileTaskSuccess = ([=](const cocos2d::network::DownloadTask& task) {

	CCLOG("Sucess");

	std::string filePath = FileUtils::getInstance()->getWritablePath() + "newgameimages";


});

downloader->onTaskError = ([=](const cocos2d::network::DownloadTask& task, int errorCode, int errorCodeInternal, const std::string& errorStr) {
	
	CCLOG("download failed with error: %s error code: %i error code internal: %i", errorStr.c_str(), errorCode, errorCodeInternal);

});

downloader->createDownloadFileTask(url, filePath);

}

Here is the code for Downloader. But i am confuse what to do in onFileTaskSuccess CallBack .
I read Assestmanager Class Also . But don’t get any idea
Thanks