我改完了复制过去替换原来的就能用了
.h
//
// DJZipArchive.h
// TestUI
//
// Created by 杜甲 on 14-5-3.
//
//
#ifndef __TestUI__DJZipArchive__
#define __TestUI__DJZipArchive__
#include "cocos2d.h"
#include "unzip/unzip.h"
#include "cocos-ext.h"
USING_NS_CC;
class DJZipArchive
{
public:
DJZipArchive();
~DJZipArchive();
bool unzipOpenFile(const char* zipFile);
bool unzipFileToAndOverWrite(std::string path,bool overwrite);
bool createDirectory(const char *path);
private:
unzFile _unzFile;
std::string _password;
};
#endif /* defined(__TestUI__DJZipArchive__) */
.cpp
//
// DJZipArchive.cpp
// TestUI
//
// Created by 杜甲 on 14-5-3.
//
//
#include "DJZipArchive.h"
#include "stdio.h"
#include "sys/stat.h"
DJZipArchive::DJZipArchive()
{
_unzFile= nullptr;
}
bool DJZipArchive::unzipOpenFile(const char *zipFile)
{
_unzFile = cocos2d::unzOpen(zipFile);
if (_unzFile)
{
unz_global_info globalInfo = {0};
if (unzGetGlobalInfo(_unzFile, &globalInfo) == UNZ_OK)
{
log("%lu entries in the zip file",globalInfo.number_entry);
}
}
return _unzFile != nullptr;
}
bool DJZipArchive::unzipFileToAndOverWrite(std::string path, bool overwrite)
{
bool success = true;
int ret = unzGoToFirstFile(_unzFile);
unsigned char buffer[4096] = {0};
do {
if (_password.size() == 0)
ret = unzOpenCurrentFile(_unzFile);
else
ret = unzOpenCurrentFilePassword(_unzFile, _password.c_str());
if (ret != UNZ_OK)
{
success = false;
break;
}
// reading data and write to file
int read ;
unz_file_info fileInfo = {0};
ret = unzGetCurrentFileInfo(_unzFile, &fileInfo, nullptr, 0, nullptr, 0, nullptr, 0);
if (ret != UNZ_OK) {
success = false;
unzCloseCurrentFile(_unzFile);
}
char* filename = (char*)malloc(fileInfo.size_filename + 1);
unzGetCurrentFileInfo(_unzFile, &fileInfo, filename, fileInfo.size_filename + 1, nullptr, 0, nullptr, 0);
filename[fileInfo.size_filename] = '\0';
log("filename = %s",filename);
bool isDirectory = false;
if (filename[fileInfo.size_filename - 1] == '/' || filename[fileInfo.size_filename - 1] == '\\')
isDirectory = true;
free(filename);
std::string fullPath = path + StringUtils::format("%s",filename);
log("%s",path.c_str());
if (isDirectory) {
this->createDirectory(fullPath.c_str());
}
FILE* fp = fopen(fullPath.c_str(), "wb");
while (fp)
{
read = unzReadCurrentFile(_unzFile, buffer, 4096);
if (read > 0) {
fwrite(buffer, read, 1, fp);
}
else if (read < 0)
{
break;
}
else
break;
}
if (fp)
{
fclose(fp);
}
unzCloseCurrentFile(_unzFile);
ret = unzGoToNextFile(_unzFile);
}
while (ret == UNZ_OK && UNZ_OK != UNZ_END_OF_LIST_OF_FILE);
return success;
}
//创建子目录
bool DJZipArchive::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
}