home *** CD-ROM | disk | FTP | other *** search
- /*
- * Examples.cpp
- * Copyright (c) 1998-1999, Xceed Software Inc.
- *
- * Description:
- * Sample application using the Xceed Zip 4 DLL API interface.
- * This file contains the main function showing how to use the
- * Xceed Zip's DLL API interface instead of the ActiveX.
- *
- * There are many ways to access the exported functions from the
- * XceedZip.dll file.
- *
- * a) Through a .lib file. We distribute the XceedZip.lib file,
- * which can be used in Visual C++ 6. Older versions of VC++
- * don't seem to recognize this file. You could also generate
- * a .lib file manually, for example using TDUMP in Borland C++.
- *
- * b) Using a GetProcAddress call for each function you wish to
- * use. The XceedZipAPi.h file contains predefined typedefs
- * that will help you in this task.
- *
- * c) A DLL can not only export functions, but also global
- * variables. The XceedZip.dll file exports a very useful
- * variable called g_xzFunctions, which is a pointer to an
- * XceedZipFunctions structure (see XceedZipAPI.h) already
- * containing a pointer to EACH EXPORTED FUNCTION.
- *
- * This sample application uses methods (b) and (c), to avoid
- * dependency to Visual C++ 6.
- *
- * Take a look at WinMain for the required initialization.
- */
-
-
- #include "stdafx.h"
- #include <commctrl.h>
- #include "Examples.h"
-
- // License string found in the LICENSE.TXT file
- #define XCD_LICENSE_A ""
- #define XCD_LICENSE_W L""
-
- // This sample works on static filenames. Change these to accomodate
- // your machine
- #define XCD_ZIPFILENAME_A "C:\\Temp\\DllApi.zip"
- #define XCD_ZIPFILENAME_W L"C:\\Temp\\DllApi.zip"
-
- #define XCD_FILESTOPROCESS_A "C:\\*"
- #define XCD_FILESTOPROCESS_W L"C:\\*"
-
- #define XCD_UNZIPTOFOLDER_A "C:\\Temp"
- #define XCD_UNZIPTOFOLDER_W L"C:\\Temp"
-
- // These other defines are just usefull to demonstrate Ansi/Unicode support
- #define XCD_LICENSE _T(XCD_LICENSE_A)
- #define XCD_ZIPFILENAME _T(XCD_ZIPFILENAME_A)
- #define XCD_FILESTOPROCESS _T(XCD_FILESTOPROCESS_A)
- #define XCD_UNZIPTOFOLDER _T(XCD_UNZIPTOFOLDER_A)
-
- // Use the global handle to XceedZip.dll
- extern HMODULE hXceedZipDll;
-
-
- // Zipping example
- // ---------------
- // Uses method (c) described above to call Xceed Zip APIs.
- // For events, it shows how to use the window message technique.
-
- void DoZipExample( HWND hParent, HWND hList )
- {
- // The GetProcAddress can do much more than get function pointers.
- // It would be better to call it GetExportedSymbolAddress. q;-)
- XceedZipFunctions * pFuncs = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
-
- if( pFuncs )
- {
- // Create an instance for this operation. Throughout this sample, we create
- // instances on demand. We could also create the required instance(s) at the
- // beginning, and free them at the end.
- HXCEEDZIP hZip = pFuncs->lpfnXzCreateXceedZipA( XCD_LICENSE );
-
- if ( hZip )
- {
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Zipping example -----" );
-
- // Tell this instance that we want to receive the WM_USER_XCEEDZIPEVENT
- // message (see XceedZipAPI.h) when an event is triggered.
- pFuncs->lpfnXzSetXceedZipWindow( hZip, hParent );
-
- // The rest of the code is identical to using the ActiveX. You set
- // properties, and then call a method. Here, we only need to set the
- // zip filename we want to create or update, set the files we want to
- // zip, and then launch the zipping by calling the XzZip function
- // (The Zip method).
- pFuncs->lpfnXzSetZipFilenameA( hZip, XCD_ZIPFILENAME );
- pFuncs->lpfnXzSetFilesToProcessA( hZip, XCD_FILESTOPROCESS );
-
- int nErr = pFuncs->lpfnXzZip( hZip );
-
- // You can also use the GetErrorDescription method through the
- // XzGetErrorDescription function, to get a default error message.
- TCHAR szMsg[ 200 ];
- pFuncs->lpfnXzGetErrorDescriptionA( hZip, xvtError, nErr, szMsg, 200 );
-
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- // For each XzCreateXceedZip, there must be a XzDestroyXceedZip call
- pFuncs->lpfnXzDestroyXceedZip( hZip );
- }
- }
- }
-
- // Unzipping example
- // -----------------
- // Here, we'll use UNICODE functions only, and use a callback function
- // for event notifications. This way, we will not mix up Ansi and Unicode
- // support in event handling.
-
- void CALLBACK MyCallback( WPARAM wXceedMessage, LPARAM lParam )
- {
- static HWND hList = NULL;
- static XceedZipFunctions* pFuncs = NULL;
-
- switch( wXceedMessage )
- {
- case 0xffff:
- // It's a trick to set hList!
- hList = ( HWND ) lParam;
- break;
-
- case 0xfffe:
- // And another trick to set pFuncs!
- pFuncs = ( XceedZipFunctions* ) lParam;
- break;
-
- // A file is evaluated for unzipping
- case XM_UNZIPPREPROCESSINGFILE:
- {
- // For this particular event, we decided to create our handle using
- // the wide version of XzCreateXceedZip (see DoUnzipExample)
- // So we typecast the lParam to the wide version of the structure
- xcdUnzipPreprocessingFileParamsW* pParams = ( xcdUnzipPreprocessingFileParamsW * ) lParam;
- // Here, you could change some information about this file, or reject
- // the file based on your own custom filtering conditions.
-
- // For this example, we are going to append a ".Unzipped" to all files!
- wcscat( pParams->szDestFilename, L".Unzipped" );
-
- break;
- }
-
- // A file is skipped for some reason
- case XM_SKIPPINGFILE:
- {
- xcdSkippingFileParamsW* pParams = ( xcdSkippingFileParamsW * ) lParam;
-
- // Display the skipped filename. We make sure to use Ansi strings to
- // work on Win95/98
- char szMsg[ 300 ];
- wsprintf( szMsg, "Skipping file %S", pParams->szFilename );
- SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- // Get and display the reason. You can see that each structure contains
- // the handle to the instance that triggered the event (hZip).
- szMsg[ 0 ] = ' ';
- // Even though we created this handle with the wide version, we can
- // call Ansi versions of other API's.
- if( pFuncs )
- {
- // As explained in "DoUnzipExample" below, we can call ansi functions
- // on a "wide-created" instance.
- pFuncs->lpfnXzGetErrorDescriptionA( pParams->hZip, xvtSkippingReason, pParams->xReason, szMsg+1, 299 );
- SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
- }
-
- break;
- }
-
- // The FileStatus event is the triggered event along the actual
- // processing. That's why it's the best place for status messages.
- case XM_FILESTATUS:
- {
- xcdFileStatusParamsW* pParams = ( xcdFileStatusParamsW * ) lParam;
-
- // When starting to process a file, we display it's name
- if ( pParams->lBytesProcessed == 0 && pFuncs )
- {
- char szMsg[ 300 ] = { 0 };
-
- // Starting to process this file
- switch( pFuncs->lpfnXzGetCurrentOperation( pParams->hZip ) )
- {
- case xcoUnzipping:
- {
- wsprintfA( szMsg, "Unzipping %S", pParams->szFilename );
- break;
- }
- }
-
- if ( szMsg[ 0 ] )
- {
- SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
- }
- }
-
- // We could insert here code to update a file status progress bar.
-
- break;
- }
- // The GlobalStatus event gives the global progression
- case XM_GLOBALSTATUS:
- {
- xcdGlobalStatusParams* pParam = ( xcdGlobalStatusParams * ) lParam;
- break;
- }
- // A recoverable inconsistency was encountered
- case XM_WARNING:
- {
- xcdWarningParamsW* pParams = ( xcdWarningParamsW * ) lParam;
-
- // Display the affected file and the message. We can call the Ansi
- // function, even if the handle was created with the Wide creator.
- char szWarning[ 200 ] = { 0 };
-
- if( pFuncs )
- {
- pFuncs->lpfnXzGetErrorDescriptionA( pParams->hZip, xvtWarning, pParams->xWarning, szWarning, 200 );
- }
-
- char szMsg[ 200 ];
- wsprintfA( szMsg, "Warning: %s", szWarning );
- SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- szMsg[ 0 ] = ' ';
- wsprintfA( szMsg+1, "Filename: %S", pParams->szFilename );
- SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- break;
- }
- // A file is about to be replaced
- case XM_REPLACINGFILE:
- {
- xcdReplacingFileParamsW* pParams = ( xcdReplacingFileParamsW * ) lParam;
-
- // Let's confirm with the user
- char szMsg[ 1024 ];
- wsprintfA( szMsg, "Replace file %S\nLast modified %02d/%02d/%04d at %02dh%02d\n\n"
- "with file %S\nLast modified %02d/%02d/%04d at %02dh%02d ?",
- pParams->szOrigFilename, pParams->stOrigLastModified.wDay,
- pParams->stOrigLastModified.wMonth, pParams->stOrigLastModified.wYear,
- pParams->stOrigLastModified.wHour, pParams->stOrigLastModified.wMinute,
- pParams->szFilename, pParams->stLastModified.wDay,
- pParams->stLastModified.wMonth, pParams->stLastModified.wYear,
- pParams->stLastModified.wHour, pParams->stLastModified.wMinute );
-
- if ( MessageBoxA( NULL, szMsg, "Replacing file", MB_ICONQUESTION | MB_YESNO ) == IDNO )
- {
- pParams->bReplaceFile = FALSE;
- }
- break;
- }
- }
- }
-
- void DoUnzipExample( HWND hList )
- {
- // Get a pointer to the functions structure
- XceedZipFunctions * pFuncs = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
-
- if( pFuncs )
- {
- // We send dummy messages to our callback to set it's static variables!
- MyCallback( 0xffff, ( LPARAM ) hList );
- MyCallback( 0xfffe, ( LPARAM ) pFuncs );
-
- // Create an instance for this operation. Why not work in UNICODE?
- // The MyCallback function will have to expect UNICODE strings it its params.
- HXCEEDZIP hZip = pFuncs->lpfnXzCreateXceedZipW( XCD_LICENSE_W );
-
- if ( hZip )
- {
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Unzipping example -----" );
-
- pFuncs->lpfnXzSetXceedZipCallback( hZip, MyCallback );
-
- pFuncs->lpfnXzSetZipFilenameW( hZip, XCD_ZIPFILENAME_W ); // Wide version
- pFuncs->lpfnXzSetUnzipToFolderW( hZip, XCD_UNZIPTOFOLDER_W ); // Wide version
-
- int nErr = pFuncs->lpfnXzUnzip( hZip );
-
- // Windows 95 does not implement UNICODE APIs, so we must use SendMessageA
- // (SendMessage is mapped to SendMessageA when UNICODE isn't define)
- // Why not call XzGetErrorDescriptionA. It's not because we created the
- // instance with the wide version that we are stuck calling wide functions.
- // The only impact of creating the instance with the wide or ansi version
- // is the format of the string parameters in events.
- char szMsg[ 200 ];
- pFuncs->lpfnXzGetErrorDescriptionA( hZip, xvtError, nErr, szMsg, 200 );
- SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- pFuncs->lpfnXzDestroyXceedZip( hZip );
- }
-
- // Clean-up our callback's static variables.
- MyCallback( 0xffff, 0 );
- MyCallback( 0xfffe, 0 );
- }
- }
-
-
- // Previewing example
- // ------------------
- // Back to a window message handling. Here, we show the BackgroundProcessing
- // method. The method call returns right away with a special code.
-
- void DoPreviewExample( HWND hParent, HWND hList, HXCEEDZIP* phZip )
- {
- // Get a pointer to the functions structure
- XceedZipFunctions * pFuncs = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
-
- if( pFuncs )
- {
- // Create an instance
- *phZip = pFuncs->lpfnXzCreateXceedZipA( XCD_LICENSE );
-
- if ( *phZip )
- {
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Previewing example -----" );
-
- pFuncs->lpfnXzSetXceedZipWindow( *phZip, hParent );
- pFuncs->lpfnXzSetFilesToProcessA( *phZip, XCD_FILESTOPROCESS );
- pFuncs->lpfnXzSetProcessSubfolders( *phZip, TRUE );
- pFuncs->lpfnXzSetBackgroundProcessing( *phZip, TRUE );
-
- int nErr = pFuncs->lpfnXzPreviewFiles( *phZip, FALSE );
-
- TCHAR szMsg[ 200 ];
- pFuncs->lpfnXzGetErrorDescriptionA( *phZip, xvtError, nErr, szMsg, 200 );
-
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- if( nErr != 1 /* xerProcessStarted */ )
- {
- // The window won't get the XM_PROCESSCOMPLETED event, so clean up
- pFuncs->lpfnXzDestroyXceedZip( *phZip );
- *phZip = NULL;
- }
- // else, it's the ProcessCompleted event handler that will clean up!
- }
- }
- }
-
-
- // Listing example
- // ---------------
- // Here, we show how to use method (b). The XceedZipAPI.h file already defines
- // all function pointer types.
-
- void DoListingExample( HWND hParent, HWND hList )
- {
- LPFNXZCREATEXCEEDZIPA lpfnCreate =
- ( LPFNXZCREATEXCEEDZIPA ) GetProcAddress( hXceedZipDll, "XzCreateXceedZipA" );
-
- LPFNXZSETXCEEDZIPWINDOW lpfnSetWindow =
- ( LPFNXZSETXCEEDZIPWINDOW ) GetProcAddress( hXceedZipDll, "XzSetXceedZipWindow" );
-
- LPFNXZSETZIPFILENAMEA lpfnSetZipFilename =
- ( LPFNXZSETZIPFILENAMEA ) GetProcAddress( hXceedZipDll, "XzSetZipFilenameA" );
-
- LPFNXZLISTZIPCONTENTS lpfnListZipContents =
- ( LPFNXZLISTZIPCONTENTS ) GetProcAddress( hXceedZipDll, "XzListZipContents" );
-
- LPFNXZGETERRORDESCRIPTIONA lpfnGetErrorDesc =
- ( LPFNXZGETERRORDESCRIPTIONA ) GetProcAddress( hXceedZipDll, "XzGetErrorDescriptionA" );
-
- LPFNXZDESTROYXCEEDZIP lpfnDestroy =
- ( LPFNXZDESTROYXCEEDZIP ) GetProcAddress( hXceedZipDll, "XzDestroyXceedZip" );
-
-
- if ( lpfnCreate && lpfnSetWindow && lpfnSetZipFilename &&
- lpfnListZipContents && lpfnGetErrorDesc && lpfnDestroy )
- {
- HXCEEDZIP hZip = lpfnCreate( XCD_LICENSE_A );
-
- if ( hZip )
- {
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Listing example -----" );
-
- lpfnSetWindow( hZip, hParent );
- lpfnSetZipFilename( hZip, XCD_ZIPFILENAME_A );
-
- int nErr = lpfnListZipContents( hZip );
-
- TCHAR szMsg[ 200 ];
- lpfnGetErrorDesc( hZip, xvtError, nErr, szMsg, 200 );
-
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- lpfnDestroy( hZip );
- }
- }
- }
-
- // Testing example
- // ---------------
- // Back to the (c) method. Are you still wondering why? q;-)
-
- void DoTestingExample( HWND hParent, HWND hList )
- {
- // Get the structure
- XceedZipFunctions * pFuncs = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
-
- if ( pFuncs )
- {
- HXCEEDZIP hZip = pFuncs->lpfnXzCreateXceedZipA( XCD_LICENSE_A );
-
- if ( hZip )
- {
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Testing example -----" );
-
- pFuncs->lpfnXzSetXceedZipWindow( hZip, hParent );
- pFuncs->lpfnXzSetZipFilenameA( hZip, XCD_ZIPFILENAME_A );
-
- int nErr = pFuncs->lpfnXzTestZipFile( hZip, TRUE );
-
- TCHAR szMsg[ 200 ];
- pFuncs->lpfnXzGetErrorDescriptionA( hZip, xvtError, nErr, szMsg, 200 );
-
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- pFuncs->lpfnXzDestroyXceedZip( hZip );
- }
- }
- }
-
- // Getting zip contents example
- // ----------------------------
- // Again, using method (c) is the easiest. In this example, we show
- // you how to retrieve a zip file's contents without having to handle
- // messages or callbacks.
-
- void DoGetContentsExample( HWND hParent, HWND hList )
- {
- // Get the structure
- XceedZipFunctions * pFuncs = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
-
- if ( pFuncs )
- {
- HXCEEDZIP hZip = pFuncs->lpfnXzCreateXceedZipA( XCD_LICENSE_A );
-
- if ( hZip )
- {
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Get zip contents example -----" );
-
- pFuncs->lpfnXzSetZipFilenameA( hZip, XCD_ZIPFILENAME_A );
-
- HXCEEDZIPITEMS hItems;
- int nErr = pFuncs->lpfnXzGetZipContents( hZip, &hItems );
-
- char szMsg[ MAX_PATH + 100 ];
- pFuncs->lpfnXzGetErrorDescriptionA( hZip, xvtError, nErr, szMsg, 200 );
-
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- if( nErr == xerSuccess )
- {
- // The hItems handle enables us to wlak throught each item in the zip file
- xcdListingFileParamsA xItemInfo;
-
- BOOL bAvailable = pFuncs->lpfnXziGetFirstItemA( hItems, &xItemInfo );
-
- while( bAvailable )
- {
- wsprintf( szMsg, "%s [%ukb -> %ukb] %02d/%02d/%04d",
- xItemInfo.szFilename,
- ( xItemInfo.lSize+1023 ) / 1024,
- ( xItemInfo.lCompressedSize+1023) / 1024,
- xItemInfo.stLastModified.wDay,
- xItemInfo.stLastModified.wMonth,
- xItemInfo.stLastModified.wYear );
- SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
-
- bAvailable = pFuncs->lpfnXziGetNextItemA( hItems, &xItemInfo );
- }
-
- // Important: We must always destroy the items handle!
- pFuncs->lpfnXziDestroyXceedZipItems( hItems );
- }
-
- pFuncs->lpfnXzDestroyXceedZip( hZip );
- }
- }
- }
-
-
- //
- // END_OF_FILE
- //
-