OpenRCT2/src/openrct2/core/Zip.cpp

192 lines
5.1 KiB
C++
Raw Normal View History

2016-11-13 14:48:24 +01:00
/*****************************************************************************
* Copyright (c) 2014-2018 OpenRCT2 developers
2016-11-13 14:48:24 +01: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
*
* 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__
2016-11-13 14:48:24 +01:00
#include <zip.h>
#include "IStream.hpp"
#include "Zip.h"
class ZipArchive final : public IZipArchive
2016-11-13 14:48:24 +01:00
{
private:
zip_t * _zip;
ZIP_ACCESS _access;
std::vector<std::vector<uint8_t>> _writeBuffers;
2016-11-13 14:48:24 +01:00
public:
ZipArchive(const std::string_view& path, ZIP_ACCESS access)
2016-11-13 14:48:24 +01:00
{
auto zipOpenMode = ZIP_RDONLY;
if (access == ZIP_ACCESS::WRITE)
{
zipOpenMode = ZIP_CREATE;
}
int32_t error;
_zip = zip_open(path.data(), zipOpenMode, &error);
2016-11-13 14:48:24 +01:00
if (_zip == nullptr)
{
throw IOException("Unable to open zip file.");
}
_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
}
std::string GetFileName(size_t index) const override
2016-11-13 14:48:24 +01: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
}
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;
}
}
std::vector<uint8_t> GetFileData(const std::string_view& path) const override
2016-11-13 14:48:24 +01:00
{
std::vector<uint8_t> result;
2018-05-12 17:11:51 +02:00
auto index = GetIndexFromPath(path);
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)
{
result.resize((size_t)dataSize);
uint64_t readBytes = zip_fread(zipFile, result.data(), dataSize);
2016-11-13 14:48:24 +01:00
if (readBytes != dataSize)
{
result.clear();
result.shrink_to_fit();
2016-11-13 14:48:24 +01:00
}
zip_fclose(zipFile);
}
}
return result;
2016-11-13 14:48:24 +01:00
}
void SetFileData(const std::string_view& path, std::vector<uint8_t>&& data) override
{
// 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();
auto source = zip_source_buffer(_zip, writeBuffer.data(), writeBuffer.size(), 0);
2018-05-12 17:11:51 +02:00
auto index = GetIndexFromPath(path);
if (index == -1)
{
zip_add(_zip, path.data(), source);
}
else
{
zip_replace(_zip, index, source);
}
}
2016-12-05 17:52:42 +01:00
void DeleteFile(const 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
void RenameFile(const std::string_view& path, const 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);
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.
*/
zip_int64_t GetIndexFromPath(const std::string_view& path) const
{
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;
}
static std::string NormalisePath(const std::string_view& path)
{
std::string result;
if (!path.empty())
{
// Convert back slashes to forward slashes
2018-05-13 23:22:40 +02:00
result = std::string(path);
2018-05-12 17:11:51 +02:00
for (auto ch = result.data(); *ch != '\0'; ch++)
{
if (*ch == '\\')
{
*ch = '/';
}
}
}
return result;
}
2016-11-13 14:48:24 +01:00
};
namespace Zip
{
std::unique_ptr<IZipArchive> Open(const std::string_view& path, ZIP_ACCESS access)
2016-11-13 14:48:24 +01:00
{
return std::make_unique<ZipArchive>(path, access);
2016-11-13 14:48:24 +01:00
}
std::unique_ptr<IZipArchive> TryOpen(const std::string_view& path, ZIP_ACCESS access)
2016-11-13 14:48:24 +01:00
{
std::unique_ptr<IZipArchive> result;
2016-11-13 14:48:24 +01:00
try
{
result = std::make_unique<ZipArchive>(path, access);
2016-11-13 14:48:24 +01: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
# endif