Can not unzip file with cocos::ZipFile(filePath)

Can not unzip file with cocos::ZipFile(filePath)
0
#1

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);

    }

}
Cocos2d-x 3.17.1 -> Make sure to update deps!
#2

What is happening?

How far do you get in this code?

what version of cocos2d-x?

what operating system?

any errors in the console?

#3

we have one zip file in server we wants to download it and then unzip it and wants to assets,

it was working before we have tested, now it’s downloading successfully, but

from the line it’s returning null, means it can’t find out zip even though we are providing exactly path,

cocos2dx 3.17.1
windows 10
no error it’s just returning null zip file

#4

What does filePath contain? just null?

Can you verify the file is even where it was downloaded to?

What change did you make to where it worked and then now doesn’t work? Did you update cocos2d-x versions? From what version did you upgrade from?

#5

yeah we have checked file there only, it just not unzipping.

we were using 3.17.0 and upgraded to 3.17.1

#6

I don’t see any GitHub issues already open about this. You should make one.

If you revert to 3.17.0 does it start working again?

#7

we have not tried with 3.17.0 after this issue, and have you tried this code, please check once and see you can help us :slight_smile:

#8

So it was working in 3.17.0 is what I am asking?

#9

yeah it was :slight_smile:

#10

my advice is to go back to 3.17.0 for now. If this is a bug it wont be fixed right away, AFAIK.

#11

thank you, I will try if you find out solution let us know

#12

@parth3535 I’m almost certain it’s this issue and the fix is to update the cocos2d-x 3rd party libraries. Use v3-deps-156 since it seems to have worked for others.

2 Likes
#13

@R101. Nice eyes, I didn’t see that. Thank you.

1 Like
#14

thanks we will try it and let you know whatever happens

#15

It’s an issue I came across when I updated my project to use v3.17.1 too, so it’s still fresh in my mind. :slight_smile: A few others seem to have come across it when they were trying to use OBB files on Android, but it’s a more general issue related to the 3rd party zip library.

#16

@R101. I think we need to let users know this is a must step: https://discuss.cocos2d-x.org/t/cocos2d-x-3-17-1-make-sure-to-update-deps/

1 Like
#17

Couldn’t agree more. I think there are several posts about this already from users asking for help. They won’t see it as an issue on Github either since it doesn’t show closed issues by default.

1 Like
#18

i was gonna ask for this

#19

we have run the command from coco2dx root

and it downloaded v3-deps-153.zip what should we do now? will it work

#20

I don’t know if any versions prior had this fix. But download this and extract it overtop

https://github.com/cocos2d/cocos2d-x-3rd-party-libs-bin/archive/v3-deps-156.zip