ZipUnpack.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "ZipUnpack.h"
  2. #include "stdio.h"
  3. #include "sys/stat.h"
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <cstring>
  8. using namespace std;
  9. using namespace cocos2d;
  10. #define BUFFER_SIZE_MAX 8192
  11. #define MAX_FILENAME_PATH 512
  12. ZipUnpack::ZipUnpack()
  13. {
  14. _unzFile= nullptr;
  15. }
  16. void ZipUnpack::merger(std::string dir,const std::string outFilePath)
  17. {
  18. long long read_size = 0;
  19. std::string buffTotal = "";
  20. std::string outFileDir = basename(outFilePath);
  21. auto fileUtils = FileUtils::getInstance();
  22. if (!fileUtils->isDirectoryExist(outFileDir)) {
  23. if (!fileUtils->createDirectory(outFileDir)) {
  24. return;
  25. }
  26. }
  27. for (int i = 0; i < 1000; i++)
  28. {
  29. std::string openFilePath = dir + to_string(i);
  30. bool isFileExist = fileUtils->isFileExist(openFilePath.c_str());
  31. if (!isFileExist)
  32. {
  33. break;
  34. }
  35. long long curlen = fileUtils->getFileSize(openFilePath);
  36. read_size += curlen;
  37. auto buff = fileUtils->getStringFromFile(openFilePath);
  38. buffTotal += buff;
  39. }
  40. if (read_size > 0)
  41. {
  42. fileUtils->writeStringToFile(buffTotal, outFilePath);
  43. }
  44. }
  45. bool ZipUnpack::unpack(std::string zip, std::string unpackPath)
  46. {
  47. auto _fileUtils = FileUtils::getInstance();
  48. const std::string rootPath = unpackPath + "/";
  49. unzFile zipfile = unzOpen(zip.c_str());
  50. if (!zipfile)
  51. {
  52. return false;
  53. }
  54. unz_global_info global_info;
  55. if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)
  56. {
  57. unzClose(zipfile);
  58. return false;
  59. }
  60. char readBuffer[BUFFER_SIZE_MAX];
  61. uLong i;
  62. for (i = 0; i < global_info.number_entry; ++i)
  63. {
  64. unz_file_info fileInfo;
  65. char fileName[MAX_FILENAME_PATH];
  66. if (unzGetCurrentFileInfo(zipfile,
  67. &fileInfo,
  68. fileName,
  69. MAX_FILENAME_PATH,
  70. NULL,
  71. 0,
  72. NULL,
  73. 0) != UNZ_OK)
  74. {
  75. unzClose(zipfile);
  76. return false;
  77. }
  78. const std::string fullPath = rootPath + fileName;
  79. const size_t filenameLength = strlen(fileName);
  80. if (fileName[filenameLength - 1] == '/')
  81. {
  82. if (!_fileUtils->createDirectory(basename(fullPath)))
  83. {
  84. unzClose(zipfile);
  85. return false;
  86. }
  87. }
  88. else
  89. {
  90. std::string dir = basename(fullPath);
  91. if (!_fileUtils->isDirectoryExist(dir)) {
  92. if (!_fileUtils->createDirectory(dir)) {
  93. unzClose(zipfile);
  94. return false;
  95. }
  96. }
  97. if (unzOpenCurrentFile(zipfile) != UNZ_OK)
  98. {
  99. unzClose(zipfile);
  100. return false;
  101. }
  102. FILE *out = fopen(FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str(), "wb");
  103. if (!out)
  104. {
  105. unzCloseCurrentFile(zipfile);
  106. unzClose(zipfile);
  107. return false;
  108. }
  109. int error = UNZ_OK;
  110. do
  111. {
  112. error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE_MAX);
  113. if (error < 0)
  114. {
  115. fclose(out);
  116. unzCloseCurrentFile(zipfile);
  117. unzClose(zipfile);
  118. return false;
  119. }
  120. if (error > 0)
  121. {
  122. fwrite(readBuffer, error, 1, out);
  123. }
  124. } while (error > 0);
  125. fclose(out);
  126. }
  127. unzCloseCurrentFile(zipfile);
  128. if ((i + 1) < global_info.number_entry)
  129. {
  130. if (unzGoToNextFile(zipfile) != UNZ_OK)
  131. {
  132. unzClose(zipfile);
  133. return false;
  134. }
  135. }
  136. }
  137. unzClose(zipfile);
  138. return true;
  139. }
  140. std::string ZipUnpack::basename(const std::string& path)
  141. {
  142. size_t found = path.find_last_of("/\\");
  143. if (std::string::npos != found)
  144. {
  145. return path.substr(0, found);
  146. }
  147. else
  148. {
  149. return path;
  150. }
  151. }