2013-07-30 18:02:55 +03:00
|
|
|
/*
|
|
|
|
* CZipLoader.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CZipLoader.h"
|
|
|
|
#include "FileStream.h"
|
|
|
|
|
|
|
|
#include "../ScopeGuard.h"
|
2013-07-30 18:02:55 +03:00
|
|
|
|
2016-02-04 11:28:12 +02:00
|
|
|
CZipStream::CZipStream(std::shared_ptr<CIOApi> api, const boost::filesystem::path & archive, unz64_file_pos filepos)
|
2013-07-30 18:02:55 +03:00
|
|
|
{
|
2015-08-11 15:56:07 +02:00
|
|
|
zlib_filefunc64_def zlibApi;
|
2016-02-04 11:28:12 +02:00
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
zlibApi = api->getApiStructure();
|
2016-02-04 11:28:12 +02:00
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
file = unzOpen2_64(archive.c_str(), &zlibApi);
|
2016-01-16 17:36:16 +02:00
|
|
|
unzGoToFilePos64(file, &filepos);
|
2013-07-30 18:02:55 +03:00
|
|
|
unzOpenCurrentFile(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
CZipStream::~CZipStream()
|
|
|
|
{
|
|
|
|
unzCloseCurrentFile(file);
|
|
|
|
unzClose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
si64 CZipStream::readMore(ui8 * data, si64 size)
|
|
|
|
{
|
|
|
|
return unzReadCurrentFile(file, data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
si64 CZipStream::getSize()
|
|
|
|
{
|
2016-01-16 17:36:16 +02:00
|
|
|
unz_file_info64 info;
|
|
|
|
unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
|
2013-07-30 18:02:55 +03:00
|
|
|
return info.uncompressed_size;
|
|
|
|
}
|
|
|
|
|
2013-11-09 16:49:36 +03:00
|
|
|
ui32 CZipStream::calculateCRC32()
|
|
|
|
{
|
2016-01-16 17:36:16 +02:00
|
|
|
unz_file_info64 info;
|
|
|
|
unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
|
2013-11-09 16:49:36 +03:00
|
|
|
return info.crc;
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
///CZipLoader
|
2016-02-04 09:27:51 +02:00
|
|
|
CZipLoader::CZipLoader(const std::string & mountPoint, const boost::filesystem::path & archive, std::shared_ptr<CIOApi> api):
|
2015-08-11 15:56:07 +02:00
|
|
|
ioApi(api),
|
2016-02-04 11:28:12 +02:00
|
|
|
zlibApi(ioApi->getApiStructure()),
|
2013-07-30 18:02:55 +03:00
|
|
|
archiveName(archive),
|
|
|
|
mountPoint(mountPoint),
|
|
|
|
files(listFiles(mountPoint, archive))
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->trace("Zip archive loaded, %d files found", files.size());
|
2013-07-30 18:02:55 +03:00
|
|
|
}
|
|
|
|
|
2016-01-16 17:36:16 +02:00
|
|
|
std::unordered_map<ResourceID, unz64_file_pos> CZipLoader::listFiles(const std::string & mountPoint, const boost::filesystem::path & archive)
|
2013-07-30 18:02:55 +03:00
|
|
|
{
|
2016-01-16 17:36:16 +02:00
|
|
|
std::unordered_map<ResourceID, unz64_file_pos> ret;
|
2013-07-30 18:02:55 +03:00
|
|
|
|
2015-08-11 15:56:07 +02:00
|
|
|
unzFile file = unzOpen2_64(archive.c_str(), &zlibApi);
|
2016-02-04 11:28:12 +02:00
|
|
|
|
2015-08-13 02:29:13 +02:00
|
|
|
if(file == nullptr)
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("%s failed to open", archive.string());
|
2013-07-30 18:02:55 +03:00
|
|
|
|
|
|
|
if (unzGoToFirstFile(file) == UNZ_OK)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2016-01-16 17:36:16 +02:00
|
|
|
unz_file_info64 info;
|
2013-07-30 18:02:55 +03:00
|
|
|
std::vector<char> filename;
|
|
|
|
// Fill unz_file_info structure with current file info
|
2016-01-16 17:36:16 +02:00
|
|
|
unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
|
2013-07-30 18:02:55 +03:00
|
|
|
|
|
|
|
filename.resize(info.size_filename);
|
|
|
|
// Get name of current file. Contrary to docs "info" parameter can't be null
|
2016-01-16 17:36:16 +02:00
|
|
|
unzGetCurrentFileInfo64 (file, &info, filename.data(), filename.size(), nullptr, 0, nullptr, 0);
|
2013-07-30 18:02:55 +03:00
|
|
|
|
|
|
|
std::string filenameString(filename.data(), filename.size());
|
2016-01-16 17:36:16 +02:00
|
|
|
unzGetFilePos64(file, &ret[ResourceID(mountPoint + filenameString)]);
|
2013-07-30 18:02:55 +03:00
|
|
|
}
|
|
|
|
while (unzGoToNextFile(file) == UNZ_OK);
|
|
|
|
}
|
|
|
|
unzClose(file);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CInputStream> CZipLoader::load(const ResourceID & resourceName) const
|
|
|
|
{
|
2016-02-04 11:28:12 +02:00
|
|
|
return std::unique_ptr<CInputStream>(new CZipStream(ioApi, archiveName, files.at(resourceName)));
|
2013-07-30 18:02:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CZipLoader::existsResource(const ResourceID & resourceName) const
|
|
|
|
{
|
|
|
|
return files.count(resourceName) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CZipLoader::getMountPoint() const
|
|
|
|
{
|
|
|
|
return mountPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_set<ResourceID> CZipLoader::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
|
|
|
|
{
|
|
|
|
std::unordered_set<ResourceID> foundID;
|
|
|
|
|
|
|
|
for (auto & file : files)
|
|
|
|
{
|
|
|
|
if (filter(file.first))
|
|
|
|
foundID.insert(file.first);
|
|
|
|
}
|
|
|
|
return foundID;
|
2013-08-19 14:50:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// extracts currently selected file from zip into stream "where"
|
|
|
|
static bool extractCurrent(unzFile file, std::ostream & where)
|
|
|
|
{
|
|
|
|
std::array<char, 8 * 1024> buffer;
|
|
|
|
|
|
|
|
unzOpenCurrentFile(file);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int readSize = unzReadCurrentFile(file, buffer.data(), buffer.size());
|
|
|
|
|
|
|
|
if (readSize < 0) // error
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (readSize == 0) // end-of-file. Also performs CRC check
|
|
|
|
return unzCloseCurrentFile(file) == UNZ_OK;
|
|
|
|
|
2014-03-23 15:59:03 +03:00
|
|
|
if (readSize > 0) // successful read
|
2013-08-19 14:50:53 +03:00
|
|
|
{
|
|
|
|
where.write(buffer.data(), readSize);
|
|
|
|
if (!where.good())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// extraction failed. Close file and exit
|
|
|
|
unzCloseCurrentFile(file);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:23:55 +02:00
|
|
|
std::vector<std::string> ZipArchive::listFiles(boost::filesystem::path filename)
|
2013-08-19 14:50:53 +03:00
|
|
|
{
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
|
2016-01-09 22:23:55 +02:00
|
|
|
unzFile file = unzOpen2_64(filename.c_str(), FileStream::GetMinizipFilefunc());
|
2013-08-19 14:50:53 +03:00
|
|
|
|
|
|
|
if (unzGoToFirstFile(file) == UNZ_OK)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2016-01-09 22:23:55 +02:00
|
|
|
unz_file_info64 info;
|
2013-08-19 14:50:53 +03:00
|
|
|
std::vector<char> filename;
|
|
|
|
|
2016-01-09 22:23:55 +02:00
|
|
|
unzGetCurrentFileInfo64 (file, &info, nullptr, 0, nullptr, 0, nullptr, 0);
|
2013-08-19 14:50:53 +03:00
|
|
|
|
|
|
|
filename.resize(info.size_filename);
|
|
|
|
// Get name of current file. Contrary to docs "info" parameter can't be null
|
2016-01-09 22:23:55 +02:00
|
|
|
unzGetCurrentFileInfo64 (file, &info, filename.data(), filename.size(), nullptr, 0, nullptr, 0);
|
2013-08-19 14:50:53 +03:00
|
|
|
|
|
|
|
ret.push_back(std::string(filename.data(), filename.size()));
|
|
|
|
}
|
|
|
|
while (unzGoToNextFile(file) == UNZ_OK);
|
|
|
|
}
|
|
|
|
unzClose(file);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:23:55 +02:00
|
|
|
bool ZipArchive::extract(boost::filesystem::path from, boost::filesystem::path where)
|
2013-08-19 14:50:53 +03:00
|
|
|
{
|
|
|
|
// Note: may not be fast enough for large archives (should NOT happen with mods)
|
|
|
|
// because locating each file by name may be slow. Unlikely slower than decompression though
|
|
|
|
return extract(from, where, listFiles(from));
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:23:55 +02:00
|
|
|
bool ZipArchive::extract(boost::filesystem::path from, boost::filesystem::path where, std::vector<std::string> what)
|
2013-08-19 14:50:53 +03:00
|
|
|
{
|
2016-01-09 22:23:55 +02:00
|
|
|
unzFile archive = unzOpen2_64(from.c_str(), FileStream::GetMinizipFilefunc());
|
2013-08-19 14:50:53 +03:00
|
|
|
|
|
|
|
auto onExit = vstd::makeScopeGuard([&]()
|
|
|
|
{
|
|
|
|
unzClose(archive);
|
|
|
|
});
|
|
|
|
|
2016-01-09 22:23:55 +02:00
|
|
|
for (const std::string & file : what)
|
2013-08-19 14:50:53 +03:00
|
|
|
{
|
|
|
|
if (unzLocateFile(archive, file.c_str(), 1) != UNZ_OK)
|
|
|
|
return false;
|
|
|
|
|
2016-01-09 22:23:55 +02:00
|
|
|
const boost::filesystem::path fullName = where / file;
|
|
|
|
const boost::filesystem::path fullPath = fullName.parent_path();
|
2013-08-19 14:50:53 +03:00
|
|
|
|
|
|
|
boost::filesystem::create_directories(fullPath);
|
|
|
|
// directory. No file to extract
|
|
|
|
// TODO: better way to detect directory? Probably check return value of unzOpenCurrentFile?
|
|
|
|
if (boost::algorithm::ends_with(file, "/"))
|
|
|
|
continue;
|
|
|
|
|
2016-01-09 22:23:55 +02:00
|
|
|
FileStream destFile(fullName, std::ios::out | std::ios::binary);
|
2013-08-19 14:50:53 +03:00
|
|
|
if (!destFile.good())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!extractCurrent(archive, destFile))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|