2015-08-11 20:20:13 +02:00
|
|
|
/*
|
|
|
|
* CZipSaver.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CZipSaver.h"
|
|
|
|
|
|
|
|
///CZipOutputStream
|
2015-08-12 02:31:06 +02:00
|
|
|
CZipOutputStream::CZipOutputStream(CZipSaver * owner_, zipFile archive, const std::string & archiveFilename):
|
|
|
|
handle(archive),
|
|
|
|
owner(owner_)
|
2015-08-11 20:20:13 +02:00
|
|
|
{
|
|
|
|
//zip_fileinfo fileInfo;
|
|
|
|
|
2015-08-13 02:29:13 +02:00
|
|
|
int status = zipOpenNewFileInZip(handle,
|
2015-08-11 20:20:13 +02:00
|
|
|
archiveFilename.c_str(),
|
|
|
|
nullptr,//todo: use fileInfo,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
"",
|
|
|
|
Z_DEFLATED,
|
|
|
|
Z_DEFAULT_COMPRESSION);
|
2015-08-13 02:29:13 +02:00
|
|
|
|
|
|
|
if(status != ZIP_OK)
|
|
|
|
throw new std::runtime_error("CZipOutputStream: zipOpenNewFileInZip failed");
|
|
|
|
|
2015-08-12 02:31:06 +02:00
|
|
|
owner->activeStream = this;
|
2015-08-11 20:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CZipOutputStream::~CZipOutputStream()
|
|
|
|
{
|
|
|
|
zipCloseFileInZip(handle);
|
2015-08-12 02:31:06 +02:00
|
|
|
owner->activeStream = nullptr;
|
2015-08-11 20:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CZipOutputStream::write(const ui8 * data, si64 size)
|
|
|
|
{
|
|
|
|
int ret = zipWriteInFileInZip(handle, (const void*)data, (unsigned)size);
|
|
|
|
|
|
|
|
if (ret == ZIP_OK)
|
|
|
|
return size;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
///CZipSaver
|
|
|
|
CZipSaver::CZipSaver(std::shared_ptr<CIOApi> api, const std::string & path):
|
|
|
|
ioApi(api),
|
|
|
|
zipApi(ioApi->getApiStructure()),
|
2015-08-12 02:31:06 +02:00
|
|
|
handle(nullptr),
|
|
|
|
activeStream(nullptr)
|
2015-08-11 20:20:13 +02:00
|
|
|
{
|
|
|
|
handle = zipOpen2_64(path.c_str(), APPEND_STATUS_CREATE, nullptr, &zipApi);
|
|
|
|
|
|
|
|
if (handle == nullptr)
|
2015-08-13 02:29:13 +02:00
|
|
|
throw new std::runtime_error("CZipSaver: Failed to create archive");
|
2015-08-11 20:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CZipSaver::~CZipSaver()
|
|
|
|
{
|
2015-08-12 02:31:06 +02:00
|
|
|
if(activeStream != nullptr)
|
|
|
|
{
|
|
|
|
logGlobal->error("CZipSaver::~CZipSaver: active stream found");
|
|
|
|
zipCloseFileInZip(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-11 20:20:13 +02:00
|
|
|
if(handle != nullptr)
|
|
|
|
zipClose(handle, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<COutputStream> CZipSaver::addFile(const std::string & archiveFilename)
|
|
|
|
{
|
|
|
|
if(activeStream != nullptr)
|
|
|
|
throw new std::runtime_error("CZipSaver::addFile: stream already opened");
|
|
|
|
|
2015-08-12 02:31:06 +02:00
|
|
|
std::unique_ptr<COutputStream> stream(new CZipOutputStream(this, handle, archiveFilename));
|
|
|
|
return std::move(stream);
|
2015-08-11 20:20:13 +02:00
|
|
|
}
|
|
|
|
|