Cocos 2.2.3 zip文件解压缩

环境配置:
1.包含头文件#include "support/zip_support/unzip.h"
2.项目——属性——C/C++——附加包含目录——D:\cocos2dx-xtc\cocos2dx\platform\third_party\win32\zlib(自己添加对应路径即可)
3.包含如下头文件,用于android中创建文件夹
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#endif

解压缩代码:
bool uncompress(char * pOutFileName)
{	
	//outFileName为要解压缩的文件名
	//就是D:\\cocos2dx-xtc\\projects\\dlStudy0\\proj.win32\\Debug.win32\\A.zip
	//即CCFileUtils::sharedFileUtils()->getWritablePath() + “A.zip"
	string outFileName = pOutFileName;			
	
	// 打开压缩文件
	unzFile zipfile = unzOpen(outFileName.c_str());
	if (! zipfile)
	{
		cclog("can not open downloaded zip file %s",outFileName.c_str());
		return false;
	}

	// 获取zip文件信息
	unz_global_info global_info;
	if (unzGetGlobalInfo(zipfile,&global_info) != UNZ_OK)
	{
		cclog("can not read file global info of %s",outFileName.c_str());
		unzClose(zipfile);
		return false;
	}

	// 临时缓存,用于从zip中读取数据,然后将数据给解压后的文件
	// BUFFER_SIZE == 8192
	char readBuffer[BUFFER_SIZE];

	//开始解压缩
	cclog("start uncompressing");

	//创建解压缩后的文件夹
	//即在A.zip路径下创建命名为A的文件夹用于存放解压出来的文件
	string storageDir;
	int pos = outFileName.find_last_of(".");
	storageDir = outFileName.substr(0,pos).append("\\");
	createDirectory(storageDir.c_str());

	// 循环提取压缩包内文件
	// global_info.number_entry为压缩包内文件个数
	uLong i;
	for (i = 0; i < global_info.number_entry; ++i)
	{
		// 获取压缩包内的文件名
		unz_file_info fileInfo;
		char fileName[MAX_FILENAME];    // MAX_FILENAME == 512
		if (unzGetCurrentFileInfo(zipfile,&fileInfo,fileName,MAX_FILENAME,NULL,0) != UNZ_OK)
		{
			cclog("can not read file info");
			unzClose(zipfile);
			return false;
		}

		//该文件最终存放路径
		string fullPath = storageDir + fileName;

		// 检测路径是文件夹还是文件
		const size_t filenameLength = strlen(fileName);
		if (fileName[filenameLength-1] == '/')
		{
			// 该文件一个文件夹,那么就创建它			
			if (!createDirectory(fullPath.c_str()))
			{
				cclog("can not create directory %s",fullPath.c_str());
				unzClose(zipfile);
				return false;
			}
		}
		else
		{
			// 该文件一个文件,那么就提取创建它

			// 打开压缩文件
			if (unzOpenCurrentFile(zipfile) != UNZ_OK)
			{
				cclog("can not open file %s",fileName);
				unzClose(zipfile);
				return false;
			}

			// 创建目标文件
			FILE *out = fopen(fullPath.c_str(),"wb");
			if (! out)
			{
				cclog("can not open destination file %s",fullPath.c_str());
				unzCloseCurrentFile(zipfile);
				unzClose(zipfile);
				return false;
			}

			// 将压缩文件内容写入目标文件
			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",error);
					unzCloseCurrentFile(zipfile);
					unzClose(zipfile);
					return false;
				}

				if (error > 0)
				{
					fwrite(readBuffer,error,1,out);
				}
			} while(error > 0);

			fclose(out);
		}

		//关闭当前被解压缩的文件
		unzCloseCurrentFile(zipfile);

		// 如果zip内还有其他文件,则将当前文件指定为下一个待解压的文件
		if ((i+1) < global_info.number_entry)
		{
			if (unzGoToNextFile(zipfile) != UNZ_OK)
			{
				cclog("can not read next file");
				unzClose(zipfile);
				return false;
			}
		}
	}

	//压缩完毕
	cclog("end uncompressing");

	//压缩完毕删除zip文件删除前要先关闭
	unzClose(zipfile);
	if (remove(outFileName.c_str()) != 0)
	{
		cclog("can not remove downloaded zip file %s",outFileName.c_str());
	}

	return true;
}

//用于创建文件夹,ex.A.zip解压后需创建名为A的文件夹,解压代码中有用到
bool 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
}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...