Skip to content

Commit 708116f

Browse files
halx99minggo
authored andcommitted
Update ZipUtils.cpp (#20192)
1 parent cc57e2f commit 708116f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

cocos/base/ZipUtils.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#else // from our embedded sources
3232
#include "unzip.h"
3333
#endif
34+
#include "ioapi_mem.h"
35+
#include <memory>
3436

3537
#include <zlib.h>
3638
#include <assert.h>
@@ -509,6 +511,7 @@ class ZipFilePrivate
509511
{
510512
public:
511513
unzFile zipFile;
514+
std::unique_ptr<ourmemory_s> memfs;
512515

513516
// std::unordered_map is faster if available on the platform
514517
typedef std::unordered_map<std::string, struct ZipEntryInfo> FileListContainer;
@@ -739,10 +742,17 @@ int ZipFile::getCurrentFileInfo(std::string *filename, unz_file_info *info)
739742
bool ZipFile::initWithBuffer(const void *buffer, uLong size)
740743
{
741744
if (!buffer || size == 0) return false;
745+
746+
zlib_filefunc_def memory_file = { 0 };
742747

743-
_data->zipFile = unzOpenBuffer(buffer, size);
744-
if (!_data->zipFile) return false;
748+
std::unique_ptr<ourmemory_t> memfs(new(std::nothrow) ourmemory_t{ (char*)const_cast<void*>(buffer), static_cast<uint32_t>(size), 0, 0, 0 });
749+
if (!memfs) return false;
750+
fill_memory_filefunc(&memory_file, memfs.get());
745751

752+
_data->zipFile = unzOpen2(nullptr, &memory_file);
753+
if (!_data->zipFile) return false;
754+
_data->memfs = std::move(memfs);
755+
746756
setFilter(emptyFilename);
747757
return true;
748758
}

0 commit comments

Comments
 (0)