home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedZip.Cab / F112452_Examples.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-13  |  17.3 KB  |  499 lines

  1. /*
  2.  * Examples.cpp
  3.  * Copyright (c) 1998-1999, Xceed Software Inc.
  4.  *
  5.  * Description:
  6.  *    Sample application using the Xceed Zip 4 DLL API interface.
  7.  *    This file contains the main function showing how to use the
  8.  *    Xceed Zip's DLL API interface instead of the ActiveX.
  9.  *
  10.  *    There are many ways to access the exported functions from the
  11.  *    XceedZip.dll file.
  12.  *
  13.  *    a) Through a .lib file. We distribute the XceedZip.lib file,
  14.  *       which can be used in Visual C++ 6. Older versions of VC++
  15.  *       don't seem to recognize this file. You could also generate
  16.  *       a .lib file manually, for example using TDUMP in Borland C++.
  17.  *
  18.  *    b) Using a GetProcAddress call for each function you wish to
  19.  *       use. The XceedZipAPi.h file contains predefined typedefs
  20.  *       that will help you in this task.
  21.  *
  22.  *    c) A DLL can not only export functions, but also global 
  23.  *       variables. The XceedZip.dll file exports a very useful 
  24.  *       variable called g_xzFunctions, which is a pointer to an
  25.  *       XceedZipFunctions structure (see XceedZipAPI.h) already 
  26.  *       containing a pointer to EACH EXPORTED FUNCTION.
  27.  *
  28.  *    This sample application uses methods (b) and (c), to avoid
  29.  *    dependency to Visual C++ 6.
  30.  *
  31.  *    Take a look at WinMain for the required initialization.
  32.  */
  33.  
  34.  
  35. #include "stdafx.h"
  36. #include <commctrl.h>
  37. #include "Examples.h"
  38.  
  39. // License string found in the LICENSE.TXT file
  40. #define XCD_LICENSE_A    ""
  41. #define XCD_LICENSE_W   L""
  42.  
  43. // This sample works on static filenames. Change these to accomodate
  44. // your machine
  45. #define XCD_ZIPFILENAME_A      "C:\\Temp\\DllApi.zip"
  46. #define XCD_ZIPFILENAME_W     L"C:\\Temp\\DllApi.zip"
  47.  
  48. #define XCD_FILESTOPROCESS_A   "C:\\*"
  49. #define XCD_FILESTOPROCESS_W  L"C:\\*"
  50.  
  51. #define XCD_UNZIPTOFOLDER_A    "C:\\Temp"
  52. #define XCD_UNZIPTOFOLDER_W   L"C:\\Temp"
  53.  
  54. // These other defines are just usefull to demonstrate Ansi/Unicode support
  55. #define XCD_LICENSE           _T(XCD_LICENSE_A)
  56. #define XCD_ZIPFILENAME       _T(XCD_ZIPFILENAME_A)
  57. #define XCD_FILESTOPROCESS    _T(XCD_FILESTOPROCESS_A)
  58. #define XCD_UNZIPTOFOLDER     _T(XCD_UNZIPTOFOLDER_A)
  59.  
  60. // Use the global handle to XceedZip.dll
  61. extern HMODULE hXceedZipDll;
  62.  
  63.  
  64. // Zipping example
  65. // ---------------
  66. // Uses method (c) described above to call Xceed Zip APIs.
  67. // For events, it shows how to use the window message technique.
  68.  
  69. void DoZipExample( HWND hParent, HWND hList )
  70. {
  71.   // The GetProcAddress can do much more than get function pointers.
  72.   // It would be better to call it GetExportedSymbolAddress. q;-)
  73.   XceedZipFunctions * pFuncs  = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
  74.  
  75.   if( pFuncs )
  76.   {
  77.     // Create an instance for this operation. Throughout this sample, we create
  78.     // instances on demand. We could also create the required instance(s) at the
  79.     // beginning, and free them at the end.
  80.     HXCEEDZIP hZip  = pFuncs->lpfnXzCreateXceedZipA( XCD_LICENSE );
  81.  
  82.     if ( hZip )
  83.     {
  84.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Zipping example -----" );
  85.  
  86.       // Tell this instance that we want to receive the WM_USER_XCEEDZIPEVENT
  87.       // message (see XceedZipAPI.h) when an event is triggered.
  88.       pFuncs->lpfnXzSetXceedZipWindow( hZip, hParent );
  89.  
  90.       // The rest of the code is identical to using the ActiveX. You set 
  91.       // properties, and then call a method. Here, we only need to set the 
  92.       // zip filename we want to create or update, set the files we want to
  93.       // zip, and then launch the zipping by calling the XzZip function
  94.       // (The Zip method).
  95.       pFuncs->lpfnXzSetZipFilenameA( hZip, XCD_ZIPFILENAME );
  96.       pFuncs->lpfnXzSetFilesToProcessA( hZip, XCD_FILESTOPROCESS );
  97.  
  98.       int nErr  = pFuncs->lpfnXzZip( hZip );
  99.   
  100.       // You can also use the GetErrorDescription method through the
  101.       // XzGetErrorDescription function, to get a default error message.
  102.       TCHAR szMsg[ 200 ];
  103.       pFuncs->lpfnXzGetErrorDescriptionA( hZip, xvtError, nErr, szMsg, 200 );
  104.  
  105.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  106.  
  107.       // For each XzCreateXceedZip, there must be a XzDestroyXceedZip call
  108.       pFuncs->lpfnXzDestroyXceedZip( hZip );
  109.     }
  110.   }
  111. }
  112.  
  113. // Unzipping example
  114. // -----------------
  115. // Here, we'll use UNICODE functions only, and use a callback function
  116. // for event notifications. This way, we will not mix up Ansi and Unicode
  117. // support in event handling.
  118.  
  119. void CALLBACK MyCallback( WPARAM wXceedMessage, LPARAM lParam )
  120. {
  121.   static HWND               hList   = NULL;
  122.   static XceedZipFunctions* pFuncs  = NULL;
  123.  
  124.   switch( wXceedMessage )
  125.   {
  126.     case 0xffff:
  127.       // It's a trick to set hList!
  128.       hList = ( HWND ) lParam;
  129.       break;
  130.  
  131.     case 0xfffe:
  132.       // And another trick to set pFuncs!
  133.       pFuncs  = ( XceedZipFunctions* ) lParam;
  134.       break;
  135.  
  136.     // A file is evaluated for unzipping
  137.     case XM_UNZIPPREPROCESSINGFILE:
  138.     {
  139.       // For this particular event, we decided to create our handle using 
  140.       // the wide version of XzCreateXceedZip (see DoUnzipExample)
  141.       // So we typecast the lParam to the wide version of the structure
  142.       xcdUnzipPreprocessingFileParamsW* pParams = ( xcdUnzipPreprocessingFileParamsW * ) lParam;
  143.       // Here, you could change some information about this file, or reject
  144.       // the file based on your own custom filtering conditions.
  145.  
  146.       // For this example, we are going to append a ".Unzipped" to all files!
  147.       wcscat( pParams->szDestFilename, L".Unzipped" );
  148.  
  149.       break;
  150.     }
  151.  
  152.     // A file is skipped for some reason
  153.     case XM_SKIPPINGFILE:
  154.     {
  155.       xcdSkippingFileParamsW*  pParams = ( xcdSkippingFileParamsW * ) lParam;
  156.  
  157.       // Display the skipped filename. We make sure to use Ansi strings to
  158.       // work on Win95/98
  159.       char  szMsg[ 300 ];
  160.       wsprintf( szMsg, "Skipping file %S", pParams->szFilename );
  161.       SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  162.  
  163.       // Get and display the reason. You can see that each structure contains
  164.       // the handle to the instance that triggered the event (hZip).
  165.       szMsg[ 0 ]  = ' ';
  166.       // Even though we created this handle with the wide version, we can
  167.       // call Ansi versions of other API's.
  168.       if( pFuncs )
  169.       {
  170.         // As explained in "DoUnzipExample" below, we can call ansi functions
  171.         // on a "wide-created" instance.
  172.         pFuncs->lpfnXzGetErrorDescriptionA( pParams->hZip, xvtSkippingReason, pParams->xReason, szMsg+1, 299 );
  173.         SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  174.       }
  175.  
  176.       break;
  177.     }
  178.  
  179.     // The FileStatus event is the triggered event along the actual
  180.     // processing. That's why it's the best place for status messages.
  181.     case XM_FILESTATUS:
  182.     {
  183.       xcdFileStatusParamsW*  pParams = ( xcdFileStatusParamsW * ) lParam;
  184.  
  185.       // When starting to process a file, we display it's name
  186.       if ( pParams->lBytesProcessed == 0 && pFuncs )
  187.       {
  188.         char  szMsg[ 300 ] = { 0 };
  189.  
  190.         // Starting to process this file
  191.         switch( pFuncs->lpfnXzGetCurrentOperation( pParams->hZip ) )
  192.         {
  193.           case xcoUnzipping:
  194.           {
  195.             wsprintfA( szMsg, "Unzipping %S", pParams->szFilename );
  196.             break;
  197.           }
  198.         }
  199.  
  200.         if ( szMsg[ 0 ] )
  201.         {
  202.           SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  203.         }
  204.       }
  205.  
  206.       // We could insert here code to update a file status progress bar.
  207.  
  208.       break;
  209.     }
  210.     // The GlobalStatus event gives the global progression
  211.     case XM_GLOBALSTATUS:
  212.     {
  213.       xcdGlobalStatusParams*  pParam  = ( xcdGlobalStatusParams * ) lParam;
  214.       break;
  215.     }
  216.     // A recoverable inconsistency was encountered
  217.     case XM_WARNING:
  218.     {
  219.       xcdWarningParamsW* pParams = ( xcdWarningParamsW * ) lParam;
  220.  
  221.       // Display the affected file and the message. We can call the Ansi
  222.       // function, even if the handle was created with the Wide creator.
  223.       char  szWarning[ 200 ]  = { 0 };
  224.  
  225.       if( pFuncs )
  226.       {
  227.         pFuncs->lpfnXzGetErrorDescriptionA( pParams->hZip, xvtWarning, pParams->xWarning, szWarning, 200 );
  228.       }
  229.  
  230.       char  szMsg[ 200 ];
  231.       wsprintfA( szMsg, "Warning: %s", szWarning );
  232.       SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  233.  
  234.       szMsg[ 0 ]  = ' ';
  235.       wsprintfA( szMsg+1, "Filename: %S", pParams->szFilename );
  236.       SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  237.  
  238.       break;
  239.     }
  240.     // A file is about to be replaced
  241.     case XM_REPLACINGFILE:
  242.     {
  243.       xcdReplacingFileParamsW* pParams = ( xcdReplacingFileParamsW * ) lParam;
  244.  
  245.       // Let's confirm with the user
  246.       char  szMsg[ 1024 ];
  247.       wsprintfA( szMsg, "Replace file %S\nLast modified %02d/%02d/%04d at %02dh%02d\n\n"
  248.                         "with file %S\nLast modified %02d/%02d/%04d at %02dh%02d ?",
  249.                 pParams->szOrigFilename, pParams->stOrigLastModified.wDay, 
  250.                 pParams->stOrigLastModified.wMonth, pParams->stOrigLastModified.wYear,
  251.                 pParams->stOrigLastModified.wHour, pParams->stOrigLastModified.wMinute, 
  252.                 pParams->szFilename, pParams->stLastModified.wDay, 
  253.                 pParams->stLastModified.wMonth, pParams->stLastModified.wYear,
  254.                 pParams->stLastModified.wHour, pParams->stLastModified.wMinute );
  255.  
  256.       if ( MessageBoxA( NULL, szMsg, "Replacing file", MB_ICONQUESTION | MB_YESNO ) == IDNO )
  257.       {
  258.         pParams->bReplaceFile = FALSE;
  259.       }
  260.       break;
  261.     }
  262.   }
  263. }
  264.  
  265. void DoUnzipExample( HWND hList )
  266. {
  267.   // Get a pointer to the functions structure
  268.   XceedZipFunctions * pFuncs  = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
  269.  
  270.   if( pFuncs )
  271.   {
  272.     // We send dummy messages to our callback to set it's static variables!
  273.     MyCallback( 0xffff, ( LPARAM ) hList );
  274.     MyCallback( 0xfffe, ( LPARAM ) pFuncs );
  275.  
  276.     // Create an instance for this operation. Why not work in UNICODE?
  277.     // The MyCallback function will have to expect UNICODE strings it its params.
  278.     HXCEEDZIP hZip  = pFuncs->lpfnXzCreateXceedZipW( XCD_LICENSE_W );
  279.  
  280.     if ( hZip )
  281.     {
  282.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Unzipping example -----" );
  283.  
  284.       pFuncs->lpfnXzSetXceedZipCallback( hZip, MyCallback );
  285.  
  286.       pFuncs->lpfnXzSetZipFilenameW( hZip, XCD_ZIPFILENAME_W );     // Wide version
  287.       pFuncs->lpfnXzSetUnzipToFolderW( hZip, XCD_UNZIPTOFOLDER_W ); // Wide version
  288.  
  289.       int nErr  = pFuncs->lpfnXzUnzip( hZip );
  290.   
  291.       // Windows 95 does not implement UNICODE APIs, so we must use SendMessageA
  292.       // (SendMessage is mapped to SendMessageA when UNICODE isn't define)
  293.       // Why not call XzGetErrorDescriptionA. It's not because we created the
  294.       // instance with the wide version that we are stuck calling wide functions.
  295.       // The only impact of creating the instance with the wide or ansi version
  296.       // is the format of the string parameters in events.
  297.       char  szMsg[ 200 ];
  298.       pFuncs->lpfnXzGetErrorDescriptionA( hZip, xvtError, nErr, szMsg, 200 );
  299.       SendMessageA( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  300.  
  301.       pFuncs->lpfnXzDestroyXceedZip( hZip );
  302.     }
  303.  
  304.     // Clean-up our callback's static variables.
  305.     MyCallback( 0xffff, 0 );
  306.     MyCallback( 0xfffe, 0 );
  307.   }
  308. }
  309.  
  310.  
  311. // Previewing example
  312. // ------------------
  313. // Back to a window message handling. Here, we show the BackgroundProcessing
  314. // method. The method call returns right away with a special code.
  315.  
  316. void DoPreviewExample( HWND hParent, HWND hList, HXCEEDZIP* phZip )
  317. {
  318.   // Get a pointer to the functions structure
  319.   XceedZipFunctions * pFuncs  = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
  320.  
  321.   if( pFuncs )
  322.   {
  323.     // Create an instance
  324.     *phZip  = pFuncs->lpfnXzCreateXceedZipA( XCD_LICENSE );
  325.  
  326.     if ( *phZip )
  327.     {
  328.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Previewing example -----" );
  329.  
  330.       pFuncs->lpfnXzSetXceedZipWindow( *phZip, hParent );
  331.       pFuncs->lpfnXzSetFilesToProcessA( *phZip, XCD_FILESTOPROCESS );
  332.       pFuncs->lpfnXzSetProcessSubfolders( *phZip, TRUE );
  333.       pFuncs->lpfnXzSetBackgroundProcessing( *phZip, TRUE );
  334.  
  335.       int nErr  = pFuncs->lpfnXzPreviewFiles( *phZip, FALSE );
  336.   
  337.       TCHAR szMsg[ 200 ];
  338.       pFuncs->lpfnXzGetErrorDescriptionA( *phZip, xvtError, nErr, szMsg, 200 );
  339.  
  340.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  341.  
  342.       if( nErr != 1 /* xerProcessStarted */ )
  343.       {
  344.         // The window won't get the XM_PROCESSCOMPLETED event, so clean up
  345.         pFuncs->lpfnXzDestroyXceedZip( *phZip );
  346.         *phZip  = NULL;
  347.       }
  348.       // else, it's the ProcessCompleted event handler that will clean up!
  349.     }
  350.   }
  351. }
  352.  
  353.  
  354. // Listing example
  355. // ---------------
  356. // Here, we show how to use method (b). The XceedZipAPI.h file already defines 
  357. // all function pointer types.
  358.  
  359. void DoListingExample( HWND hParent, HWND hList )
  360. {
  361.   LPFNXZCREATEXCEEDZIPA lpfnCreate  =
  362.     ( LPFNXZCREATEXCEEDZIPA ) GetProcAddress( hXceedZipDll, "XzCreateXceedZipA" );
  363.  
  364.   LPFNXZSETXCEEDZIPWINDOW lpfnSetWindow = 
  365.     ( LPFNXZSETXCEEDZIPWINDOW ) GetProcAddress( hXceedZipDll, "XzSetXceedZipWindow" );
  366.  
  367.   LPFNXZSETZIPFILENAMEA lpfnSetZipFilename  = 
  368.     ( LPFNXZSETZIPFILENAMEA ) GetProcAddress( hXceedZipDll, "XzSetZipFilenameA" );
  369.  
  370.   LPFNXZLISTZIPCONTENTS lpfnListZipContents = 
  371.     ( LPFNXZLISTZIPCONTENTS ) GetProcAddress( hXceedZipDll, "XzListZipContents" );
  372.  
  373.   LPFNXZGETERRORDESCRIPTIONA  lpfnGetErrorDesc  = 
  374.     ( LPFNXZGETERRORDESCRIPTIONA ) GetProcAddress( hXceedZipDll, "XzGetErrorDescriptionA" );
  375.  
  376.   LPFNXZDESTROYXCEEDZIP lpfnDestroy = 
  377.     ( LPFNXZDESTROYXCEEDZIP ) GetProcAddress( hXceedZipDll, "XzDestroyXceedZip" );
  378.  
  379.  
  380.   if ( lpfnCreate && lpfnSetWindow && lpfnSetZipFilename && 
  381.        lpfnListZipContents && lpfnGetErrorDesc && lpfnDestroy )
  382.   {
  383.     HXCEEDZIP hZip  = lpfnCreate( XCD_LICENSE_A );
  384.  
  385.     if ( hZip )
  386.     {
  387.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Listing example -----" );
  388.  
  389.       lpfnSetWindow( hZip, hParent );
  390.       lpfnSetZipFilename( hZip, XCD_ZIPFILENAME_A );
  391.  
  392.       int nErr  = lpfnListZipContents( hZip );
  393.     
  394.       TCHAR szMsg[ 200 ];
  395.       lpfnGetErrorDesc( hZip, xvtError, nErr, szMsg, 200 );
  396.  
  397.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  398.  
  399.       lpfnDestroy( hZip );
  400.     }
  401.   }
  402. }
  403.  
  404. // Testing example
  405. // ---------------
  406. // Back to the (c) method. Are you still wondering why? q;-)
  407.  
  408. void DoTestingExample( HWND hParent, HWND hList )
  409. {
  410.   // Get the structure
  411.   XceedZipFunctions * pFuncs  = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
  412.  
  413.   if ( pFuncs )
  414.   {
  415.     HXCEEDZIP hZip  = pFuncs->lpfnXzCreateXceedZipA( XCD_LICENSE_A );
  416.  
  417.     if ( hZip )
  418.     {
  419.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Testing example -----" );
  420.  
  421.       pFuncs->lpfnXzSetXceedZipWindow( hZip, hParent );
  422.       pFuncs->lpfnXzSetZipFilenameA( hZip, XCD_ZIPFILENAME_A );
  423.  
  424.       int nErr  = pFuncs->lpfnXzTestZipFile( hZip, TRUE );
  425.  
  426.       TCHAR szMsg[ 200 ];
  427.       pFuncs->lpfnXzGetErrorDescriptionA( hZip, xvtError, nErr, szMsg, 200 );
  428.  
  429.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  430.  
  431.       pFuncs->lpfnXzDestroyXceedZip( hZip );
  432.     }
  433.   }
  434. }
  435.  
  436. // Getting zip contents example
  437. // ----------------------------
  438. // Again, using method (c) is the easiest. In this example, we show
  439. // you how to retrieve a zip file's contents without having to handle
  440. // messages or callbacks.
  441.  
  442. void DoGetContentsExample( HWND hParent, HWND hList )
  443. {
  444.   // Get the structure
  445.   XceedZipFunctions * pFuncs  = ( XceedZipFunctions * ) GetProcAddress( hXceedZipDll, "g_xzFunctions" );
  446.  
  447.   if ( pFuncs )
  448.   {
  449.     HXCEEDZIP hZip  = pFuncs->lpfnXzCreateXceedZipA( XCD_LICENSE_A );
  450.  
  451.     if ( hZip )
  452.     {
  453.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) "----- Get zip contents example -----" );
  454.  
  455.       pFuncs->lpfnXzSetZipFilenameA( hZip, XCD_ZIPFILENAME_A );
  456.  
  457.       HXCEEDZIPITEMS  hItems;
  458.       int nErr  = pFuncs->lpfnXzGetZipContents( hZip, &hItems );
  459.  
  460.       char  szMsg[ MAX_PATH + 100 ];
  461.       pFuncs->lpfnXzGetErrorDescriptionA( hZip, xvtError, nErr, szMsg, 200 );
  462.  
  463.       SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  464.  
  465.       if( nErr == xerSuccess )
  466.       {
  467.         // The hItems handle enables us to wlak throught each item in the zip file
  468.         xcdListingFileParamsA xItemInfo;
  469.  
  470.         BOOL  bAvailable  = pFuncs->lpfnXziGetFirstItemA( hItems, &xItemInfo );
  471.  
  472.         while( bAvailable )
  473.         {
  474.           wsprintf( szMsg, "%s [%ukb -> %ukb] %02d/%02d/%04d", 
  475.                     xItemInfo.szFilename, 
  476.                     ( xItemInfo.lSize+1023 ) / 1024,
  477.                     ( xItemInfo.lCompressedSize+1023) / 1024, 
  478.                     xItemInfo.stLastModified.wDay,
  479.                     xItemInfo.stLastModified.wMonth, 
  480.                     xItemInfo.stLastModified.wYear );
  481.           SendMessage( hList, LB_ADDSTRING, 0, ( LPARAM ) szMsg );
  482.  
  483.           bAvailable  = pFuncs->lpfnXziGetNextItemA( hItems, &xItemInfo );
  484.         }
  485.  
  486.         // Important: We must always destroy the items handle!
  487.         pFuncs->lpfnXziDestroyXceedZipItems( hItems );
  488.       }
  489.  
  490.       pFuncs->lpfnXzDestroyXceedZip( hZip );
  491.     }
  492.   }
  493. }
  494.  
  495.  
  496. //
  497. // END_OF_FILE
  498. //
  499.