From f7c61baa2f9f248fe91b04619fd8959916bc23b0 Mon Sep 17 00:00:00 2001 From: MelchiorSchuh Date: Wed, 22 Jul 2026 11:39:11 +0200 Subject: [PATCH] fix(UnZipFile): Added fallback to previous method when dealing with bug files which cannot be openned in memory --- src/geode/basic/zip_file.cpp | 37 ++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/geode/basic/zip_file.cpp b/src/geode/basic/zip_file.cpp index 87f176b20..9633e7477 100644 --- a/src/geode/basic/zip_file.cpp +++ b/src/geode/basic/zip_file.cpp @@ -143,17 +143,19 @@ namespace geode Impl( std::string_view file, std::string_view unarchive_temp_filename ) { directory_ = create_directory( file, unarchive_temp_filename ); - if( !load_zip_into_memory( file ) ) + if( !load_zip_into_memory( file ) || !open_reader() ) { - throw OpenGeodeBasicException( nullptr, - OpenGeodeException::TYPE::internal, - "[UnzipFile] Failed to read zip file into memory" ); - } - if( !open_reader() ) - { - throw OpenGeodeBasicException( nullptr, - OpenGeodeException::TYPE::internal, - "[UnzipFile] Error opening zip for reading" ); + Logger::info( "[UnzipFile] Couldn't open zip in memory, trying " + "to open on disk, this could take more time" ); + reader_ = nullptr; + memory_stream_ = nullptr; + if( !create_reader_from_disk( file ) ) + { + std::filesystem::remove_all( directory_ ); + throw OpenGeodeBasicException( nullptr, + OpenGeodeException::TYPE::internal, + "[UnzipFile] Error opening zip for reading" ); + } } } @@ -190,8 +192,8 @@ namespace geode } auto out_path = directory_ / info->filename; std::filesystem::create_directories( out_path.parent_path() ); - FILE* f = fopen( out_path.string().c_str(), "wb" ); - if( !f ) + FILE* file = fopen( out_path.string().c_str(), "wb" ); + if( !file ) { status = mz_zip_reader_goto_next_entry( reader_ ); continue; @@ -204,11 +206,11 @@ namespace geode reader_, buffer.data(), BUF_SIZE ) ) > 0 ) { - fwrite( buffer.data(), 1, bytes_read, f ); + fwrite( buffer.data(), 1, bytes_read, file ); } mz_zip_reader_entry_close( reader_ ); } - fclose( f ); + fclose( file ); status = mz_zip_reader_goto_next_entry( reader_ ); } } @@ -244,6 +246,13 @@ namespace geode return mz_zip_reader_open( reader_, memory_stream_ ) == MZ_OK; } + bool create_reader_from_disk( std::string_view file ) + { + reader_ = mz_zip_reader_create(); + return mz_zip_reader_open_file( reader_, to_string( file ).c_str() ) + == MZ_OK; + } + private: std::filesystem::path directory_; std::vector< uint8_t > zip_data_;