Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

LiteZip and LiteUnzip

, 7 Aug 2008
Easy to use, small-footprint DLLs to let your app create zip archives, and extract the contents of them. Useful for C, C++, VB, and other languages. Works for Win32 and Linux
LiteZip.zip
LiteZip
LiteZip.mak
LiteZip.mdp
makefile
UnzipFile
makefile
test.zip
UnzipFile.mak
UnzipFile.mdp
UnzipFile.ncb
UnzipMem
makefile
test.zip
UnzipMem.mak
UnzipMem.mdp
UnzipMem.ncb
UnzipMemRaw
makefile
test.zip
UnzipResource
simple_res.zip
UnzipResource.mak
UnzipResource.mdp
UnzipResource.ncb
ZipDir
makefile
MyDir
MySubDir
ZipDir.mak
ZipDir.mdp
ZipDir.ncb
ZipFile
makefile
test.jpg
ZipFile.mak
ZipFile.mdp
ZipFile.ncb
ZipMemory
makefile
ZipMemory.mak
ZipMemory.mdp
ZipMemory.ncb
ZipMemoryRaw
makefile
ZipMemoryRaw.mak
ZipMemoryRaw.mdp
GZipFile
GZipFile.mak
GZipFile.mdp
GZipFile.ncb
makefile
test.jpg
LiteUnzip
LiteUnzip.def
LITEUNZIP.mak
LITEUNZIP.mdp
LITEUNZIP.ncb
makefile
LiteZip.def
#ifdef WIN32
#include <windows.h>
#include <tchar.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
typedef void * HMODULE;
typedef char TCHAR;
#define _T(a) (a)
#define lstrlen(a) strlen(a)
#endif

// Include LiteZip.h for creating a ZIP archive
#include "../LiteZip.h"





// Where we store the pointers to LiteZip.dll's functions that we call
ZipCreateBufferPtr		*lpZipCreateBuffer;
ZipAddBufferRawPtr		*lpZipAddBufferRaw;
ZipGetMemoryPtr			*lpZipGetMemory;
ZipClosePtr				*lpZipClose;
ZipFormatMessagePtr		*lpZipFormatMessage;

// Here is some data in memory which we'll compress into our zip file
const TCHAR Data[] =	_T("This is a test of LiteZip.dll's raw mode.\r\n\r\nHopefully this worked!\r\n");


#ifdef WIN32
/*********************** show_errmsg() **********************
 * Displays a messagebox for the passed OS error number.
 */

void show_errmsg(void)
{
	TCHAR buffer[160];

	buffer[0] = 0;
	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &buffer[0], 160, 0);
	MessageBox(0, &buffer[0], _T("Error"), MB_OK);
}
#endif



/************************** main() *************************
 * Program entry point.
 */

int main()
{
	HMODULE		zipDll;
	HZIP		hz;
	DWORD		result;

#ifdef WIN32
	// Open the LiteZip.DLL. Note: If LiteZip.dll is not placed in a path that can be found
	// by this app, then LoadLibrary will fail. So, either copy LiteZip.dll to the same
	// directory as this EXE, or to some directory that Windows is set to search.
	if ((zipDll = (HMODULE)LoadLibrary(_T("../LiteZip.dll"))))
	{
		// Get the addresses of 5 functions in LiteZip.dll -- ZipCreateBuffer(), ZipAddBuffer()
		// ZipGetMemory(), ZipClose, and ZipFormatMessage
		lpZipCreateBuffer = (ZipCreateBufferPtr *)GetProcAddress(zipDll, ZIPCREATEBUFFERNAME);
		lpZipAddBufferRaw = (ZipAddBufferRawPtr *)GetProcAddress(zipDll, ZIPADDBUFFERRAWNAME);
		lpZipGetMemory = (ZipGetMemoryPtr *)GetProcAddress(zipDll, ZIPGETMEMORYNAME);
		lpZipFormatMessage = (ZipFormatMessagePtr *)GetProcAddress(zipDll, ZIPFORMATMESSAGENAME);
		lpZipClose = (ZipClosePtr *)GetProcAddress(zipDll, ZIPCLOSENAME);
#else
	if ((zipDll = dlopen("../LiteZip/liblitezip.so", RTLD_LAZY)))
	{
		lpZipCreateBuffer = (ZipCreateBufferPtr *)dlsym(zipDll, ZIPCREATEBUFFERNAME);
		lpZipAddBufferRaw = (ZipAddBufferPtr *)dlsym(zipDll, ZIPADDBUFFERRAWNAME);
		lpZipGetMemory = (ZipGetMemoryPtr *)dlsym(zipDll, ZIPGETMEMORYNAME);
		lpZipFormatMessage = (ZipFormatMessagePtr *)dlsym(zipDll, ZIPFORMATMESSAGENAME);
		lpZipClose = (ZipClosePtr *)dlsym(zipDll, ZIPCLOSENAME);
#endif
		// Create a ZIP archive in a memory buffer. Let LiteZip.dll allocate growable memory
		// from the memory pool. We'll set an upper limit of 100,000 bytes on this growable
		// memory. NOTE: This limit is not applicable to Linux
		if (!(result = lpZipCreateBuffer(&hz, 0, 100000, 0)))
		{
			unsigned char	*buffer;
			DWORD			len;
			HANDLE			base;

			// Add the contents of the Data[] memory buffer to the ZIP
			if ((result = lpZipAddBufferRaw(hz, &Data[0], lstrlen(&Data[0]))))
			{
				lpZipClose(hz);
				goto bad;
			}

			// We're done adding files to our ZIP in memory, so let's get a pointer to that
			// memory, and the final size of it. NOTE: We tell LiteZip.dll to give us the
			// memory, and we'll free it when we're done.
			if ((result = lpZipGetMemory(hz, (void **)&buffer, &len, &base))) goto bad;

			// Let's write out the zip memory-buffer to a disk file named "test.zip"
#ifdef WIN32
			{
			HANDLE	handle;
			DWORD	written;

			handle = CreateFile(_T("test.zip"), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
			if (handle == INVALID_HANDLE_VALUE) show_errmsg();
			else
			{
				if (!WriteFile(handle, buffer, len, &written, 0)) show_errmsg();

				CloseHandle(handle);
			}
			}

			// Free the memory now that we're done with it.
			UnmapViewOfFile(buffer);
			CloseHandle(base);
#else
			{
			int	handle;

			if ((handle = open("test.zip", O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) == -1)
				printf("ERROR: %s\n", strerror(errno));
			else
			{
				if (write(handle, base, len) != len)
					printf("ERROR: %s\n", strerror(errno));
				close(handle);
			}
			}

			// Free the memory now that we're done with it.
			free(base);
#endif
		}
		else
		{
			TCHAR	msg[100];

bad:		lpZipFormatMessage(result, msg, sizeof(msg));
#ifdef WIN32
			MessageBox(0, &msg[0], _T("Error"), MB_OK);
#else
			printf("ERROR: %s\n", &msg[0]);
#endif
		}

		// Free the LiteZip.DLL
#ifdef WIN32
		FreeLibrary(zipDll);
#else
		dlclose(zipDll);
#endif
	}
	else
	{
#ifdef WIN32
		show_errmsg();
#else
		printf("ERROR: %s\n", dlerror());
#endif
	}

	return(0);
}

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, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

About the Author

Jeff Glatt

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 8 Aug 2008
Article Copyright 2006 by Jeff Glatt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid