2016-11-13 14:48:24 +01:00
|
|
|
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
|
|
|
/*****************************************************************************
|
|
|
|
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
|
|
|
*
|
|
|
|
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
|
|
|
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
*
|
|
|
|
* OpenRCT2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* A full copy of the GNU General Public License can be found in licence.txt
|
|
|
|
*****************************************************************************/
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
#include <zip.h>
|
|
|
|
#include "IStream.hpp"
|
|
|
|
#include "Zip.h"
|
|
|
|
|
|
|
|
class ZipArchive : public IZipArchive
|
|
|
|
{
|
|
|
|
private:
|
2016-11-19 15:38:05 +01:00
|
|
|
zip_t * _zip;
|
|
|
|
ZIP_ACCESS _access;
|
2016-11-13 14:48:24 +01:00
|
|
|
|
|
|
|
public:
|
2016-11-19 15:38:05 +01:00
|
|
|
ZipArchive(const utf8 * path, ZIP_ACCESS access)
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
2016-11-19 15:38:05 +01:00
|
|
|
int zipOpenMode = ZIP_RDONLY;
|
|
|
|
if (access == ZIP_ACCESS_WRITE)
|
|
|
|
{
|
|
|
|
zipOpenMode = ZIP_CREATE;
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:48:24 +01:00
|
|
|
int error;
|
2016-11-19 15:38:05 +01:00
|
|
|
_zip = zip_open(path, 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
|
|
|
|
{
|
|
|
|
return zip_get_num_files(_zip);
|
|
|
|
}
|
|
|
|
|
|
|
|
const utf8 * GetFileName(size_t index) const override
|
|
|
|
{
|
|
|
|
const utf8 * name = zip_get_name(_zip, index, ZIP_FL_ENC_GUESS);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint64 GetFileSize(size_t index) const override
|
|
|
|
{
|
|
|
|
zip_stat_t zipFileStat;
|
|
|
|
if (zip_stat_index(_zip, index, 0, &zipFileStat) == ZIP_ER_OK)
|
|
|
|
{
|
|
|
|
return zipFileStat.size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void * GetFileData(const utf8 * path, size_t * outSize) const override
|
|
|
|
{
|
|
|
|
void * data = nullptr;
|
|
|
|
|
|
|
|
size_t index = (size_t)zip_name_locate(_zip, path, 0);
|
|
|
|
uint64 dataSize = GetFileSize(index);
|
|
|
|
if (dataSize > 0 && dataSize < SIZE_MAX)
|
|
|
|
{
|
|
|
|
zip_file_t * zipFile = zip_fopen(_zip, path, 0);
|
|
|
|
if (zipFile != nullptr)
|
|
|
|
{
|
2016-11-21 01:16:47 +01:00
|
|
|
data = Memory::Allocate<void>((size_t)dataSize);
|
|
|
|
uint64 readBytes = zip_fread(zipFile, data, dataSize);
|
2016-11-13 14:48:24 +01:00
|
|
|
if (readBytes != dataSize)
|
|
|
|
{
|
2016-11-21 01:16:47 +01:00
|
|
|
Memory::Free(data);
|
2016-11-13 14:48:24 +01:00
|
|
|
data = nullptr;
|
|
|
|
dataSize = 0;
|
|
|
|
}
|
|
|
|
zip_fclose(zipFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 01:16:47 +01:00
|
|
|
if (outSize != nullptr) *outSize = (size_t)dataSize;
|
2016-11-13 14:48:24 +01:00
|
|
|
return data;
|
|
|
|
}
|
2016-11-19 15:38:05 +01:00
|
|
|
|
|
|
|
void SetFileData(const utf8 * path, void * data, size_t dataSize) override
|
|
|
|
{
|
|
|
|
zip_source_t * source = zip_source_buffer(_zip, data, dataSize, 0);
|
|
|
|
sint64 index = zip_name_locate(_zip, path, 0);
|
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
zip_add(_zip, path, source);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
zip_replace(_zip, index, source);
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 14:48:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace Zip
|
|
|
|
{
|
2016-11-19 15:38:05 +01:00
|
|
|
IZipArchive * Open(const utf8 * path, ZIP_ACCESS access)
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
2016-11-19 15:38:05 +01:00
|
|
|
return new ZipArchive(path, access);
|
2016-11-13 14:48:24 +01:00
|
|
|
}
|
|
|
|
|
2016-11-19 15:38:05 +01:00
|
|
|
IZipArchive * TryOpen(const utf8 * path, ZIP_ACCESS access)
|
2016-11-13 14:48:24 +01:00
|
|
|
{
|
|
|
|
IZipArchive * result = nullptr;
|
|
|
|
try
|
|
|
|
{
|
2016-11-19 15:38:05 +01:00
|
|
|
result = new ZipArchive(path, access);
|
2016-11-13 14:48:24 +01:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|