Mesen/Utilities/ZIPReader.cpp
2015-12-26 17:11:00 -05:00

82 lines
1.7 KiB
C++

#include "stdafx.h"
#include <string.h>
#include <sstream>
#include "ZipReader.h"
ZipReader::ZipReader()
{
memset(&_zipArchive, 0, sizeof(mz_zip_archive));
}
ZipReader::~ZipReader()
{
if(_initialized) {
mz_zip_reader_end(&_zipArchive);
}
}
void ZipReader::LoadZipArchive(void* buffer, size_t size)
{
if(mz_zip_reader_init_mem(&_zipArchive, buffer, size, 0)) {
_initialized = true;
}
}
void ZipReader::LoadZipArchive(string filepath)
{
if(mz_zip_reader_init_file(&_zipArchive, filepath.c_str(), 0)) {
_initialized = true;
}
}
vector<string> ZipReader::GetFileList()
{
vector<string> fileList;
if(_initialized) {
for(int i = 0, len = (int)mz_zip_reader_get_num_files(&_zipArchive); i < len; i++) {
mz_zip_archive_file_stat file_stat;
if(!mz_zip_reader_file_stat(&_zipArchive, i, &file_stat)) {
std::cout << "mz_zip_reader_file_stat() failed!" << std::endl;
}
fileList.push_back(file_stat.m_filename);
}
}
return fileList;
}
void ZipReader::ExtractFile(string filename, uint8_t **fileBuffer, size_t &fileSize)
{
if(_initialized) {
size_t uncompSize;
void *p = mz_zip_reader_extract_file_to_heap(&_zipArchive, filename.c_str(), &uncompSize, 0);
if(!p) {
std::cout << "mz_zip_reader_extract_file_to_heap() failed!" << std::endl;
}
*fileBuffer = new uint8_t[uncompSize];
memcpy(*fileBuffer, p, uncompSize);
// We're done.
mz_free(p);
fileSize = uncompSize;
}
}
std::stringstream ZipReader::ExtractFile(string filename)
{
std::stringstream ss;
if(_initialized) {
uint8_t* buffer = nullptr;
size_t size = 0;
ExtractFile(filename, &buffer, size);
ss.write((char*)buffer, size);
delete[] buffer;
}
return ss;
}