No need to allocate a full size buffer for small files

pull/3556/head
Jean-Louis 2020-12-24 12:18:07 +01:00
parent 895137c7d7
commit effe52368c
1 changed files with 6 additions and 3 deletions

View File

@ -218,16 +218,19 @@ ZipFile *ZipFileInfo::Extract(unzFile zip_handle) const {
ZipFile *zip_file = new ZipFile(m_Size); ZipFile *zip_file = new ZipFile(m_Size);
// Unzip has a limit of UINT16_MAX bytes buffer // Unzip has a limit of UINT16_MAX bytes buffer
std::unique_ptr<uint8_t[]> unzipBuffer = std::unique_ptr<uint8_t[]>(new uint8_t[UINT16_MAX]); uint16_t unzipBufferSize = zip_file->m_Size <= UINT16_MAX ? static_cast<uint16_t>(zip_file->m_Size) : UINT16_MAX;
std::unique_ptr<uint8_t[]> unzipBuffer = std::unique_ptr<uint8_t[]>(new uint8_t[unzipBufferSize]);
size_t readCount = 0; size_t readCount = 0;
while (readCount < zip_file->m_Size) { while (readCount < zip_file->m_Size)
{
size_t bufferSize = zip_file->m_Size - readCount; size_t bufferSize = zip_file->m_Size - readCount;
if (bufferSize > UINT16_MAX) { if (bufferSize > UINT16_MAX) {
bufferSize = UINT16_MAX; bufferSize = UINT16_MAX;
} }
int ret = unzReadCurrentFile(zip_handle, unzipBuffer.get(), static_cast<unsigned int>(bufferSize)); int ret = unzReadCurrentFile(zip_handle, unzipBuffer.get(), static_cast<unsigned int>(bufferSize));
if (ret != static_cast<int>(bufferSize)) { if (ret != static_cast<int>(bufferSize))
{
// Failed, release the memory // Failed, release the memory
delete zip_file; delete zip_file;
zip_file = nullptr; zip_file = nullptr;