I used zlib.dll to decompress excel format of zip package, the function "unzReadCurrentFile" was throw an exception.
My source code as below:
ZIP_ERR CZipHelperImpl::Uncompress(const char* pSrcFile, const char* pDestPath)
{
unzFile unzfile = NULL;
ZIP_ERR errCode = ZIP_OK;
try
{
unzfile = unzOpen64(pSrcFile);
if (NULL == unzfile)
{
throw ZIP_OPEN_SRC_FILE_FAIL;
}
unz_global_info64* pstGlobalInfo = new unz_global_info64;
unz_file_info64* pstFileInfo = new unz_file_info64;
auto ReleaseResource = [&pstGlobalInfo, &pstFileInfo, &unzfile]() -> void
{
delete pstGlobalInfo;
delete pstFileInfo;
unzClose(unzfile);
};
try
{
if(UNZ_OK != unzGetGlobalInfo64(unzfile, pstGlobalInfo))
{
throw ZIP_GET_ZIP_GLOBAL_INFO_FAIL;
}
char szZipFileName[256] = {'\0'};
for (uLong count = 0; count < pstGlobalInfo->number_entry; count++)
{
if(UNZ_OK != unzGetCurrentFileInfo64(unzfile, pstFileInfo, szZipFileName, 256, NULL, 0, NULL, 0))
{
throw ZIP_GET_ZIP_CUR_FILE_INFO_FAIL;
}
std::string archive = std::string(pDestPath) + "\\" + std::string(szZipFileName);
switch (pstFileInfo->external_fa) {
case FILE_ATTRIBUTE_DIRECTORY:
{
CreateDirectory(archive.c_str(), NULL);
}
break;
default:
{
if(UNZ_OK != unzOpenCurrentFile(unzfile))
{
throw ZIP_OPEN_ZIP_CUR_FILE_FAIL;
}
__Unzip2File(unzfile, archive.c_str());
if(UNZ_OK != unzCloseCurrentFile(unzfile))
{
throw ZIP_CLOSE_ZIP_CUR_FILE_FAIL;
}
}
}
unzGoToNextFile(unzfile);
}
ReleaseResource();
}
catch(const ZIP_ERR& err)
{
ReleaseResource();
errCode = err;
}
}
catch(const ZIP_ERR& err)
{
errCode = err;
}
return errCode;
}
void CZipHelperImpl::__Unzip2File(unzFile unzfile, const char* file)
{
assert(NULL != unzfile);
HANDLE hFile = ::CreateFile(
file, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL );
if (INVALID_HANDLE_VALUE == hFile)
{
throw ZIP_CREATE_UNZIP_FILE_FAIL;
}
try
{
std::string buf(BUFLEN, '\0');
int len = 0;
while(true)
{
buf.clear();
buf.resize(BUFLEN, '\0');
len = unzReadCurrentFile(unzfile, const_cast<char*>(buf.c_str()), BUFLEN);
if(0 > len)
{
throw ZIP_READ_ZIP_FILE_FAIL;
}
else if (0 == len)
{
break;
}
else
{
DWORD dwNumOfWrite = 0;
if(!::WriteFile(hFile, (LPCVOID)buf.c_str(), len, &dwNumOfWrite, NULL))
{
throw ZIP_WRITE_UNZIP_FILE_FAIL;
}
}
}
::CloseHandle(hFile);
}
catch(const ZIP_ERR& err)
{
::CloseHandle(hFile);
throw err;
}
}
can you help me to check this issue?
Thanks very much!
What I have tried:
I have tried to decompress txt format of zip package, but this operate is successful.