cocos2dx解压缩文件

原创 2015年07月07日 09:55:19

Uncommpress.h

#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
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

cocos2dx中zip包读取解压使用

记录一下在cocos2dx中读取zip和解压zip 1.读取zip 获取可读写入路径,把zip文件拷到可读写路径下,如下 bool ResourcesDecode::loadZIP(const st...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

cocos2dx 3.10 读取加密压缩ZIP文件

0,废话 最近需要把资源打包到压缩文件并加密,一开始是上网搜索怎么读取加密的ZIP文件的,很遗憾没有搜到读取加密的,只有一些读取没有加密的,于是就想着自己实现,刚开始网上了解了一下,大家都是使用z...

Cocos2d-x3.0 解压zip

2dx3.0为我们集成了unzip库,帮助我们实现对文件的
  • qqMCY
  • qqMCY
  • 2014年05月03日 20:02
  • 6335

IceDemos:环境搭建和HelloWorld

搭建ICE环境 1、linux下源码编译安装ice3.4.2, 参考帖子:http://www.php230.com/compile-and-install-linux-envir...

6 zeroice 的同步和异步调用

1 前言         软件模块之间总是存在着一定的接口,从调用方式上,可以把他们分为三类:同步调用、回调和异步调用。同步调用是一种阻塞式调用,调用方要等待对方执行完毕才返回,它是一种单向调用(on...

cocos2dx读取excel文件

  • 2016年04月22日 20:15
  • 4KB
  • 下载

cocos2dx android编译java文件

  • 2013年11月04日 19:49
  • 32KB
  • 下载

cocos2dx-新建工程时避免文件和文件夹的拷贝

cocos2dx-新建工程时避免文件和文件夹的拷贝 1.在D盘根目录上新建一个文件夹cocos2dxSource,再把D:\cocos2d-2.0-x-2.0.4目录下的cocos2...

cocos2dx简单布局文件

  • 2017年05月19日 16:10
  • 3KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:cocos2dx解压缩文件
举报原因:
原因补充:

(最多只允许输入30个字)