cocos2dx解压缩文件
2015-07-07 09:55
651 查看
Uncommpress.h
Uncommpress.cpp
#ifndef _UNCOMPRESS_H_ #define _UNCOMPRESS_H_ #include <pthread.h> #define BUFFER_SIZE 8192 #define MAX_FILENAME 512 #define THREAD_MAX 10 class 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) #else if(_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 file unz_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 file char 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); } //解压完成,将其写入xml CCUserDefault::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; #else BOOL ret = CreateDirectoryA(path, NULL); if (!ret && ERROR_ALREADY_EXISTS != GetLastError()) { return false; } return true; #endif }
相关文章推荐
- 修改VS2012代码补全快捷键 分类: cocos2d安装设置 2015-07-07 09:12 14人阅读 评论(0) 收藏
- Cocos发展Visual Studio下一个libcurl图书馆开发环境的搭建
- [Cocos2d-x]在Cocos2d-x 3.x如何通过版本号WebSocket连接server数据的传输
- 第8章Cocos2D-X中的瓦片地图集(总结)8.1 瓦片地图集及编辑器简介
- Cocos2d-x HelloWorld 之源码分析
- Xcode安装Cocos2d-iphone
- Cocos2d-x学习笔记(两)Cocos2d-x总体框架
- cocos2d-x-3.2 利用cocos.py创建项目
- quick cocos2d-x 3.5打包安卓apk之Eclipse篇
- 教你pomeloclient包libpomelo增加cocos2d-x 3.0工程(Windows、Android、IOS平台)
- Mac os 下 cocos2dx-3.2
- cocos2dx3.4-android项目eclipse运行报错CDT',Properties下c/c++build无法打开
- 在Windows7上搭建Cocos2d-x win32开发环境
- quickcocoslua中继承的父类接口调用
- cocos2dx 3.6 UI 之 Button
- cocos2dx要点总结
- C++调用JS,可变参模板实现,方便调用(cocos2d-x)
- 为什么Cocos2D开源引擎最适合游戏创业团队?
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十六课:Cocos2D-X物理引擎之Box2D11-12
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十五课:Cocos2D-X物理引擎之Box2D8-10