123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #include "ZipUnpack.h"
- #include "stdio.h"
- #include "sys/stat.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstring>
- using namespace std;
- using namespace cocos2d;
- #define BUFFER_SIZE_MAX 8192
- #define MAX_FILENAME_PATH 512
- ZipUnpack::ZipUnpack()
- {
- _unzFile= nullptr;
- }
- void ZipUnpack::merger(std::string dir,const std::string outFilePath)
- {
- long long read_size = 0;
- std::string buffTotal = "";
- std::string outFileDir = basename(outFilePath);
- auto fileUtils = FileUtils::getInstance();
- if (!fileUtils->isDirectoryExist(outFileDir)) {
- if (!fileUtils->createDirectory(outFileDir)) {
- return;
- }
- }
- for (int i = 0; i < 1000; i++)
- {
- std::string openFilePath = dir + to_string(i);
- bool isFileExist = fileUtils->isFileExist(openFilePath.c_str());
- if (!isFileExist)
- {
- break;
- }
- long long curlen = fileUtils->getFileSize(openFilePath);
- read_size += curlen;
- auto buff = fileUtils->getStringFromFile(openFilePath);
- buffTotal += buff;
- }
- if (read_size > 0)
- {
- fileUtils->writeStringToFile(buffTotal, outFilePath);
- }
- }
- bool ZipUnpack::unpack(std::string zip, std::string unpackPath)
- {
- auto _fileUtils = FileUtils::getInstance();
- const std::string rootPath = unpackPath + "/";
- unzFile zipfile = unzOpen(zip.c_str());
- if (!zipfile)
- {
- return false;
- }
- unz_global_info global_info;
- if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)
- {
- unzClose(zipfile);
- return false;
- }
- char readBuffer[BUFFER_SIZE_MAX];
- uLong i;
- for (i = 0; i < global_info.number_entry; ++i)
- {
- unz_file_info fileInfo;
- char fileName[MAX_FILENAME_PATH];
- if (unzGetCurrentFileInfo(zipfile,
- &fileInfo,
- fileName,
- MAX_FILENAME_PATH,
- NULL,
- 0,
- NULL,
- 0) != UNZ_OK)
- {
- unzClose(zipfile);
- return false;
- }
- const std::string fullPath = rootPath + fileName;
- const size_t filenameLength = strlen(fileName);
- if (fileName[filenameLength - 1] == '/')
- {
- if (!_fileUtils->createDirectory(basename(fullPath)))
- {
- unzClose(zipfile);
- return false;
- }
- }
- else
- {
- std::string dir = basename(fullPath);
- if (!_fileUtils->isDirectoryExist(dir)) {
- if (!_fileUtils->createDirectory(dir)) {
- unzClose(zipfile);
- return false;
- }
- }
- if (unzOpenCurrentFile(zipfile) != UNZ_OK)
- {
- unzClose(zipfile);
- return false;
- }
- FILE *out = fopen(FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str(), "wb");
- if (!out)
- {
- unzCloseCurrentFile(zipfile);
- unzClose(zipfile);
- return false;
- }
- int error = UNZ_OK;
- do
- {
- error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE_MAX);
- if (error < 0)
- {
- fclose(out);
- unzCloseCurrentFile(zipfile);
- unzClose(zipfile);
- return false;
- }
- if (error > 0)
- {
- fwrite(readBuffer, error, 1, out);
- }
- } while (error > 0);
- fclose(out);
- }
- unzCloseCurrentFile(zipfile);
- if ((i + 1) < global_info.number_entry)
- {
- if (unzGoToNextFile(zipfile) != UNZ_OK)
- {
- unzClose(zipfile);
- return false;
- }
- }
- }
- unzClose(zipfile);
- return true;
- }
- std::string ZipUnpack::basename(const std::string& path)
- {
- size_t found = path.find_last_of("/\\");
- if (std::string::npos != found)
- {
- return path.substr(0, found);
- }
- else
- {
- return path;
- }
- }
|