PuzzleSDK
ZipTests.cpp 文件参考
#include "ZipTests.h"
#include <sstream>
#include "unzip/unzip.h"
#include "unzip/crypt.h"
+ ZipTests.cpp 的引用(Include)关系图:

浏览源代码.

函数

static void unzipTest (Label *label, const std::string &originFile, const std::string &tmpName, const std::string &zipFile, const std::string &password="")
 

函数说明

◆ unzipTest()

static void unzipTest ( Label *  label,
const std::string &  originFile,
const std::string &  tmpName,
const std::string &  zipFile,
const std::string &  password = "" 
)
static

在文件 ZipTests.cpp44 行定义.

45{
46
47 auto fu = FileUtils::getInstance();
48 cocos2d::Data origContent;
49 const int BUFF_SIZE = 1024;
50 char *buff = nullptr;
51 std::vector<char> fileData;
52 bool hasError = false;
53 unz_file_info fileInfo = {0};
54 char fileName[40] = {0};
55
56 auto newLocal = fu->getWritablePath() + tmpName;
57 //copy file to support android
58
59 if(fu->isFileExist(newLocal)) {
60 CCLOG("Remove file %s", newLocal.c_str());
61 fu->removeFile(newLocal);
62 }
63
64 CCLOG("Copy %s to %s", zipFile.c_str(), newLocal.c_str());
65 auto writeSuccess = fu->writeDataToFile(fu->getDataFromFile(zipFile), newLocal);
66 if(!writeSuccess) {
67 label->setString("Failed to copy zip file to writable path");
68 return;
69 }
70
71 unzFile fp = unzOpen(newLocal.c_str());
72 if(!fp) {
73 CCLOG("Failed to open zip file %s", newLocal.c_str());
74 label->setString("Failed to open zip file");
75 return;
76 }
77
78 int err = unzGoToFirstFile(fp);
79 if(err != UNZ_OK) {
80 label->setString("Failed to local first file");
81 goto close_and_return;
82 }
83
84 unzGetCurrentFileInfo(fp, &fileInfo, fileName, sizeof(fileName) -1, nullptr, 0, nullptr, 0 );
85
86 CCASSERT(strncmp("10k.txt", fileName, 7) == 0, "file name should be 10k.txt");
87
88 if(password.empty())
89 {
90 err = unzOpenCurrentFile(fp);
91 }
92 else
93 {
94 err = unzOpenCurrentFilePassword(fp, password.c_str());
95 }
96
97 if(err != UNZ_OK) {
98 label->setString("failed to open zip file");
99 goto close_and_return;
100 }
101
102 buff = new char[BUFF_SIZE];
103
104 for(;;) {
105 int retSize = unzReadCurrentFile(fp, buff, BUFF_SIZE);
106 if(retSize < 0) {
107 hasError = true;
108 break;
109 }
110 else if(retSize == 0) {
111 break;
112 }
113
114 fileData.insert(fileData.end(), buff, buff + retSize);
115 }
116
117 delete[] buff;
118
119 if(hasError) {
120 label->setString("unzip error! read error!");
121 goto close_and_return;
122 }
123
124 origContent = FileUtils::getInstance()->getDataFromFile(originFile);
125
126 if(origContent.getSize() == fileData.size() &&
127 memcmp(origContent.getBytes(), fileData.data(), fileData.size()) == 0) {
128 label->setString("unzip ok!");
129 } else {
130 label->setString("unzip error! data mismatch!");
131 }
132close_and_return:
133 unzClose(fp);
134}

被这些函数引用 UnZipNormalFile::onEnter() , 以及 UnZipWithPassword::onEnter().

+ 这是这个函数的调用关系图: