方法是匯出ZipUtils,在ZipUtils裡面新增uncompressDir的方法給lua用
bool ZipUtils::uncompressDir(char* filename, char* destPath)
{
if (filename == NULL || destPath == NULL)
CCLOG("source zip is null or dest path is null!");
return false;
}
// Open the zip file
std::string outFileName = filename;
unzFile zipfile = unzOpen(outFileName.c_str());
if (!zipfile)
CCLOG("can not open downloaded zip file %s", outFileName.c_str());
// 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.c_str());
unzClose(zipfile);
const int BUFFER_SIZE = 8192;
const int MAX_FILENAME = 512;
// 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
// Get info about current file.
unz_file_info fileInfo;
char fileName[MAX_FILENAME];
if (unzGetCurrentFileInfo(zipfile,
&fileInfo,
fileName,
MAX_FILENAME,
NULL,
0,
0) != UNZ_OK)
CCLOG("can not read file info");
std::string storagePath = destPath;
std::string fullPath = storagePath + fileName;
// Check if this entry is a directory or a file.
const size_t filenameLength = strlen(fileName);
if (fileName[filenameLength - 1] == "/")
// get all dir
std::string fileNameStr = std::string(fileName);
size_t position = 0;
while ((position = fileNameStr.find_first_of("/", position)) != std::string::npos)
std::string dirPath = storagePath + fileNameStr.substr(0, position);
// Entry is a direcotry, so create it.
// If the directory exists, it will failed scilently.
if (!createDirectory(dirPath.c_str()))
CCLOG("can not create directory %s", dirPath.c_str());
//unzClose(zipfile);
//return false;
position++;
else
// Entry is a file, so extract it.
// Open current file.
if (unzOpenCurrentFile(zipfile) != UNZ_OK)
CCLOG("can not open file %s", fileName);
// Create a file to store current file.
FILE *out = fopen(fullPath.c_str(), "wb");
if (!out)
CCLOG("can not open destination file %s", fullPath.c_str());
unzCloseCurrentFile(zipfile);
// Write current file content to destinate file.
int error = UNZ_OK;
do
error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE);
if (error
CCLOG("can not read zip file %s, error code is %d", fileName, error);
if (error > 0)
fwrite(readBuffer, error, 1, out);
} while (error > 0);
fclose(out);
// Goto next entry listed in the zip file.
if ((i + 1)
if (unzGoToNextFile(zipfile) != UNZ_OK)
CCLOG("can not read next file");
CCLOG("end uncompressing");
return true;
方法是匯出ZipUtils,在ZipUtils裡面新增uncompressDir的方法給lua用
bool ZipUtils::uncompressDir(char* filename, char* destPath)
{
if (filename == NULL || destPath == NULL)
{
CCLOG("source zip is null or dest path is null!");
return false;
}
// Open the zip file
std::string outFileName = filename;
unzFile zipfile = unzOpen(outFileName.c_str());
if (!zipfile)
{
CCLOG("can not open downloaded zip file %s", outFileName.c_str());
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.c_str());
unzClose(zipfile);
return false;
}
const int BUFFER_SIZE = 8192;
const int MAX_FILENAME = 512;
// 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
{
// Get info about current file.
unz_file_info fileInfo;
char fileName[MAX_FILENAME];
if (unzGetCurrentFileInfo(zipfile,
&fileInfo,
fileName,
MAX_FILENAME,
NULL,
0,
NULL,
0) != UNZ_OK)
{
CCLOG("can not read file info");
unzClose(zipfile);
return false;
}
std::string storagePath = destPath;
std::string fullPath = storagePath + fileName;
// Check if this entry is a directory or a file.
const size_t filenameLength = strlen(fileName);
if (fileName[filenameLength - 1] == "/")
{
// get all dir
std::string fileNameStr = std::string(fileName);
size_t position = 0;
while ((position = fileNameStr.find_first_of("/", position)) != std::string::npos)
{
std::string dirPath = storagePath + fileNameStr.substr(0, position);
// Entry is a direcotry, so create it.
// If the directory exists, it will failed scilently.
if (!createDirectory(dirPath.c_str()))
{
CCLOG("can not create directory %s", dirPath.c_str());
//unzClose(zipfile);
//return false;
}
position++;
}
}
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);
return false;
}
// Create a file to store current file.
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;
}
// Write current file content to destinate file.
int error = UNZ_OK;
do
{
error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE);
if (error
{
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);
// Goto next entry listed in the zip file.
if ((i + 1)
{
if (unzGoToNextFile(zipfile) != UNZ_OK)
{
CCLOG("can not read next file");
unzClose(zipfile);
return false;
}
}
}
unzClose(zipfile);
CCLOG("end uncompressing");
return true;
}