Ejemplo n.º 1
0
bool CSimpleZip::Add( const void* src, const unsigned int& srcSize, void* dst, unsigned int& dstSize, std::string& szErrmsg)
{
	if ( NULL==src )
	{
		szErrmsg =  "CSimpleZip: Invalid source.";
		return false;
	}

	bool bRet = false;
	CZipMemFile mfIn;
	CZipMemFile mfOut;
	CZipArchive zip;
	try
	{
		mfIn.Write(src, srcSize);
		zip.Open(mfOut, CZipArchive::zipCreate);
		zip.AddNewFile(mfIn, _T("temp.txt"));
		zip.Close();
		int nLen = (int)mfOut.GetLength();
		if ( NULL==dst || dstSize<nLen )
		{
			dstSize = nLen;
			szErrmsg = "CSimpleZip: The size of destination buffer is too small.";
			return false;
		}
		BYTE* b = mfOut.Detach();
		memcpy_s(dst, dstSize, b, nLen);
		dstSize = nLen;
		free(b);
		bRet = true;
	}
	catch(CZipException& e)
	{
		if (!zip.IsClosed())
			zip.Close();
		szErrmsg =  e.GetErrorDescription().c_str();
	}
	catch(...)
	{
		if (!zip.IsClosed())
			zip.Close();
		szErrmsg =  "CSimpleZip: failed to add, catch exception.";
	}
	return bRet;
}
Ejemplo n.º 2
0
bool CSimpleZip::Extract( const void* src, const unsigned int& srcSize, void* dst, unsigned int& dstSize, std::string& szErrmsg)
{
	if ( NULL==src )
	{
		szErrmsg = "CSimpleZip: Invalid source.";
		return false;
	}

	bool bRet = false;
	CZipMemFile mfIn;
	CZipMemFile mfOut;
	CZipArchive zip;
	CZipFileHeader fhInfo;
	try
	{
		mfIn.Write(src, srcSize);
		zip.Open(mfIn, CZipArchive::zipOpen);
		if ( !zip.GetFileInfo(fhInfo, 0) )
		{
			szErrmsg = "CSimpleZip: Failed to GetFileInfo";
			zip.Close();
			return false;
		}

		if ( NULL==dst || dstSize<fhInfo.m_uUncomprSize )
		{
			dstSize = fhInfo.m_uUncomprSize;
			szErrmsg = "CSimpleZip: The size of destination buffer is too small.";
			return false;
		}

		mfOut.SetLength( fhInfo.m_uUncomprSize );
		((CZipAbstractFile*)&mfOut)->SeekToBegin(); // may be needed when mfOut was used previously
		if ( !zip.ExtractFile(0, mfOut) )
		{
			szErrmsg =  "CSimpleZip: Failed to ExtractFile";
			zip.Close();
			return false;
		}
		zip.Close();
		BYTE* b = mfOut.Detach();
		//int nLen = (int)mfOut.GetLength();			// this is an error length value 
		memcpy_s(dst, dstSize, b, fhInfo.m_uUncomprSize);
		dstSize = fhInfo.m_uUncomprSize;
		free(b);
		bRet = true;
	}
	catch(CZipException& e)
	{
		if (!zip.IsClosed())
			zip.Close();
		szErrmsg =  e.GetErrorDescription().c_str();
	}
	catch(...)
	{
		if (!zip.IsClosed())
			zip.Close();
		szErrmsg =  "CSimpleZip: Failed to Extract, catch exception.";
	}
	return bRet;
}