本文介绍了使用zlib.dll解压缩文件会发生致命错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用zlib.dll解压缩zip包的excel格式,函数unzReadCurrentFile抛出异常。



我的源代码如下:

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)//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;
}







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;
	}
}





你可以帮我查一下这个问题吗?



非常感谢!



我尝试过:



我试图解压缩zip包的txt格式,但这个操作是成功的。



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.

推荐答案


这篇关于使用zlib.dll解压缩文件会发生致命错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 14:15
查看更多