2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2019 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
http://www.cocos.com
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ZipTests.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "unzip/unzip.h"
|
|
|
|
#include "unzip/crypt.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ZipTests::ZipTests()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ADD_TEST_CASE(UnZipNormalFile);
|
|
|
|
ADD_TEST_CASE(UnZipWithPassword);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string ZipTest::title() const
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return "Unzip Test";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static void unzipTest(Label* label,
|
|
|
|
std::string_view originFile,
|
|
|
|
std::string_view tmpName,
|
|
|
|
std::string_view zipFile,
|
|
|
|
std::string_view password = "")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
auto fu = FileUtils::getInstance();
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Data origContent;
|
2019-11-23 20:27:39 +08:00
|
|
|
const int BUFF_SIZE = 1024;
|
2021-12-31 12:12:40 +08:00
|
|
|
char* buff = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<char> fileData;
|
2021-12-31 12:12:40 +08:00
|
|
|
bool hasError = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
unz_file_info fileInfo = {0};
|
2021-12-31 12:12:40 +08:00
|
|
|
char fileName[40] = {0};
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string newLocal{fu->getWritablePath()};
|
|
|
|
newLocal += tmpName;
|
|
|
|
// copy file to support android
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (fu->isFileExist(newLocal))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
CCLOG("Remove file %s", newLocal.c_str());
|
|
|
|
fu->removeFile(newLocal);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
CCLOG("Copy %s to %s", zipFile.data(), newLocal.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
auto writeSuccess = fu->writeDataToFile(fu->getDataFromFile(zipFile), newLocal);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!writeSuccess)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setString("Failed to copy zip file to writable path");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unzFile fp = unzOpen(newLocal.c_str());
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!fp)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
CCLOG("Failed to open zip file %s", newLocal.c_str());
|
|
|
|
label->setString("Failed to open zip file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int err = unzGoToFirstFile(fp);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (err != UNZ_OK)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setString("Failed to local first file");
|
|
|
|
goto close_and_return;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
unzGetCurrentFileInfo(fp, &fileInfo, fileName, sizeof(fileName) - 1, nullptr, 0, nullptr, 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
CCASSERT(strncmp("10k.txt", fileName, 7) == 0, "file name should be 10k.txt");
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (password.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
err = unzOpenCurrentFile(fp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
err = unzOpenCurrentFilePassword(fp, password.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (err != UNZ_OK)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setString("failed to open zip file");
|
|
|
|
goto close_and_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buff = new char[BUFF_SIZE];
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int retSize = unzReadCurrentFile(fp, buff, BUFF_SIZE);
|
|
|
|
if (retSize < 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
hasError = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else if (retSize == 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileData.insert(fileData.end(), buff, buff + retSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] buff;
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (hasError)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setString("unzip error! read error!");
|
|
|
|
goto close_and_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
origContent = FileUtils::getInstance()->getDataFromFile(originFile);
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (origContent.getSize() == fileData.size() &&
|
|
|
|
memcmp(origContent.getBytes(), fileData.data(), fileData.size()) == 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setString("unzip ok!");
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setString("unzip error! data mismatch!");
|
|
|
|
}
|
|
|
|
close_and_return:
|
|
|
|
unzClose(fp);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UnZipNormalFile::onEnter()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
TestCase::onEnter();
|
|
|
|
|
|
|
|
const auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Label* label = Label::createWithTTF("unziping file", "fonts/Marker Felt.ttf", 23);
|
|
|
|
label->setPosition(winSize.width / 2, winSize.height / 2);
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(label);
|
|
|
|
|
|
|
|
unzipTest(label, "zip/10k.txt", "10-nopasswd.zip", "zip/10k-nopass.zip");
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string UnZipNormalFile::subtitle() const
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return "unzip without password";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UnZipWithPassword::onEnter()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
TestCase::onEnter();
|
|
|
|
|
|
|
|
const auto winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Label* label = Label::createWithTTF("unziping file", "fonts/Marker Felt.ttf", 23);
|
|
|
|
label->setPosition(winSize.width / 2, winSize.height / 2);
|
2019-11-23 20:27:39 +08:00
|
|
|
addChild(label);
|
|
|
|
|
|
|
|
unzipTest(label, "zip/10k.txt", "10.zip", "zip/10k.zip", "123456");
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string UnZipWithPassword::subtitle() const
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return "unzip with password";
|
|
|
|
}
|