| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
|
| 8 |
#include <mz.h> |
| 9 |
#include <mz_zip.h> |
| 10 |
#include <mz_zip_rw.h> |
| 11 |
|
| 12 |
#include "Common/CommonTypes.h" |
| 13 |
#include "Common/ScopeGuard.h" |
| 14 |
|
| 15 |
namespace Common |
| 16 |
{ |
| 17 |
|
| 18 |
inline bool ReadFileFromZip(void* zip_reader, u8* destination, u64 len) |
| 19 |
{ |
| 20 |
const u64 MAX_BUFFER_SIZE = 65535; |
| 21 |
|
| 22 |
if (mz_zip_reader_entry_open(zip_reader) != MZ_OK) |
| 23 |
return false; |
| 24 |
|
| 25 |
Common::ScopeGuard guard{[&] { mz_zip_reader_entry_close(zip_reader); }}; |
| 26 |
|
| 27 |
u64 bytes_to_go = len; |
| 28 |
while (bytes_to_go > 0) |
| 29 |
{ |
| 30 |
|
| 31 |
|
| 32 |
const u32 read_len = static_cast<u32>(std::min(bytes_to_go, MAX_BUFFER_SIZE)); |
| 33 |
const int rv = mz_zip_reader_entry_read(zip_reader, destination, read_len); |
| 34 |
if (rv < 0) |
| 35 |
return false; |
| 36 |
|
| 37 |
const u32 bytes_read = static_cast<u32>(rv); |
| 38 |
bytes_to_go -= bytes_read; |
| 39 |
destination += bytes_read; |
| 40 |
} |
| 41 |
|
| 42 |
return bytes_to_go == 0; |
| 43 |
} |
| 44 |
|
| 45 |
template <typename ContiguousContainer> |
| 46 |
bool ReadFileFromZip(void* file, ContiguousContainer* destination) |
| 47 |
{ |
| 48 |
return ReadFileFromZip(file, reinterpret_cast<u8*>(destination->data()), destination->size()); |
| 49 |
} |
| 50 |
} |