Mesen/Utilities/ZIPReader.cpp

82 lines
1.7 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include <string.h>
2015-12-26 22:11:00 +00:00
#include <sstream>
#include "ZipReader.h"
2015-12-26 22:11:00 +00:00
ZipReader::ZipReader()
{
memset(&_zipArchive, 0, sizeof(mz_zip_archive));
}
2015-12-26 22:11:00 +00:00
ZipReader::~ZipReader()
{
if(_initialized) {
mz_zip_reader_end(&_zipArchive);
}
}
2015-12-26 22:11:00 +00:00
void ZipReader::LoadZipArchive(void* buffer, size_t size)
{
if(mz_zip_reader_init_mem(&_zipArchive, buffer, size, 0)) {
_initialized = true;
}
}
2015-12-26 22:11:00 +00:00
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;
}
2015-12-26 22:11:00 +00:00
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;
}
}
2015-12-26 22:11:00 +00:00
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;
}