2019-08-23 22:38:44 +02:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-08-23 22:38:44 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2020-04-29 12:39:30 +02:00
|
|
|
#include <unzip.h>
|
2019-08-23 22:38:44 +02:00
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/ScopeGuard.h"
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
// Reads all of the current file. destination must be big enough to fit the whole file.
|
|
|
|
template <typename ContiguousContainer>
|
|
|
|
bool ReadFileFromZip(unzFile file, ContiguousContainer* destination)
|
|
|
|
{
|
|
|
|
const u32 MAX_BUFFER_SIZE = 65535;
|
|
|
|
|
|
|
|
if (unzOpenCurrentFile(file) != UNZ_OK)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Common::ScopeGuard guard{[&] { unzCloseCurrentFile(file); }};
|
|
|
|
|
|
|
|
u32 bytes_to_go = static_cast<u32>(destination->size());
|
|
|
|
while (bytes_to_go > 0)
|
|
|
|
{
|
|
|
|
const int bytes_read =
|
|
|
|
unzReadCurrentFile(file, &(*destination)[destination->size() - bytes_to_go],
|
|
|
|
std::min(bytes_to_go, MAX_BUFFER_SIZE));
|
|
|
|
|
|
|
|
if (bytes_read < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bytes_to_go -= static_cast<u32>(bytes_read);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace Common
|