#include "fsCZipFile.h"
#include "miniz/miniz.h"
#include "fsCResourceName.h"
#include "fsIFile.h"
#include "debug/fsAssert.h"
struct fsCZipFile::Impl {
mz_zip_archive archive;
fsStr zipBytes;
};
fsCZipFile::fsCZipFile(const fsCResourceName& pResourceName) :
fsIZipFile(pResourceName)
{
mArchive = std::make_unique<Impl>();<--- Variable 'mArchive' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'mArchive' a value by passing the value to the constructor in the initialization list.
memset(&mArchive->archive, 0, sizeof(mArchive->archive));
mArchive->zipBytes = fsIFile::bytesGetFromCompressedFileBinary(pResourceName);
const fsU8* zipData = mArchive->zipBytes.ptrGet();
const size_t zipSize = static_cast<size_t>(mArchive->zipBytes.dataSizeGet());
if (mz_zip_reader_init_mem(&mArchive->archive, zipData, zipSize, 0) != MZ_TRUE) {
// Reads then find no entries; say why rather than fail silently per asset.
fsLogError("Could not open zip %s: %s", pResourceName.nameGet().utf8Get(),
mz_zip_get_error_string(mz_zip_get_last_error(&mArchive->archive)));
}
}
fsStr fsCZipFile::assetRead(fsStr &pAssetName)
{
std::string entry = pAssetName.utf8Get(); // exporter stores entries at zip root
int fileIndex = mz_zip_reader_locate_file(&mArchive->archive, entry.c_str(), nullptr, 0);
fsAssert(fileIndex >= 0, "cluster load error (missing zip entry)");
size_t outSize = 0;
void* outBuf = mz_zip_reader_extract_to_heap(&mArchive->archive, fileIndex, &outSize, 0);
fsAssert(outBuf && outSize > 0, "cluster load error (zip extract)");
fsStr out(reinterpret_cast<const char*>(outBuf), static_cast<fsS32>(outSize));
mz_free(outBuf);
return out;
}
fsBool fsCZipFile::assetExistsGet(fsStr &pAssetName)
{
std::string entry = pAssetName.utf8Get(); // exporter stores entries at zip root
int fileIndex = mz_zip_reader_locate_file(&mArchive->archive, entry.c_str(), nullptr, 0);
return fileIndex >= 0;
}
fsCZipFile::~fsCZipFile()
{
mz_zip_reader_end(&mArchive->archive);
}