2016-11-13 14:48:24 +01:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2016-11-13 14:48:24 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2016-11-13 14:48:24 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-11-13 14:48:24 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2017-06-15 14:22:15 +02:00
|
|
|
#ifndef __ANDROID__
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "Zip.h"
|
2016-11-13 14:48:24 +01:00
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "IStream.hpp"
|
2018-06-22 22:58:39 +02:00
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include <zip.h>
|
2018-06-22 22:58:39 +02:00
|
|
|
|
2017-01-13 12:12:50 +01:00
|
|
|
class ZipArchive final : public IZipArchive
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
|
|
|
private:
|
2018-06-22 22:58:39 +02:00
|
|
|
zip_t* _zip;
|
2018-05-02 14:15:26 +02:00
|
|
|
ZIP_ACCESS _access;
|
2018-06-20 17:28:51 +02:00
|
|
|
std::vector<std::vector<uint8_t>> _writeBuffers;
|
2016-11-13 14:48:24 +01:00
|
|
|
|
|
|
|
public:
|
2021-01-06 17:22:22 +01:00
|
|
|
ZipArchive(std::string_view path, ZIP_ACCESS access)
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
2018-05-02 14:15:26 +02:00
|
|
|
auto zipOpenMode = ZIP_RDONLY;
|
|
|
|
if (access == ZIP_ACCESS::WRITE)
|
2016-11-19 15:38:05 +01:00
|
|
|
{
|
|
|
|
zipOpenMode = ZIP_CREATE;
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t error;
|
2021-01-06 17:22:22 +01:00
|
|
|
_zip = zip_open(std::string(path).data(), zipOpenMode, &error);
|
2016-11-13 14:48:24 +01:00
|
|
|
if (_zip == nullptr)
|
|
|
|
{
|
|
|
|
throw IOException("Unable to open zip file.");
|
|
|
|
}
|
2016-11-19 15:38:05 +01:00
|
|
|
|
|
|
|
_access = access;
|
2016-11-13 14:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~ZipArchive() override
|
|
|
|
{
|
|
|
|
zip_close(_zip);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetNumFiles() const override
|
|
|
|
{
|
2018-05-12 17:11:51 +02:00
|
|
|
return zip_get_num_entries(_zip, 0);
|
2016-11-13 14:48:24 +01:00
|
|
|
}
|
|
|
|
|
2018-05-02 14:15:26 +02:00
|
|
|
std::string GetFileName(size_t index) const override
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
2018-05-02 14:15:26 +02:00
|
|
|
std::string result;
|
|
|
|
auto name = zip_get_name(_zip, index, ZIP_FL_ENC_GUESS);
|
|
|
|
if (name != nullptr)
|
|
|
|
{
|
|
|
|
result = name;
|
|
|
|
}
|
|
|
|
return result;
|
2016-11-13 14:48:24 +01:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint64_t GetFileSize(size_t index) const override
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
|
|
|
zip_stat_t zipFileStat;
|
|
|
|
if (zip_stat_index(_zip, index, 0, &zipFileStat) == ZIP_ER_OK)
|
|
|
|
{
|
|
|
|
return zipFileStat.size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 17:22:22 +01:00
|
|
|
std::vector<uint8_t> GetFileData(std::string_view path) const override
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
std::vector<uint8_t> result;
|
2018-05-12 17:11:51 +02:00
|
|
|
auto index = GetIndexFromPath(path);
|
2018-05-02 14:15:26 +02:00
|
|
|
auto dataSize = GetFileSize(index);
|
2016-11-13 14:48:24 +01:00
|
|
|
if (dataSize > 0 && dataSize < SIZE_MAX)
|
|
|
|
{
|
2018-05-12 17:11:51 +02:00
|
|
|
auto zipFile = zip_fopen_index(_zip, index, 0);
|
2016-11-13 14:48:24 +01:00
|
|
|
if (zipFile != nullptr)
|
|
|
|
{
|
2020-04-22 17:09:29 +02:00
|
|
|
result.resize(static_cast<size_t>(dataSize));
|
2018-06-20 17:28:51 +02:00
|
|
|
uint64_t readBytes = zip_fread(zipFile, result.data(), dataSize);
|
2016-11-13 14:48:24 +01:00
|
|
|
if (readBytes != dataSize)
|
|
|
|
{
|
2018-05-02 14:15:26 +02:00
|
|
|
result.clear();
|
|
|
|
result.shrink_to_fit();
|
2016-11-13 14:48:24 +01:00
|
|
|
}
|
|
|
|
zip_fclose(zipFile);
|
|
|
|
}
|
|
|
|
}
|
2018-05-02 14:15:26 +02:00
|
|
|
return result;
|
2016-11-13 14:48:24 +01:00
|
|
|
}
|
2016-11-19 15:38:05 +01:00
|
|
|
|
2021-01-06 17:22:22 +01:00
|
|
|
void SetFileData(std::string_view path, std::vector<uint8_t>&& data) override
|
2017-01-31 20:15:48 +01:00
|
|
|
{
|
2018-05-02 14:15:26 +02:00
|
|
|
// Push buffer to an internal list as libzip requires access to it until the zip
|
|
|
|
// handle is closed.
|
|
|
|
_writeBuffers.push_back(std::move(data));
|
|
|
|
const auto& writeBuffer = *_writeBuffers.rbegin();
|
2017-01-31 20:15:48 +01:00
|
|
|
|
2018-05-02 14:15:26 +02:00
|
|
|
auto source = zip_source_buffer(_zip, writeBuffer.data(), writeBuffer.size(), 0);
|
2018-05-12 17:11:51 +02:00
|
|
|
auto index = GetIndexFromPath(path);
|
2016-11-19 15:38:05 +01:00
|
|
|
if (index == -1)
|
|
|
|
{
|
2018-05-02 14:15:26 +02:00
|
|
|
zip_add(_zip, path.data(), source);
|
2016-11-19 15:38:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
zip_replace(_zip, index, source);
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 17:52:42 +01:00
|
|
|
|
2021-01-06 17:22:22 +01:00
|
|
|
void DeleteFile(std::string_view path) override
|
2016-12-05 17:52:42 +01:00
|
|
|
{
|
2018-05-12 17:11:51 +02:00
|
|
|
auto index = GetIndexFromPath(path);
|
2016-12-05 17:52:42 +01:00
|
|
|
zip_delete(_zip, index);
|
|
|
|
}
|
2016-12-07 13:00:02 +01:00
|
|
|
|
2021-01-06 17:22:22 +01:00
|
|
|
void RenameFile(std::string_view path, std::string_view newPath) override
|
2016-12-07 13:00:02 +01:00
|
|
|
{
|
2018-05-12 17:11:51 +02:00
|
|
|
auto index = GetIndexFromPath(path);
|
2018-05-02 14:15:26 +02:00
|
|
|
zip_file_rename(_zip, index, newPath.data(), ZIP_FL_ENC_GUESS);
|
2016-12-07 13:00:02 +01:00
|
|
|
}
|
2018-05-12 17:11:51 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Normalises both the given path and the stored paths and finds the first match.
|
|
|
|
*/
|
2021-01-06 17:22:22 +01:00
|
|
|
zip_int64_t GetIndexFromPath(std::string_view path) const
|
2018-05-12 17:11:51 +02:00
|
|
|
{
|
|
|
|
auto normalisedPath = NormalisePath(path);
|
|
|
|
if (!normalisedPath.empty())
|
|
|
|
{
|
|
|
|
auto numFiles = zip_get_num_entries(_zip, 0);
|
|
|
|
for (zip_int64_t i = 0; i < numFiles; i++)
|
|
|
|
{
|
|
|
|
auto normalisedZipPath = NormalisePath(zip_get_name(_zip, i, ZIP_FL_ENC_GUESS));
|
|
|
|
if (normalisedZipPath == normalisedPath)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-06 17:22:22 +01:00
|
|
|
static std::string NormalisePath(std::string_view path)
|
2018-05-12 17:11:51 +02:00
|
|
|
{
|
2021-01-06 17:22:22 +01:00
|
|
|
std::string result(path);
|
2018-05-12 17:11:51 +02:00
|
|
|
if (!path.empty())
|
|
|
|
{
|
|
|
|
// Convert back slashes to forward slashes
|
|
|
|
for (auto ch = result.data(); *ch != '\0'; ch++)
|
|
|
|
{
|
|
|
|
if (*ch == '\\')
|
|
|
|
{
|
|
|
|
*ch = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-11-13 14:48:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace Zip
|
|
|
|
{
|
2021-01-06 17:22:22 +01:00
|
|
|
std::unique_ptr<IZipArchive> Open(std::string_view path, ZIP_ACCESS access)
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
2018-05-02 14:15:26 +02:00
|
|
|
return std::make_unique<ZipArchive>(path, access);
|
2016-11-13 14:48:24 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 17:22:22 +01:00
|
|
|
std::unique_ptr<IZipArchive> TryOpen(std::string_view path, ZIP_ACCESS access)
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
2018-05-02 14:15:26 +02:00
|
|
|
std::unique_ptr<IZipArchive> result;
|
2016-11-13 14:48:24 +01:00
|
|
|
try
|
|
|
|
{
|
2018-05-02 14:15:26 +02:00
|
|
|
result = std::make_unique<ZipArchive>(path, access);
|
2016-11-13 14:48:24 +01:00
|
|
|
}
|
2018-05-02 14:15:26 +02:00
|
|
|
catch (const std::exception&)
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-05-04 22:40:09 +02:00
|
|
|
} // namespace Zip
|
2017-06-15 14:22:15 +02:00
|
|
|
|
2018-06-22 22:58:39 +02:00
|
|
|
#endif
|