Click here to Skip to main content
16,008,183 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I used zlib.dll to decompress excel format of zip package, the function "unzReadCurrentFile" was throw an exception.

My source code as below:
C++
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)//external file attributes
				{
				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;
}


C++
void CZipHelperImpl::__Unzip2File(unzFile unzfile, const char* file)
{
	assert(NULL != unzfile);
	HANDLE hFile = ::CreateFile(
		file,                      //file name
		GENERIC_WRITE,             //access mode
		0,                         //share mode
		NULL,                      //SD
		OPEN_ALWAYS,               //how to create
		FILE_FLAG_WRITE_THROUGH,   //file attributes
		NULL                       //handle to template file
		);
	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.
Posted
Updated 1-Mar-18 0:44am

1 solution

What exception? What data? We can't access either of those, and they are both vital to solving this.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900