PuzzleSDK
ZipTests.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos.com
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "ZipTests.h"
26
27#include <sstream>
28
29#include "unzip/unzip.h"
30#include "unzip/crypt.h"
31
32using namespace cocos2d;
33
34ZipTests::ZipTests() {
37}
38
39std::string ZipTest::title() const {
40 return "Unzip Test";
41}
42
43
44static void unzipTest(Label *label, const std::string &originFile, const std::string &tmpName, const std::string &zipFile, const std::string& password = "")
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}
135
136
139
140 const auto winSize = Director::getInstance()->getWinSize();
141
142 Label *label = Label::createWithTTF("unziping file", "fonts/Marker Felt.ttf", 23);
143 label->setPosition(winSize.width/2, winSize.height/2);
144 addChild(label);
145
146 unzipTest(label, "zip/10k.txt", "10-nopasswd.zip", "zip/10k-nopass.zip");
147}
148
149std::string UnZipNormalFile::subtitle() const {
150 return "unzip without password";
151}
152
155
156 const auto winSize = Director::getInstance()->getWinSize();
157
158 Label *label = Label::createWithTTF("unziping file", "fonts/Marker Felt.ttf", 23);
159 label->setPosition(winSize.width/2, winSize.height/2);
160 addChild(label);
161
162 unzipTest(label, "zip/10k.txt", "10.zip", "zip/10k.zip", "123456");
163}
164
165std::string UnZipWithPassword::subtitle() const {
166 return "unzip with password";
167}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
static void unzipTest(Label *label, const std::string &originFile, const std::string &tmpName, const std::string &zipFile, const std::string &password="")
Definition: ZipTests.cpp:44
virtual void onEnter() override
Definition: BaseTest.cpp:430
virtual void onEnter() override
Definition: ZipTests.cpp:137
virtual std::string subtitle() const override
Definition: ZipTests.cpp:149
virtual void onEnter() override
Definition: ZipTests.cpp:153
virtual std::string subtitle() const override
Definition: ZipTests.cpp:165
virtual std::string title() const override
Definition: ZipTests.cpp:39