您的位置:首页 > 移动开发 > Cocos引擎

Cocos 2.2.3 zip文件解压缩

2016-02-25 12:16 459 查看
环境配置:
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,
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", fileName, 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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: