使用zlib.dll解压缩文件会发生致命错误。 [英] Used zlib.dll to unzip file occur fatal error.

查看:248
本文介绍了使用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.

推荐答案

有什么例外?什么数据?我们无法访问其中任何一个,它们对于解决这个问题至关重要。

所以,这将取决于你。

幸运的是,你有一个工具可供您使用,它将帮助您了解正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
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!


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆