29#include "unzip/unzip.h"
30#include "unzip/crypt.h"
44static void unzipTest(Label *label,
const std::string &originFile,
const std::string &tmpName,
const std::string &zipFile,
const std::string& password =
"")
47 auto fu = FileUtils::getInstance();
48 cocos2d::Data origContent;
49 const int BUFF_SIZE = 1024;
51 std::vector<char> fileData;
52 bool hasError =
false;
53 unz_file_info fileInfo = {0};
54 char fileName[40] = {0};
56 auto newLocal = fu->getWritablePath() + tmpName;
59 if(fu->isFileExist(newLocal)) {
60 CCLOG(
"Remove file %s", newLocal.c_str());
61 fu->removeFile(newLocal);
64 CCLOG(
"Copy %s to %s", zipFile.c_str(), newLocal.c_str());
65 auto writeSuccess = fu->writeDataToFile(fu->getDataFromFile(zipFile), newLocal);
67 label->setString(
"Failed to copy zip file to writable path");
71 unzFile fp = unzOpen(newLocal.c_str());
73 CCLOG(
"Failed to open zip file %s", newLocal.c_str());
74 label->setString(
"Failed to open zip file");
78 int err = unzGoToFirstFile(fp);
80 label->setString(
"Failed to local first file");
81 goto close_and_return;
84 unzGetCurrentFileInfo(fp, &fileInfo, fileName,
sizeof(fileName) -1,
nullptr, 0,
nullptr, 0 );
86 CCASSERT(strncmp(
"10k.txt", fileName, 7) == 0,
"file name should be 10k.txt");
90 err = unzOpenCurrentFile(fp);
94 err = unzOpenCurrentFilePassword(fp, password.c_str());
98 label->setString(
"failed to open zip file");
99 goto close_and_return;
102 buff =
new char[BUFF_SIZE];
105 int retSize = unzReadCurrentFile(fp, buff, BUFF_SIZE);
110 else if(retSize == 0) {
114 fileData.insert(fileData.end(), buff, buff + retSize);
120 label->setString(
"unzip error! read error!");
121 goto close_and_return;
124 origContent = FileUtils::getInstance()->getDataFromFile(originFile);
126 if(origContent.getSize() == fileData.size() &&
127 memcmp(origContent.getBytes(), fileData.data(), fileData.size()) == 0) {
128 label->setString(
"unzip ok!");
130 label->setString(
"unzip error! data mismatch!");
140 const auto winSize = Director::getInstance()->getWinSize();
142 Label *label = Label::createWithTTF(
"unziping file",
"fonts/Marker Felt.ttf", 23);
143 label->setPosition(winSize.width/2, winSize.height/2);
146 unzipTest(label,
"zip/10k.txt",
"10-nopasswd.zip",
"zip/10k-nopass.zip");
150 return "unzip without password";
156 const auto winSize = Director::getInstance()->getWinSize();
158 Label *label = Label::createWithTTF(
"unziping file",
"fonts/Marker Felt.ttf", 23);
159 label->setPosition(winSize.width/2, winSize.height/2);
162 unzipTest(label,
"zip/10k.txt",
"10.zip",
"zip/10k.zip",
"123456");
166 return "unzip with password";
#define ADD_TEST_CASE(__className__)
static void unzipTest(Label *label, const std::string &originFile, const std::string &tmpName, const std::string &zipFile, const std::string &password="")
virtual void onEnter() override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string title() const override