LoginSignup
0
1

More than 5 years have passed since last update.

Cocos2d-xでのzipダウンロードと解凍

Last updated at Posted at 2017-11-17

はじめに

追加でデータをダウンロードして保存したい場合やデータを更新したい場合に重要

準備する事

  • Dropbox
  • GoogleDrive
  • レンタルサーバー

上記の自身が使用したいデータサーバー

zipダウンロードとzipの解凍

Download.ccp
#include "network/HttpRequest.h"
#include "network/HttpClient.h"

void Download::Data(){
    //Dropboxの場合
    //データを共有した後のアドレスを使う
    //https://www.dropbox.com/sh/269qylxhh1niprx/test.zip?dl=0
    //アクセスアドレス→sh/269qylxhh1niprx
    //ファイル名→test.zip
    std::string fullUrl = "https://dl.dropboxusercontent.com/sh/269qylxhh1niprx/test.zip";

    //GoogleDriveの場合
    //共有可能なリンクを取得する
    //https://drive.google.com/open?id=0ByUxbvgVU34iWVRYX5dxNFRWbTQ
    //アクセスアドレス変更箇所→0ByUxbvgVU34iWVRYX5dxNFRWbTQ
    std::string fullUrl = "https://drive.google.com/uc?id=0ByUxbvgVU34iWVRYX5dxNFRWbTQ";

    //レンタルサーバーの場合
    //zipのフルパスでアクセス可能
    std::string fullUrl = "http://test-site.com/data/test/test.zip" 

    auto request = new cocos2d::network::HttpRequest();
    request->setUrl(fullUrl.c_str());
    request->setRequestType(cocos2d::network::HttpRequest::Type::GET);
    request->setResponseCallback([=](cocos2d::network::HttpClient* client, cocos2d::network::HttpResponse* response){
        if (response->isSucceed()) {
            if(response->getResponseCode() == 200){
                // ファイルへ保存せずに、そのまま展開する
                unzip(&response->getResponseData()->at(0), response->getResponseData()->size());

            }else{
                CCLOG("%s", response->getHttpRequest()->getUrl());
                CC_ASSERT(0);
            }
        }
    });

    cocos2d::network::HttpClient::getInstance()->send(request);
    request->release();
}

void Download::unzip(const void* data, ssize_t datasize){
    // 保存先のルートパスを取得
    std::string path = FileUtils::getInstance()->getWritablePath() + "download/";
    //ディレクトリ(フォルダー)がなければ作成
    if (!FileUtils::getInstance()->isDirectoryExist(path)) {
        FileUtils::getInstance()->createDirectory(path);
    }

    const std::string writablePath(path);

    // zipに含まれるファイル情報リストを取得
    ZipFile* zipfile = ZipFile::createWithBuffer(data, datasize);
    for(std::string filename = zipfile->getFirstFilename(); !filename.empty(); filename = zipfile->getNextFilename()){
        if(*filename.rbegin() == '/'){
            if(FileUtils::getInstance()->createDirectory(writablePath + filename)) {
                //log("CREATE FOLDER:%s", (writablePath + filename).c_str());
            }
        }else{
            ssize_t filesize;
            unsigned char* filedata = zipfile->getFileData(filename, &filesize);

            const std::string fullPath(writablePath + filename);

            FILE* file = fopen(fullPath.c_str(), "wb");
            fwrite(filedata, filesize, 1, file);
            fclose(file);
            free(filedata);
        }
    }
    delete zipfile;
}


Part1 ~HTTPリクエスト~ 参照のHTTPリクエストと違いTypeはGETにする

request->setRequestType(cocos2d::network::HttpRequest::Type::GET);

ダウンロードは無料のクラウドを使用することが可能
私の主観

  • Dropbox
    • ダウンロード速度は時間帯によってまちまち
    • 日本時間の22時から朝6時までは速い
    • それ以外の時間帯は遅くなる
  • GoogleDrive
    • ダウンロードは全時間帯安定する
    • 速さはDropboxの最速には及ばないが安定して速い
  • レンタルサーバー
    • レンタルサーバーの速度に依存
    • 国内からのアクセスは速いだろう

注意

Mac標準zipとwindows標準zipは違うかもしれない
この解凍はwin標準zipで確認している

次回

ファイルアップロード

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1