26

Dec 08

Inflate from Zip to memory in C++

This example demonstrates how to inflate in-memory from a Zip in C++:

    CkZip zip;

...

    // Assume we have a CkZip object and a .zip has been loaded.
    // It may have been loaded from a file by calling OpenZip, or from an
    // in-memory image of a .zip by calling OpenFromMemory.
    // (or any of the other CkZip.Open* methods)
    CkString strXml;
    CkByteData gifData;

    // Find the file within the zip containing the data we want to access...
    CkZipEntry *entry = zip.FirstMatchingEntry("*hamlet.xml");
    if (entry)
	{
	// Found it.  This is a text file that may be inflated to a null-terminated
	// string.  
	const char *xmlText = entry->inflateToString2();

	// Copy the text to a safe place.  Once the CkZipEntry object is deleted,
	// it is no longer a valid pointer:
	strXml.append(xmlText);

	delete entry;
	}

    // Get the data for a binary file:
    entry = zip.FirstMatchingEntry("*dude.gif");
    if (entry)
	{
	entry->Inflate(gifData);
	delete entry;

	// The binary bytes may be accessed like this:
	const unsigned char *gifBytes = gifData.getData();
	unsigned long numGifBytes = gifData.getSize();
	}