home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedZip.Cab / F112486_Started.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-17  |  7.1 KB  |  265 lines

  1. // Started.cpp : Defines the entry point for the console application.
  2. //
  3. // ATL Sample using The Xceed Zip Compression Library 4
  4. // Copyright (c) 1998-1999 Xceed Software Inc.
  5. //
  6.  
  7. // We added ATL support in this file
  8. #include "stdafx.h"
  9. #include <stdio.h>
  10.  
  11. // These two files are distributed with the Xceed Zip Compression Library 4
  12.  
  13. #include "zipDispIds.h"       // For events' DISPIDs
  14. #include "zipAtlFuncInfo.h"   // For _ATL_FUNC_INFO structures
  15.  
  16.  
  17. // The easiest way to use the Xceed Zip Compression Library 4 with ATL
  18. // is using the #import directive. This will generate a wrapper class around
  19. // each interface in the ActiveX DLL.
  20.  
  21. #import "XceedZip.dll" no_namespace named_guids
  22.  
  23.  
  24. // Our Xceed Zip Sink ID (It can be any value)
  25.  
  26. #define DISPID_XCEEDZIP_SINK    1
  27.  
  28.  
  29. // Usually, a sink object derives from IDispEventImpl, which uses type info from the
  30. // event source's typelib to get the funcinfo. But IDispEventImpl::GetUserDefinedType()
  31. // only handles TKIND_ALIAS. In many of our events, we have TKIND_ENUM parameters.
  32. // This is why the sink object derives from IDispEventSimpleImpl, which does not use
  33. // type information. We need to provide an _ATL_FUNC_INFO for each event we want to
  34. // handle. These can be found in the zipAtlFuncInfo.h file
  35.  
  36. class CXceedZipEventSink 
  37.   : public IDispEventSimpleImpl< DISPID_XCEEDZIP_SINK, CXceedZipEventSink, &DIID__IXceedZipEvents >
  38. {
  39. public:
  40.   //
  41.   // Constructor
  42.   //
  43.  
  44.   CXceedZipEventSink( void )  { m_pZip = NULL; }
  45.  
  46.   //
  47.   // Utility methods for our event handling routines
  48.   //
  49.  
  50.   void SetXceedZip( IXceedZip * pZip = NULL )  { m_pZip = pZip; }
  51.  
  52.   //
  53.   // The event handling routines
  54.   //
  55.  
  56.   void _stdcall ListingFile( BSTR sFilename,
  57.                              BSTR sComment,
  58.                              long lSize,
  59.                              long lCompressedSize,
  60.                              short nCompressionRatio,
  61.                              long lAttributes,
  62.                              long lCRC,
  63.                              DATE dtLastModified,
  64.                              DATE dtLastAccessed,
  65.                              DATE dtCreated,
  66.                              enum xcdCompressionMethod xMethod,
  67.                              VARIANT_BOOL bEncrypted,
  68.                              int nDiskNumber,
  69.                              VARIANT_BOOL bExcluded,
  70.                              enum xcdSkippingReason xReason )
  71.   {
  72.     printf( "%S [%d bytes]\n", sFilename, lSize );
  73.   }
  74.  
  75.   void __stdcall FileStatus( BSTR sFilename, 
  76.                              long lSize, 
  77.                              long lCompressedSize, 
  78.                              long lBytesProcessed, 
  79.                              short nBytesPercent, 
  80.                              short nCompressionRatio, 
  81.                              VARIANT_BOOL bFileCompleted )
  82.   {
  83.     if( m_pZip )
  84.     {
  85.       xcdCurrentOperation xOper = m_pZip->CurrentOperation;
  86.  
  87.       switch( xOper )
  88.       {
  89.         case xcoZipping:
  90.           if( lBytesProcessed == 0 )
  91.           {
  92.             printf( "\nZipping %S [  0%%]", sFilename );
  93.           }
  94.           else
  95.           {
  96.             printf( "\b\b\b\b\b%03d%%]", nBytesPercent );
  97.           }
  98.           break;
  99.  
  100.         case xcoUnzipping:
  101.           if( lBytesProcessed == 0 )
  102.           {
  103.             printf( "\nUnzipping %S [  0%%]", sFilename );
  104.           }
  105.           else
  106.           {
  107.             printf( "\b\b\b\b\b%03d%%]", nBytesPercent );
  108.           }
  109.           break;
  110.       }
  111.     }
  112.   }
  113.  
  114.   // The sink map is used by IDispEventSimpleImpl<>. The map contains information about
  115.   // each event handler we want to provide.
  116.   BEGIN_SINK_MAP( CXceedZipEventSink )   
  117.     SINK_ENTRY_INFO(DISPID_XCEEDZIP_SINK, DIID__IXceedZipEvents, XCD_ZIP_DISPID_LISTINGFILE, ListingFile, &ListingFile_Info)
  118.     SINK_ENTRY_INFO(DISPID_XCEEDZIP_SINK, DIID__IXceedZipEvents, XCD_ZIP_DISPID_FILESTATUS, FileStatus, &FileStatus_Info)
  119.   END_SINK_MAP()
  120.  
  121. private:
  122.   //
  123.   // Private members
  124.   //
  125.  
  126.   IXceedZip*  m_pZip;
  127. };
  128.  
  129.  
  130. int main(int argc, char* argv[])
  131. {
  132.   printf( "ATL Getting Started sample using The Xceed Zip Compression Library 4\n\n" );
  133.  
  134.   char  cAction         = ' ';
  135.   char* pszZipFilename  = NULL;
  136.   bool  bRecurse        = false;
  137.   bool  bPreserve       = false;
  138.   int   nStartFiles     = 0;
  139.   int   nEndFiles       = 0;
  140.   char* pszDestFolder   = NULL;
  141.  
  142.   int nIndex  = 1;
  143.  
  144.   while( nIndex < argc && !pszZipFilename )
  145.   {
  146.     if( !lstrcmpi( argv[nIndex], "-z" ) )
  147.     {
  148.       cAction = 'Z';
  149.     }
  150.     else if( !lstrcmpi( argv[nIndex], "-u" ) )
  151.     {
  152.       cAction = 'U';
  153.     }
  154.     else if( !lstrcmpi( argv[nIndex], "-l" ) )
  155.     {
  156.       cAction = 'L';
  157.     }
  158.     else if( !lstrcmpi( argv[nIndex], "-r" ) )
  159.     {
  160.       bRecurse  = true;
  161.     }
  162.     else if( !lstrcmpi( argv[nIndex], "-p" ) )
  163.     {
  164.       bPreserve = true;
  165.     }
  166.     else
  167.     {
  168.       // This is the zip filename
  169.       pszZipFilename  = argv[nIndex];
  170.  
  171.       nStartFiles = nIndex + 1;
  172.       nEndFiles   = argc - 1;
  173.  
  174.       if( cAction == 'U' )
  175.       {
  176.         pszDestFolder = argv[argc-1];
  177.         nEndFiles--;
  178.       }
  179.     }
  180.  
  181.     nIndex++;
  182.   }
  183.  
  184.   if(   cAction == ' ' 
  185.      || !pszZipFilename 
  186.      || ( cAction == 'Z' && nStartFiles > nEndFiles ) )
  187.   {
  188.     printf( "Format: STARTED action [options] zip_filename [files] [destination]\n\n" );
  189.     printf( "action : -z = zipping\n" );
  190.     printf( "         -u = unzipping\n" );
  191.     printf( "         -l = listing\n" );
  192.     printf( "options : -r = recurse\n" );
  193.     printf( "          -p = preserve paths\n" );
  194.     printf( "zip_filename : the zip file to create or read\n" );
  195.     printf( "files : list of files to zip, unzip or list\n" );
  196.     printf( "destination : unzipping destination (required)\n" );
  197.  
  198.     return 0;
  199.   }
  200.  
  201.   CoInitialize( NULL );
  202.  
  203.   try 
  204.   {
  205.     // Instanciate XceedZip
  206.     IXceedZipPtr  pZip( CLSID_XceedZip );
  207.  
  208.     // Create and connect event sink
  209.     CXceedZipEventSink  xEventSink;
  210.     xEventSink.DispEventAdvise( pZip );
  211.  
  212.     // In this example, our event sink needs a pointer to an IXceedZip
  213.     xEventSink.SetXceedZip( pZip );
  214.  
  215.     // Set properties
  216.     pZip->ZipFilename     = pszZipFilename;
  217.     pZip->FilesToProcess  = "";
  218.  
  219.     while( nStartFiles <= nEndFiles )
  220.     {
  221.       pZip->AddFilesToProcess( argv[nStartFiles++] );
  222.     }
  223.  
  224.     pZip->ProcessSubfolders = bRecurse;
  225.     pZip->PreservePaths     = bPreserve;
  226.  
  227.     xcdError xErr = xerSuccess;
  228.     
  229.     switch( cAction )
  230.     {
  231.       case 'Z':
  232.         xErr  = pZip->Zip();
  233.         break;
  234.  
  235.       case 'U':
  236.         pZip->UnzipToFolder = pszDestFolder;
  237.         xErr  = pZip->Unzip();
  238.         break;
  239.  
  240.       case 'L':
  241.         xErr  = pZip->ListZipContents();
  242.         break;
  243.     }
  244.  
  245.     if( xErr != xerSuccess )
  246.     {
  247.       printf( pZip->GetErrorDescription( xvtError, xErr ) );
  248.     }
  249.   }
  250.   catch( const _com_error& xErr ) 
  251.   {
  252.     // The generated wrapper classes throw _com_error exceptions 
  253.     // when a COM error occurs.
  254.     printf( "\nCOM Error 0x%08X ( %s ).\n", xErr.Error(), xErr.ErrorMessage() );
  255.   }
  256.   catch ( ... ) 
  257.   {
  258.     printf( "\nUnhandled Exception.\n" );
  259.   }
  260.  
  261.   CoUninitialize();
  262.  
  263.   return 0;
  264. }
  265.