Click here to Skip to main content
15,886,873 members
Articles / Desktop Programming / MFC

Self-Extracting File Framework

Rate me:
Please Sign up or sign in to vote.
4.55/5 (5 votes)
7 May 2001 111.4K   1.6K   37  
An article about creating Self-Extracting files with integrated compression
// StreamDecompress.h: interface for the CStreamDecompress class.
//
//////////////////////////////////////////////////////////////////////
// Self Extracting File Framework
// ==============================
//
// Copyright � 2000 Rui Godinho Lopes <ruiglopes@yahoo.com>
// All rights reserved.
//
// This source file(s) may be redistributed unmodified by any means
// PROVIDING they are not sold for profit without the authors expressed
// written consent, and providing that this notice and the authors name
// and all copyright notices remain intact.
//
// Any use of the software in source or binary forms, with or without
// modification, must include, in the user documentation ("About" box and
// printed documentation) and internal comments to the code, notices to
// the end user as follows:
//
// "Portions Copyright � 2000 Rui Godinho Lopes"
//
// An email letting me know that you are using it would be nice as well.
// That's not much to ask considering the amount of work that went into
// this.
//
// THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
// EXPRESS OR IMPLIED. USE IT AT YOUT OWN RISK. THE AUTHOR ACCEPTS NO
// LIABILITY FOR ANY DATA DAMAGE/LOSS THAT THIS PRODUCT MAY CAUSE.
//
// =======================================================================
// REVISION HISTORY
// =======================================================================
// 1.00 14July2000
//   first public version
//
//////////////////////////////////////////////////////////////////////////

#if !defined(AFX_STREAMDECOMPRESS_H__293746A6_3F9E_11D4_AAF7_000000000000__INCLUDED_)
#define AFX_STREAMDECOMPRESS_H__293746A6_3F9E_11D4_AAF7_000000000000__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

struct COMPRESSIONINFO
{
	DWORD dwChecksum;				//data checksum
	DWORD dwSize;						//size of uncompressed data
	DWORD dwCompressedSize;	//size of compressed data
};

class CStreamVoidStatus
{
public:
	void OnStatus(ULONG /*ulBytesDone*/) {}
};

template <typename TReader, typename TWriter, typename TStatus= CStreamVoidStatus>
class CStreamDecompress  
{
public:
	CStreamDecompress()
		{
			m_pInBuffer= m_pOutBuffer= NULL;

			m_z.zalloc= NULL;
			m_z.zfree= NULL;
			m_z.opaque= NULL;
		}

	
	virtual ~CStreamDecompress()
		{
			::inflateEnd(&m_z);
			delete [] m_pInBuffer;
			delete [] m_pOutBuffer;
		}
	

	LRESULT Init(int iBufferSize= 4096)
		{
			int iResult= ::inflateInit(&m_z);
			if (iResult!=Z_OK)
				return FALSE;

			m_iBufferSize= iBufferSize;
			m_pInBuffer= new Byte[iBufferSize];
			m_pOutBuffer= new Byte[iBufferSize];

			m_z.avail_in= 0;
			m_z.next_out= m_pOutBuffer;
			m_z.avail_out= iBufferSize;

			return m_pOutBuffer!=NULL;
		}


	LRESULT Decompress()
		{
			if (!m_pOutBuffer)
				return S_FALSE;

			int iStatus;
			uLong ulAdler= 1;
			ULONG ul;
			LRESULT lRes;

			do
			{
				if (!m_z.avail_in)
				{
					if ((lRes= m_Reader.Read(m_pInBuffer, m_iBufferSize, &ul))!=S_OK)
						return S_FALSE;
					m_z.avail_in= ul;
					m_z.next_in= m_pInBuffer;
				}

				iStatus= ::inflate(&m_z, Z_SYNC_FLUSH);

				int iCount= m_iBufferSize - m_z.avail_out;
				if (m_Writer.Write(m_pOutBuffer, iCount, &ul)!=S_OK)
					return S_FALSE;

				//For some reason the m_z.adler is allways 1 after all data
				//has been expanded, so we have to calculate the Adler-32 ourself
				ulAdler= adler32(ulAdler, m_pOutBuffer, iCount);

				m_Status.OnStatus(m_z.total_in);

				m_z.next_out= m_pOutBuffer;
				m_z.avail_out= m_iBufferSize;
			} while (iStatus==Z_OK);

			m_z.adler= ulAdler;	//override the allways one adler with our computed adler32

			return iStatus==Z_STREAM_END? S_OK : S_FALSE;
		}

	BOOL GetInfo(COMPRESSIONINFO &Info) const
		{
			Info.dwChecksum= m_z.adler;
			Info.dwSize= m_z.total_out;
			Info.dwCompressedSize= m_z.total_in;
			return TRUE;
		}

public:
	TReader m_Reader;
	TWriter m_Writer;
	TStatus m_Status;

private:
	z_stream m_z;
	Byte *m_pInBuffer;
	Byte *m_pOutBuffer;
	int m_iBufferSize;
};

#endif // !defined(AFX_STREAMDECOMPRESS_H__293746A6_3F9E_11D4_AAF7_000000000000__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions