cocos2dx解压缩文件

来源:互联网 发布:mac如何删除文件夹 编辑:程序博客网 时间:2024/05/21 21:02

Uncommpress.h

#ifndef _UNCOMPRESS_H_#define _UNCOMPRESS_H_#include <pthread.h>#define BUFFER_SIZE    8192#define MAX_FILENAME   512#define THREAD_MAX 10class MyUncompress{public:MyUncompress();~MyUncompress();void createUncompressThread(const char* storagePath, const char* tempPackageFileName, const char *uncompressDone);private:static void* threadUncompress(void* p); bool uncompress();bool createDirectory(const char *path);private:pthread_t _tid[THREAD_MAX];pthread_mutex_t mutex;char _storagePath[MAX_FILENAME];char _tempPackageFileName[MAX_FILENAME];char _uncompressDone[MAX_FILENAME];int _mutexInitResult;};#endif//_UNCOMPRESS_H_

Uncommpress.cpp

#include "Uncompress.h"#include "cocos2d.h"using namespace cocos2d;#include "support/zip_support/unzip.h"#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)#include <dirent.h>//#include <sys/stat.h>#endif#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)#include <sys/types.h>#include <sys/stat.h>#include <errno.h>#endif/*#define BUFFER_SIZE    8192#define MAX_FILENAME   512*/char gStoragePath[MAX_FILENAME] = {0};char gTempPackageFileName[MAX_FILENAME] = {0};char gUncompressDone[MAX_FILENAME] = {0};MyUncompress::MyUncompress(){_mutexInitResult = -1;//pthread_mutex_init(&mutex,NULL);}MyUncompress::~MyUncompress(){if(0 == _mutexInitResult)pthread_mutex_destroy(&mutex);}void* MyUncompress::threadUncompress(void *data){MyUncompress* self = (MyUncompress*)data;pthread_mutex_lock(&self->mutex);//CCLog("=======================================================begin");do{memset(gStoragePath,0,sizeof(gStoragePath));memset(gTempPackageFileName,0,sizeof(gTempPackageFileName));memset(gUncompressDone,0,sizeof(gUncompressDone));strcpy(gStoragePath,self->_storagePath);strcpy(gTempPackageFileName,self->_tempPackageFileName);strcpy(gUncompressDone,self->_uncompressDone);if (!self->uncompress()){break;}}while(0);//CCLog("=======================================================end");pthread_mutex_unlock(&self->mutex);return NULL;}void MyUncompress::createUncompressThread(const char* storagePath, const char* tempPackageFileName, const char *uncompressDone){memset(_storagePath,0,sizeof(_storagePath));memset(_tempPackageFileName,0,sizeof(_tempPackageFileName));memset(_uncompressDone,0,sizeof(_uncompressDone));strcpy(_storagePath,storagePath);strcpy(_tempPackageFileName,tempPackageFileName);strcpy(_uncompressDone,uncompressDone);//CCLog("++++++++++++++++++++++++++++++++++++_mutexInitResult = %d",_mutexInitResult);if(0 != _mutexInitResult){_mutexInitResult = pthread_mutex_init(&mutex,NULL);if(0 != _mutexInitResult)return;}//CCLog("------------------------------------_mutexInitResult = %d",_mutexInitResult);for(int i=0; i<THREAD_MAX;i++){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)if(_tid[i].p == NULL)#elseif(_tid[i] == 0)#endif{pthread_create(&_tid[i], NULL, threadUncompress, this);break;}}}bool MyUncompress::uncompress(){//CCLog("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa tempPackageFileName = %s,uncompressDone = %s\n",gTempPackageFileName,gUncompressDone);//pthread_mutex_lock(&mutex);char outFileName[MAX_FILENAME] = {0};strcpy(outFileName,gStoragePath);strcat(outFileName,gTempPackageFileName);unzFile zipfile = unzOpen(outFileName);if (! zipfile){CCLOG("can not open downloaded zip file %s", outFileName);//pthread_mutex_unlock(&mutex);return false;}// Get info about the zip fileunz_global_info global_info;if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK){CCLOG("can not read file global info of %s", outFileName);unzClose(zipfile);//pthread_mutex_unlock(&mutex);return false;}// Buffer to hold data read from the zip filechar readBuffer[BUFFER_SIZE];CCLOG("start uncompressing");// Loop to extract all files.uLong i;for (i = 0; i < global_info.number_entry; ++i){// Get info about current file.unz_file_info fileInfo;char fileName[MAX_FILENAME] = {0};if (unzGetCurrentFileInfo(zipfile,&fileInfo,fileName,MAX_FILENAME,NULL,0,NULL,0) != UNZ_OK){CCLOG("can not read file info");unzClose(zipfile);//pthread_mutex_unlock(&mutex);return false;}//CCLog("*********************************************can not read file info");char fullPath[2*MAX_FILENAME] = {0};strcpy(fullPath,gStoragePath);strcat(fullPath,fileName);//CCLOG("fullPath = %s",fullPath);// Check if this entry is a directory or a file.const size_t filenameLength = strlen(fileName);if (fileName[filenameLength-1] == '/'){// Entry is a direcotry, so create it.// If the directory exists, it will failed scilently.if (!createDirectory(fullPath)){CCLOG("can not create directory %s", fullPath);unzClose(zipfile);//pthread_mutex_unlock(&mutex);return false;}}else{// Entry is a file, so extract it.// Open current file.if (unzOpenCurrentFile(zipfile) != UNZ_OK){CCLOG("can not open file %s", fileName);unzClose(zipfile);//pthread_mutex_unlock(&mutex);return false;}// Create a file to store current file.FILE *out = fopen(fullPath, "wb");if (! out){CCLOG("can not open destination file %s", fullPath);unzCloseCurrentFile(zipfile);unzClose(zipfile);//pthread_mutex_unlock(&mutex);return false;}// Write current file content to destinate file.int error = UNZ_OK;do{error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE);if (error < 0){CCLOG("can not read zip file %s, error code is %d", fileName, error);unzCloseCurrentFile(zipfile);unzClose(zipfile);//pthread_mutex_unlock(&mutex);return false;}if (error > 0){fwrite(readBuffer, error, 1, out);}} while(error > 0);fclose(out);}unzCloseCurrentFile(zipfile);// Goto next entry listed in the zip file.if ((i+1) < global_info.number_entry){if (unzGoToNextFile(zipfile) != UNZ_OK){CCLOG("can not read next file");unzClose(zipfile);//pthread_mutex_unlock(&mutex);return false;}}}//解压完成,移除压缩包if (remove(outFileName) != 0){CCLOG("can not remove downloaded zip file %s", outFileName);}else{CCLOG("remove downloaded zip file %s", outFileName);}//解压完成,将其写入xmlCCUserDefault::sharedUserDefault()->setBoolForKey(gUncompressDone,true);CCUserDefault::sharedUserDefault()->flush();//CCLog("cccccccccccccccccccccccccccccccccccccccccc tempPackageFileName = %s,uncompressDone = %s\n",gTempPackageFileName,gUncompressDone);//pthread_mutex_unlock(&mutex);CCLOG("end uncompressing\n");return true;}bool MyUncompress::createDirectory(const char *path){#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)mode_t processMask = umask(0);int ret = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);umask(processMask);if (ret != 0 && (errno != EEXIST)){return false;}return true;#elseBOOL ret = CreateDirectoryA(path, NULL);if (!ret && ERROR_ALREADY_EXISTS != GetLastError()){return false;}return true;#endif}


0 0