1
0

ZipUtils.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. // FIXME: hack, must be included before ziputils
  22. #ifdef MINIZIP_FROM_SYSTEM
  23. #include <minizip/unzip.h>
  24. #else // from our embedded sources
  25. #include "unzip.h"
  26. #endif
  27. #include "base/ZipUtils.h"
  28. #include <zlib.h>
  29. #include <assert.h>
  30. #include <stdlib.h>
  31. #include "base/CCData.h"
  32. #include "base/ccMacros.h"
  33. #include "platform/CCFileUtils.h"
  34. #include <map>
  35. // FIXME: Other platforms should use upstream minizip like mingw-w64
  36. #ifdef MINIZIP_FROM_SYSTEM
  37. #define unzGoToFirstFile64(A,B,C,D) unzGoToFirstFile2(A,B,C,D, NULL, 0, NULL, 0)
  38. #define unzGoToNextFile64(A,B,C,D) unzGoToNextFile2(A,B,C,D, NULL, 0, NULL, 0)
  39. #endif
  40. NS_CC_BEGIN
  41. unsigned int ZipUtils::s_uEncryptedPvrKeyParts[4] = {0,0,0,0};
  42. unsigned int ZipUtils::s_uEncryptionKey[1024];
  43. bool ZipUtils::s_bEncryptionKeyIsValid = false;
  44. // --------------------- ZipUtils ---------------------
  45. inline void ZipUtils::decodeEncodedPvr(unsigned int *data, ssize_t len)
  46. {
  47. const int enclen = 1024;
  48. const int securelen = 512;
  49. const int distance = 64;
  50. // check if key was set
  51. // make sure to call caw_setkey_part() for all 4 key parts
  52. CCASSERT(s_uEncryptedPvrKeyParts[0] != 0, "Cocos2D: CCZ file is encrypted but key part 0 is not set. Did you call ZipUtils::setPvrEncryptionKeyPart(...)?");
  53. CCASSERT(s_uEncryptedPvrKeyParts[1] != 0, "Cocos2D: CCZ file is encrypted but key part 1 is not set. Did you call ZipUtils::setPvrEncryptionKeyPart(...)?");
  54. CCASSERT(s_uEncryptedPvrKeyParts[2] != 0, "Cocos2D: CCZ file is encrypted but key part 2 is not set. Did you call ZipUtils::setPvrEncryptionKeyPart(...)?");
  55. CCASSERT(s_uEncryptedPvrKeyParts[3] != 0, "Cocos2D: CCZ file is encrypted but key part 3 is not set. Did you call ZipUtils::setPvrEncryptionKeyPart(...)?");
  56. // create long key
  57. if(!s_bEncryptionKeyIsValid)
  58. {
  59. unsigned int y, p, e;
  60. unsigned int rounds = 6;
  61. unsigned int sum = 0;
  62. unsigned int z = s_uEncryptionKey[enclen-1];
  63. do
  64. {
  65. #define DELTA 0x9e3779b9
  66. #define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (s_uEncryptedPvrKeyParts[(p&3)^e] ^ z)))
  67. sum += DELTA;
  68. e = (sum >> 2) & 3;
  69. for (p = 0; p < enclen - 1; p++)
  70. {
  71. y = s_uEncryptionKey[p + 1];
  72. z = s_uEncryptionKey[p] += MX;
  73. }
  74. y = s_uEncryptionKey[0];
  75. z = s_uEncryptionKey[enclen - 1] += MX;
  76. } while (--rounds);
  77. s_bEncryptionKeyIsValid = true;
  78. }
  79. int b = 0;
  80. int i = 0;
  81. // encrypt first part completely
  82. for(; i < len && i < securelen; i++)
  83. {
  84. data[i] ^= s_uEncryptionKey[b++];
  85. if(b >= enclen)
  86. {
  87. b = 0;
  88. }
  89. }
  90. // encrypt second section partially
  91. for(; i < len; i += distance)
  92. {
  93. data[i] ^= s_uEncryptionKey[b++];
  94. if(b >= enclen)
  95. {
  96. b = 0;
  97. }
  98. }
  99. }
  100. inline unsigned int ZipUtils::checksumPvr(const unsigned int *data, ssize_t len)
  101. {
  102. unsigned int cs = 0;
  103. const int cslen = 128;
  104. len = (len < cslen) ? len : cslen;
  105. for(int i = 0; i < len; i++)
  106. {
  107. cs = cs ^ data[i];
  108. }
  109. return cs;
  110. }
  111. // memory in iPhone is precious
  112. // Should buffer factor be 1.5 instead of 2 ?
  113. #define BUFFER_INC_FACTOR (2)
  114. int ZipUtils::inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t *outLength, ssize_t outLengthHint)
  115. {
  116. /* ret value */
  117. int err = Z_OK;
  118. ssize_t bufferSize = outLengthHint;
  119. *out = (unsigned char*)malloc(bufferSize);
  120. z_stream d_stream; /* decompression stream */
  121. d_stream.zalloc = (alloc_func)0;
  122. d_stream.zfree = (free_func)0;
  123. d_stream.opaque = (voidpf)0;
  124. d_stream.next_in = in;
  125. d_stream.avail_in = static_cast<unsigned int>(inLength);
  126. d_stream.next_out = *out;
  127. d_stream.avail_out = static_cast<unsigned int>(bufferSize);
  128. /* window size to hold 256k */
  129. if( (err = inflateInit2(&d_stream, 15 + 32)) != Z_OK )
  130. return err;
  131. for (;;)
  132. {
  133. err = inflate(&d_stream, Z_NO_FLUSH);
  134. if (err == Z_STREAM_END)
  135. {
  136. break;
  137. }
  138. switch (err)
  139. {
  140. case Z_NEED_DICT:
  141. err = Z_DATA_ERROR;
  142. case Z_DATA_ERROR:
  143. case Z_MEM_ERROR:
  144. inflateEnd(&d_stream);
  145. return err;
  146. }
  147. // not enough memory ?
  148. if (err != Z_STREAM_END)
  149. {
  150. *out = (unsigned char*)realloc(*out, bufferSize * BUFFER_INC_FACTOR);
  151. /* not enough memory, ouch */
  152. if (! *out )
  153. {
  154. CCLOG("cocos2d: ZipUtils: realloc failed");
  155. inflateEnd(&d_stream);
  156. return Z_MEM_ERROR;
  157. }
  158. d_stream.next_out = *out + bufferSize;
  159. d_stream.avail_out = static_cast<unsigned int>(bufferSize);
  160. bufferSize *= BUFFER_INC_FACTOR;
  161. }
  162. }
  163. *outLength = bufferSize - d_stream.avail_out;
  164. err = inflateEnd(&d_stream);
  165. return err;
  166. }
  167. ssize_t ZipUtils::inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t outLengthHint)
  168. {
  169. ssize_t outLength = 0;
  170. int err = inflateMemoryWithHint(in, inLength, out, &outLength, outLengthHint);
  171. if (err != Z_OK || *out == nullptr) {
  172. if (err == Z_MEM_ERROR)
  173. {
  174. CCLOG("cocos2d: ZipUtils: Out of memory while decompressing map data!");
  175. } else
  176. if (err == Z_VERSION_ERROR)
  177. {
  178. CCLOG("cocos2d: ZipUtils: Incompatible zlib version!");
  179. } else
  180. if (err == Z_DATA_ERROR)
  181. {
  182. CCLOG("cocos2d: ZipUtils: Incorrect zlib compressed data!");
  183. }
  184. else
  185. {
  186. CCLOG("cocos2d: ZipUtils: Unknown error while decompressing map data!");
  187. }
  188. if(*out) {
  189. free(*out);
  190. *out = nullptr;
  191. }
  192. outLength = 0;
  193. }
  194. return outLength;
  195. }
  196. ssize_t ZipUtils::inflateMemory(unsigned char *in, ssize_t inLength, unsigned char **out)
  197. {
  198. // 256k for hint
  199. return inflateMemoryWithHint(in, inLength, out, 256 * 1024);
  200. }
  201. int ZipUtils::inflateGZipFile(const char *path, unsigned char **out)
  202. {
  203. int len;
  204. unsigned int offset = 0;
  205. CCASSERT(out, "out can't be nullptr.");
  206. CCASSERT(&*out, "&*out can't be nullptr.");
  207. gzFile inFile = gzopen(FileUtils::getInstance()->getSuitableFOpen(path).c_str(), "rb");
  208. if( inFile == nullptr ) {
  209. CCLOG("cocos2d: ZipUtils: error open gzip file: %s", path);
  210. return -1;
  211. }
  212. /* 512k initial decompress buffer */
  213. unsigned int bufferSize = 512 * 1024;
  214. unsigned int totalBufferSize = bufferSize;
  215. *out = (unsigned char*)malloc( bufferSize );
  216. if( ! out )
  217. {
  218. CCLOG("cocos2d: ZipUtils: out of memory");
  219. return -1;
  220. }
  221. for (;;) {
  222. len = gzread(inFile, *out + offset, bufferSize);
  223. if (len < 0)
  224. {
  225. CCLOG("cocos2d: ZipUtils: error in gzread");
  226. free( *out );
  227. *out = nullptr;
  228. return -1;
  229. }
  230. if (len == 0)
  231. {
  232. break;
  233. }
  234. offset += len;
  235. // finish reading the file
  236. if( (unsigned int)len < bufferSize )
  237. {
  238. break;
  239. }
  240. bufferSize *= BUFFER_INC_FACTOR;
  241. totalBufferSize += bufferSize;
  242. unsigned char *tmp = (unsigned char*)realloc(*out, totalBufferSize );
  243. if( ! tmp )
  244. {
  245. CCLOG("cocos2d: ZipUtils: out of memory");
  246. free( *out );
  247. *out = nullptr;
  248. return -1;
  249. }
  250. *out = tmp;
  251. }
  252. if (gzclose(inFile) != Z_OK)
  253. {
  254. CCLOG("cocos2d: ZipUtils: gzclose failed");
  255. }
  256. return offset;
  257. }
  258. bool ZipUtils::isCCZFile(const char *path)
  259. {
  260. // load file into memory
  261. Data compressedData = FileUtils::getInstance()->getDataFromFile(path);
  262. if (compressedData.isNull())
  263. {
  264. CCLOG("cocos2d: ZipUtils: loading file failed");
  265. return false;
  266. }
  267. return isCCZBuffer(compressedData.getBytes(), compressedData.getSize());
  268. }
  269. bool ZipUtils::isCCZBuffer(const unsigned char *buffer, ssize_t len)
  270. {
  271. if (static_cast<size_t>(len) < sizeof(struct CCZHeader))
  272. {
  273. return false;
  274. }
  275. struct CCZHeader *header = (struct CCZHeader*) buffer;
  276. return header->sig[0] == 'C' && header->sig[1] == 'C' && header->sig[2] == 'Z' && (header->sig[3] == '!' || header->sig[3] == 'p');
  277. }
  278. bool ZipUtils::isGZipFile(const char *path)
  279. {
  280. // load file into memory
  281. Data compressedData = FileUtils::getInstance()->getDataFromFile(path);
  282. if (compressedData.isNull())
  283. {
  284. CCLOG("cocos2d: ZipUtils: loading file failed");
  285. return false;
  286. }
  287. return isGZipBuffer(compressedData.getBytes(), compressedData.getSize());
  288. }
  289. bool ZipUtils::isGZipBuffer(const unsigned char *buffer, ssize_t len)
  290. {
  291. if (len < 2)
  292. {
  293. return false;
  294. }
  295. return buffer[0] == 0x1F && buffer[1] == 0x8B;
  296. }
  297. int ZipUtils::inflateCCZBuffer(const unsigned char *buffer, ssize_t bufferLen, unsigned char **out)
  298. {
  299. struct CCZHeader *header = (struct CCZHeader*) buffer;
  300. // verify header
  301. if( header->sig[0] == 'C' && header->sig[1] == 'C' && header->sig[2] == 'Z' && header->sig[3] == '!' )
  302. {
  303. // verify header version
  304. unsigned int version = CC_SWAP_INT16_BIG_TO_HOST( header->version );
  305. if( version > 2 )
  306. {
  307. CCLOG("cocos2d: Unsupported CCZ header format");
  308. return -1;
  309. }
  310. // verify compression format
  311. if( CC_SWAP_INT16_BIG_TO_HOST(header->compression_type) != CCZ_COMPRESSION_ZLIB )
  312. {
  313. CCLOG("cocos2d: CCZ Unsupported compression method");
  314. return -1;
  315. }
  316. }
  317. else if( header->sig[0] == 'C' && header->sig[1] == 'C' && header->sig[2] == 'Z' && header->sig[3] == 'p' )
  318. {
  319. // encrypted ccz file
  320. header = (struct CCZHeader*) buffer;
  321. // verify header version
  322. unsigned int version = CC_SWAP_INT16_BIG_TO_HOST( header->version );
  323. if( version > 0 )
  324. {
  325. CCLOG("cocos2d: Unsupported CCZ header format");
  326. return -1;
  327. }
  328. // verify compression format
  329. if( CC_SWAP_INT16_BIG_TO_HOST(header->compression_type) != CCZ_COMPRESSION_ZLIB )
  330. {
  331. CCLOG("cocos2d: CCZ Unsupported compression method");
  332. return -1;
  333. }
  334. // decrypt
  335. unsigned int* ints = (unsigned int*)(buffer+12);
  336. ssize_t enclen = (bufferLen-12)/4;
  337. decodeEncodedPvr(ints, enclen);
  338. #if COCOS2D_DEBUG > 0
  339. // verify checksum in debug mode
  340. unsigned int calculated = checksumPvr(ints, enclen);
  341. unsigned int required = CC_SWAP_INT32_BIG_TO_HOST( header->reserved );
  342. if(calculated != required)
  343. {
  344. CCLOG("cocos2d: Can't decrypt image file. Is the decryption key valid?");
  345. return -1;
  346. }
  347. #endif
  348. }
  349. else
  350. {
  351. CCLOG("cocos2d: Invalid CCZ file");
  352. return -1;
  353. }
  354. unsigned int len = CC_SWAP_INT32_BIG_TO_HOST( header->len );
  355. *out = (unsigned char*)malloc( len );
  356. if(! *out )
  357. {
  358. CCLOG("cocos2d: CCZ: Failed to allocate memory for texture");
  359. return -1;
  360. }
  361. unsigned long destlen = len;
  362. size_t source = (size_t) buffer + sizeof(*header);
  363. int ret = uncompress(*out, &destlen, (Bytef*)source, bufferLen - sizeof(*header) );
  364. if( ret != Z_OK )
  365. {
  366. CCLOG("cocos2d: CCZ: Failed to uncompress data");
  367. free( *out );
  368. *out = nullptr;
  369. return -1;
  370. }
  371. return len;
  372. }
  373. int ZipUtils::inflateCCZFile(const char *path, unsigned char **out)
  374. {
  375. CCASSERT(out, "Invalid pointer for buffer!");
  376. // load file into memory
  377. Data compressedData = FileUtils::getInstance()->getDataFromFile(path);
  378. if (compressedData.isNull())
  379. {
  380. CCLOG("cocos2d: Error loading CCZ compressed file");
  381. return -1;
  382. }
  383. return inflateCCZBuffer(compressedData.getBytes(), compressedData.getSize(), out);
  384. }
  385. void ZipUtils::setPvrEncryptionKeyPart(int index, unsigned int value)
  386. {
  387. CCASSERT(index >= 0, "Cocos2d: key part index cannot be less than 0");
  388. CCASSERT(index <= 3, "Cocos2d: key part index cannot be greater than 3");
  389. if(s_uEncryptedPvrKeyParts[index] != value)
  390. {
  391. s_uEncryptedPvrKeyParts[index] = value;
  392. s_bEncryptionKeyIsValid = false;
  393. }
  394. }
  395. void ZipUtils::setPvrEncryptionKey(unsigned int keyPart1, unsigned int keyPart2, unsigned int keyPart3, unsigned int keyPart4)
  396. {
  397. setPvrEncryptionKeyPart(0, keyPart1);
  398. setPvrEncryptionKeyPart(1, keyPart2);
  399. setPvrEncryptionKeyPart(2, keyPart3);
  400. setPvrEncryptionKeyPart(3, keyPart4);
  401. }
  402. // --------------------- ZipFile ---------------------
  403. // from unzip.cpp
  404. #define UNZ_MAXFILENAMEINZIP 256
  405. static const std::string emptyFilename("");
  406. struct ZipEntryInfo
  407. {
  408. unz_file_pos pos;
  409. uLong uncompressed_size;
  410. };
  411. class ZipFilePrivate
  412. {
  413. public:
  414. unzFile zipFile;
  415. // std::unordered_map is faster if available on the platform
  416. typedef std::unordered_map<std::string, struct ZipEntryInfo> FileListContainer;
  417. FileListContainer fileList;
  418. };
  419. ZipFile *ZipFile::createWithBuffer(const void* buffer, uLong size)
  420. {
  421. ZipFile *zip = new (std::nothrow) ZipFile();
  422. if (zip && zip->initWithBuffer(buffer, size)) {
  423. return zip;
  424. } else {
  425. if (zip) delete zip;
  426. return nullptr;
  427. }
  428. }
  429. ZipFile::ZipFile()
  430. : _data(new ZipFilePrivate)
  431. {
  432. _data->zipFile = nullptr;
  433. }
  434. ZipFile::ZipFile(const std::string &zipFile, const std::string &filter)
  435. : _data(new ZipFilePrivate)
  436. {
  437. _data->zipFile = unzOpen(FileUtils::getInstance()->getSuitableFOpen(zipFile).c_str());
  438. setFilter(filter);
  439. }
  440. ZipFile::~ZipFile()
  441. {
  442. if (_data && _data->zipFile)
  443. {
  444. unzClose(_data->zipFile);
  445. }
  446. CC_SAFE_DELETE(_data);
  447. }
  448. bool ZipFile::setFilter(const std::string &filter)
  449. {
  450. bool ret = false;
  451. do
  452. {
  453. CC_BREAK_IF(!_data);
  454. CC_BREAK_IF(!_data->zipFile);
  455. // clear existing file list
  456. _data->fileList.clear();
  457. // UNZ_MAXFILENAMEINZIP + 1 - it is done so in unzLocateFile
  458. char szCurrentFileName[UNZ_MAXFILENAMEINZIP + 1];
  459. unz_file_info64 fileInfo;
  460. // go through all files and store position information about the required files
  461. int err = unzGoToFirstFile64(_data->zipFile, &fileInfo,
  462. szCurrentFileName, sizeof(szCurrentFileName) - 1);
  463. while (err == UNZ_OK)
  464. {
  465. unz_file_pos posInfo;
  466. int posErr = unzGetFilePos(_data->zipFile, &posInfo);
  467. if (posErr == UNZ_OK)
  468. {
  469. std::string currentFileName = szCurrentFileName;
  470. // cache info about filtered files only (like 'assets/')
  471. if (filter.empty()
  472. || currentFileName.substr(0, filter.length()) == filter)
  473. {
  474. ZipEntryInfo entry;
  475. entry.pos = posInfo;
  476. entry.uncompressed_size = (uLong)fileInfo.uncompressed_size;
  477. _data->fileList[currentFileName] = entry;
  478. }
  479. }
  480. // next file - also get the information about it
  481. err = unzGoToNextFile64(_data->zipFile, &fileInfo,
  482. szCurrentFileName, sizeof(szCurrentFileName) - 1);
  483. }
  484. ret = true;
  485. } while(false);
  486. return ret;
  487. }
  488. bool ZipFile::fileExists(const std::string &fileName) const
  489. {
  490. bool ret = false;
  491. do
  492. {
  493. CC_BREAK_IF(!_data);
  494. ret = _data->fileList.find(fileName) != _data->fileList.end();
  495. } while(false);
  496. return ret;
  497. }
  498. unsigned char *ZipFile::getFileData(const std::string &fileName, ssize_t *size)
  499. {
  500. unsigned char * buffer = nullptr;
  501. if (size)
  502. *size = 0;
  503. do
  504. {
  505. CC_BREAK_IF(!_data->zipFile);
  506. CC_BREAK_IF(fileName.empty());
  507. ZipFilePrivate::FileListContainer::const_iterator it = _data->fileList.find(fileName);
  508. CC_BREAK_IF(it == _data->fileList.end());
  509. ZipEntryInfo fileInfo = it->second;
  510. int nRet = unzGoToFilePos(_data->zipFile, &fileInfo.pos);
  511. CC_BREAK_IF(UNZ_OK != nRet);
  512. nRet = unzOpenCurrentFile(_data->zipFile);
  513. CC_BREAK_IF(UNZ_OK != nRet);
  514. buffer = (unsigned char*)malloc(fileInfo.uncompressed_size);
  515. int CC_UNUSED nSize = unzReadCurrentFile(_data->zipFile, buffer, static_cast<unsigned int>(fileInfo.uncompressed_size));
  516. CCASSERT(nSize == 0 || nSize == (int)fileInfo.uncompressed_size, "the file size is wrong");
  517. if (size)
  518. {
  519. *size = fileInfo.uncompressed_size;
  520. }
  521. unzCloseCurrentFile(_data->zipFile);
  522. } while (0);
  523. return buffer;
  524. }
  525. bool ZipFile::getFileData(const std::string &fileName, ResizableBuffer* buffer)
  526. {
  527. bool res = false;
  528. do
  529. {
  530. CC_BREAK_IF(!_data->zipFile);
  531. CC_BREAK_IF(fileName.empty());
  532. ZipFilePrivate::FileListContainer::const_iterator it = _data->fileList.find(fileName);
  533. CC_BREAK_IF(it == _data->fileList.end());
  534. ZipEntryInfo fileInfo = it->second;
  535. int nRet = unzGoToFilePos(_data->zipFile, &fileInfo.pos);
  536. CC_BREAK_IF(UNZ_OK != nRet);
  537. nRet = unzOpenCurrentFile(_data->zipFile);
  538. CC_BREAK_IF(UNZ_OK != nRet);
  539. buffer->resize(fileInfo.uncompressed_size);
  540. int CC_UNUSED nSize = unzReadCurrentFile(_data->zipFile, buffer->buffer(), static_cast<unsigned int>(fileInfo.uncompressed_size));
  541. CCASSERT(nSize == 0 || nSize == (int)fileInfo.uncompressed_size, "the file size is wrong");
  542. unzCloseCurrentFile(_data->zipFile);
  543. res = true;
  544. } while (0);
  545. return res;
  546. }
  547. std::string ZipFile::getFirstFilename()
  548. {
  549. if (unzGoToFirstFile(_data->zipFile) != UNZ_OK) return emptyFilename;
  550. std::string path;
  551. unz_file_info info;
  552. getCurrentFileInfo(&path, &info);
  553. return path;
  554. }
  555. std::string ZipFile::getNextFilename()
  556. {
  557. if (unzGoToNextFile(_data->zipFile) != UNZ_OK) return emptyFilename;
  558. std::string path;
  559. unz_file_info info;
  560. getCurrentFileInfo(&path, &info);
  561. return path;
  562. }
  563. int ZipFile::getCurrentFileInfo(std::string *filename, unz_file_info *info)
  564. {
  565. char path[FILENAME_MAX + 1];
  566. int ret = unzGetCurrentFileInfo(_data->zipFile, info, path, sizeof(path), nullptr, 0, nullptr, 0);
  567. if (ret != UNZ_OK) {
  568. *filename = emptyFilename;
  569. } else {
  570. filename->assign(path);
  571. }
  572. return ret;
  573. }
  574. bool ZipFile::initWithBuffer(const void *buffer, uLong size)
  575. {
  576. if (!buffer || size == 0) return false;
  577. _data->zipFile = unzOpenBuffer(buffer, size);
  578. if (!_data->zipFile) return false;
  579. setFilter(emptyFilename);
  580. return true;
  581. }
  582. NS_CC_END