@@ -54,10 +54,21 @@ namespace QuickView {
5454 void (*progressCb)(float progress, void * userData),
5555 void* userData
5656 ) {
57+ char logBuf[512 ];
58+ sprintf_s (logBuf, " [QVX-Extract] ExtractZipToDirectory: opening '%ls' -> '%ls'\n " , zipPath.c_str (), destDir.c_str ());
59+ OutputDebugStringA (logBuf);
60+
5761 ZipArchive zip (zipPath);
58- if (!zip.IsValid ()) return false ;
62+ if (!zip.IsValid ()) {
63+ sprintf_s (logBuf, " [QVX-Extract] Failed: zip.IsValid() is false for '%ls'\n " , zipPath.c_str ());
64+ OutputDebugStringA (logBuf);
65+ return false ;
66+ }
5967
6068 size_t totalEntries = zip.GetEntryCount ();
69+ sprintf_s (logBuf, " [QVX-Extract] Zip has %zu entries\n " , totalEntries);
70+ OutputDebugStringA (logBuf);
71+
6172 if (totalEntries == 0 ) return true ;
6273
6374 EnsureParentDirectory (destDir + L" \\ dummy.tmp" );
@@ -78,21 +89,37 @@ namespace QuickView {
7889 }
7990
8091 ::std::wstring outPath = destDir + L" \\ " + relName;
92+ if (relName.back () == L' \\ ' ) {
93+ EnsureParentDirectory (outPath + L" \\ dummy.tmp" );
94+ continue ;
95+ }
8196 EnsureParentDirectory (outPath);
8297
8398 const ArchiveEntry& entry = zip.GetEntry (i);
99+ sprintf_s (logBuf, " [QVX-Extract] Entry [%zu]: '%ls', uncompSize=%u, method=%u\n " , i, relName.c_str (), entry.uncompSize , entry.method );
100+ OutputDebugStringA (logBuf);
101+
84102 if (buffer.size () < entry.uncompSize ) {
85103 buffer.resize (entry.uncompSize );
86104 }
87105
88106 size_t extractedSize = zip.ExtractEntry (i, buffer.data (), buffer.size ());
89107 if (extractedSize != entry.uncompSize ) {
108+ sprintf_s (logBuf, " [QVX-Extract] ExtractEntry failed for '%ls' (got %zu, expected %u)\n " , relName.c_str (), extractedSize, entry.uncompSize );
109+ OutputDebugStringA (logBuf);
90110 return false ;
91111 }
92112
93- HANDLE hFile = CreateFileW (outPath.c_str (), GENERIC_WRITE , 0 , nullptr , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , nullptr );
113+ SetFileAttributesW (outPath.c_str (), FILE_ATTRIBUTE_NORMAL );
114+ HANDLE hFile = CreateFileW (outPath.c_str (), GENERIC_WRITE , FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , nullptr , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , nullptr );
94115 if (hFile == INVALID_HANDLE_VALUE ) {
95- return false ;
116+ DeleteFileW (outPath.c_str ());
117+ hFile = CreateFileW (outPath.c_str (), GENERIC_WRITE , FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , nullptr , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , nullptr );
118+ if (hFile == INVALID_HANDLE_VALUE ) {
119+ sprintf_s (logBuf, " [QVX-Extract] CreateFileW failed for '%ls' (err=%lu)\n " , outPath.c_str (), GetLastError ());
120+ OutputDebugStringA (logBuf);
121+ return false ;
122+ }
96123 }
97124
98125 DWORD written = 0 ;
@@ -103,6 +130,8 @@ namespace QuickView {
103130 CloseHandle (hFile);
104131
105132 if (!writeOk || written != entry.uncompSize ) {
133+ sprintf_s (logBuf, " [QVX-Extract] WriteFile failed for '%ls' (written=%lu, expected=%u, err=%lu)\n " , outPath.c_str (), written, entry.uncompSize , GetLastError ());
134+ OutputDebugStringA (logBuf);
106135 DeleteFileW (outPath.c_str ());
107136 return false ;
108137 }
@@ -113,6 +142,8 @@ namespace QuickView {
113142 }
114143 }
115144
145+ sprintf_s (logBuf, " [QVX-Extract] ExtractZipToDirectory completed successfully for '%ls'\n " , zipPath.c_str ());
146+ OutputDebugStringA (logBuf);
116147 return true ;
117148 }
118149
@@ -290,12 +321,19 @@ namespace QuickView {
290321 if (inflateInit2 (m_zstream.get (), -MAX_WBITS ) != Z_OK ) return 0 ;
291322 m_zstream_init = true ;
292323 } else {
293- if (inflateReset (m_zstream.get ()) != Z_OK ) return 0 ;
324+ if (inflateReset (m_zstream.get ()) != Z_OK ) {
325+ inflateEnd (m_zstream.get ());
326+ if (inflateInit2 (m_zstream.get (), -MAX_WBITS ) != Z_OK ) return 0 ;
327+ }
294328 }
295329
296330 int ret = inflate (m_zstream.get (), Z_FINISH );
297331
298- if (ret != Z_STREAM_END && ret != Z_OK ) return 0 ;
332+ if (ret != Z_STREAM_END && ret != Z_OK ) {
333+ inflateEnd (m_zstream.get ());
334+ m_zstream_init = false ;
335+ return 0 ;
336+ }
299337 } else {
300338 // Unsupported compression method
301339 return false ;
0 commit comments