Compressing / Uncompressing a hunk of text

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Compressing / Uncompressing a hunk of text

Post by Jorg »

(by BlizzyMadden)

Here are some simple functions to compress and uncompress a hunk of text. Hope this helps:

Code: Select all

#ifndef __WXCOMPRESSION_H__
#define __WXCOMPRESSION_H__

#include <wx/wx.h>
#include <wx/string.h>
#include <cstring>

/*returns a text buffer containing the compressed version of the text passed in.
out_numBytes is the size of the returning compressed text buffer.
Note that you take ownership of the returning buffer and need to delete it yourself*/
static char* wxCompressText(const wxString& textToCompress, size_t& out_numBytes)
	{
	wxMemoryOutputStream memstream;
	wxZlibOutputStream* zoutput = new wxZlibOutputStream(memstream);

	// Write the string to the zlib stream
	size_t len = std::strlen(textToCompress.mb_str(wxConvUTF8) );
	zoutput->Write(textToCompress.mb_str(wxConvUTF8), len);

	// Make sure we were successful
	size_t numWritten = zoutput->LastWrite();
	if (numWritten != len)
		{
		return NULL;
		}

	// IMPORTANT: Force flush and zip finalization to our memory buffer stream
	delete zoutput;

	// Copy to out_buffer
	size_t streamSize = memstream.GetSize();
	char* out_buffer = new char[streamSize];
	size_t numCopied = memstream.CopyTo(out_buffer, streamSize);

	if (numCopied != streamSize)
		{
		return NULL;
		}
	else
		{
		out_numBytes = numCopied;
		}

	return out_buffer;
	}

/*returns a text buffer containing the compressed version of the text passed in.
out_numBytes is the size of the returning compressed text buffer.
Note that you take ownership of the returning buffer and need to delete it yourself*/
static char* wxCompressText(const char* text, size_t& out_numBytes)
	{
	wxMemoryOutputStream memstream;
	wxZlibOutputStream* zoutput = new wxZlibOutputStream(memstream);

	// Write the string to the zlib stream
	size_t len = std::strlen(text);
	zoutput->Write(text, len);

	// Make sure we were successful
	size_t numWritten = zoutput->LastWrite();
	if (numWritten != len)
		{
		return NULL;
		}

	// IMPORTANT: Force flush and zip finalization to our memory buffer stream
	delete zoutput;

	// Copy to out_buffer
	size_t streamSize = memstream.GetSize();
	char* out_buffer = new char[streamSize];
	size_t numCopied = memstream.CopyTo(out_buffer, streamSize);

	if (numCopied != streamSize)
		{
		return NULL;
		}
	else
		{
		out_numBytes = numCopied;
		}

	return out_buffer;
	}

/*returns a text buffer containing the uncompressed version of the text passed in.
out_numBytes is the size of the returning uncompressed text buffer.
Note that you take ownership of the returning buffer and need to delete it yourself*/
static char* wxUncompressText(const char* Text, size_t size, size_t& out_numBytes)
	{
	wxMemoryInputStream inputStream(Text, size);
	wxMemoryOutputStream memstream;
	wxZlibInputStream* zoutput = new wxZlibInputStream(inputStream);

	// read the string into the zlib stream
	zoutput->Read(memstream);

	// IMPORTANT: Force flush and zip finalization to our memory buffer stream
	delete zoutput;

	// Copy to out_buffer
	size_t streamSize = memstream.GetSize();
	char* out_buffer = new char[streamSize+1];
	std::memset(out_buffer, 0, streamSize+1);
	size_t numCopied = memstream.CopyTo(out_buffer, streamSize);

	if (numCopied != streamSize)
		{
		return NULL;
		}
	else
		{
		out_numBytes = numCopied;
		}

	return out_buffer;
	}

#endif //__WXCOMPRESSION_H__
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Post Reply