home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedZip.Cab / F112433_Compress.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-10  |  6.9 KB  |  206 lines

  1. /*
  2.  * Compress Sample for C++ Builder 4
  3.  *
  4.  * This sample demonstrates the following features:
  5.  *  - Accessing the DLL API from within C++ Builder.
  6.  *  - Compressing and decompressing a memory buffer.
  7.  *
  8.  * This sample is part of the Xceed Zip Compression Library package.
  9.  * Copyright (c) 1999-2001 Xceed Software Inc.
  10.  */
  11.  
  12. #include <vcl.h>
  13. #pragma hdrstop
  14.  
  15. #include "Compress.h"
  16.  
  17. #pragma package(smart_init)
  18. #pragma resource "*.dfm"
  19.  
  20. TfrmCompress *frmCompress;
  21.  
  22. __fastcall TfrmCompress::TfrmCompress(TComponent* Owner)
  23.   : TForm( Owner )
  24. {
  25.   // Initialize members
  26.   m_hXceedZipDll  = NULL;
  27.   m_psExportedApi = NULL;
  28.  
  29.   // In order to use the DLL API, you must load the library
  30.   m_hXceedZipDll = LoadLibrary( "XceedZip.dll" );
  31.  
  32.  
  33.   if( m_hXceedZipDll )
  34.   {
  35.     // You could get each proc one by one, but I prefer the g_xzFunctions
  36.     // exported symbol. This is a pointer to an XceedZipFunctions structure
  37.     // already holding a pointer to each exported function.
  38.     m_psExportedApi = ( XceedZipFunctions* )GetProcAddress( m_hXceedZipDll, "g_xzFunctions" );
  39.  
  40.     if( m_psExportedApi )
  41.     {
  42.       // In order to use the DLL API, we must initialize the library
  43.       m_psExportedApi->lpfnXceedZipInitDLL();
  44.     }
  45.   }
  46. }
  47.  
  48. __fastcall TfrmCompress::~TfrmCompress( void )
  49. {
  50.   if( m_hXceedZipDll )
  51.   {
  52.     if( m_psExportedApi )
  53.     {
  54.       // For each call to XceedZipInitDLL, there must be a matching call to
  55.       // XceedZipShutdownDLL
  56.       m_psExportedApi->lpfnXceedZipShutdownDLL();
  57.     }
  58.  
  59.     // We release the XceedZip.dll file
  60.     FreeLibrary( m_hXceedZipDll );
  61.   }
  62. }
  63.  
  64. void __fastcall TfrmCompress::btCompressClick(TObject *Sender)
  65. {
  66.   if( !m_hXceedZipDll )
  67.   {
  68.     mmoDest->Text = "Could not load the XCEEDZIP.DLL file";
  69.   }
  70.   else if( !m_psExportedApi )
  71.   {
  72.     mmoDest->Text = "Could not retrieve a pointer to the g_xzFunctions "
  73.                     "exported symbol";
  74.   }
  75.   else
  76.   {
  77.     // Reset mmoDest
  78.     mmoDest->Text = "";
  79.  
  80.     // Retrieve a handle on a compression object
  81.     HXCEEDCMP hComp = m_psExportedApi->lpfnXcCreateXceedCompressionA( YOUR_LICENSE_STRING );
  82.  
  83.     if( !hComp )
  84.     {
  85.       mmoDest->Text = "Cannot create an instance of the XceedCompression object";
  86.       return;
  87.     }
  88.  
  89.     // Accumulated messages in output.
  90.     WideString  sMessage;
  91.  
  92.     // We want the best compression, so let's set the compression level to high
  93.     m_psExportedApi->lpfnXcSetCompressionLevel( hComp, xclHigh );
  94.  
  95.     // The XcCompress API takes an array of BYTE, and returns a newly allocated
  96.     // array of BYTE representing the compressed data.
  97.     DWORD dwOrigSize = mmoSource->GetTextLen() + 1; // We'll keep the null char;
  98.     BYTE* pcOrigData = new BYTE [ dwOrigSize ];
  99.  
  100.     mmoSource->GetTextBuf( ( char* )pcOrigData, dwOrigSize );
  101.  
  102.     // We'll also take the opportunity to show how to use XcCalculateCrc for
  103.     // data validation! The "lPreviousCrc" parameter is only useful when you
  104.     // want to calculate the CRC of a huge buffer, small blocks at a time, or
  105.     // for streamed data you are accessing only portions at a time.
  106.     long lCRC = m_psExportedApi->lpfnXcCalculateCrc( hComp, pcOrigData, dwOrigSize, 0 );
  107.  
  108.     sMessage += "The checksum for the source data is 0x" + IntToHex( ( int )lCRC, 8 ) + ".\r\n";
  109.     mmoDest->Text = sMessage;
  110.  
  111.     // We compress the data using the XcCompress API. The "bEndOfData"
  112.     // parameter is useful when compressing streamed data, or when you want to
  113.     // compress a huge buffer, but small blocks at a time.
  114.     DWORD dwDestSize;
  115.     BYTE* pcDestData;
  116.  
  117.     xcdCompressionError xResult = m_psExportedApi->lpfnXcCompress(
  118.       hComp, pcOrigData, dwOrigSize, &pcDestData, &dwDestSize, TRUE );
  119.  
  120.     if( xResult == xceSuccess )
  121.     {
  122.       sMessage += "Compressed " + IntToStr( dwOrigSize ) + " bytes "
  123.                   "into " + IntToStr( dwDestSize ) + " bytes.\r\n";
  124.       mmoDest->Text = sMessage;
  125.  
  126.       // Now, let's uncompress this back to the original message
  127.       DWORD dwBackSize;
  128.       BYTE* pcBackData;
  129.  
  130.       xResult = m_psExportedApi->lpfnXcUncompress(
  131.         hComp, pcDestData, dwDestSize, &pcBackData, &dwBackSize, TRUE );
  132.  
  133.       if( xResult == xceSuccess )
  134.       {
  135.         sMessage += "Uncompressed " + IntToStr( dwDestSize ) + " bytes "
  136.                     "back to " + IntToStr( dwBackSize ) + " bytes.\r\n";
  137.  
  138.         // Let's recheck the CRC! But be aware that the library already keeps
  139.         // the CRC of the data and verifies this CRC when uncompressing.
  140.         long lNewCRC = m_psExportedApi->lpfnXcCalculateCrc( hComp, pcBackData, dwBackSize, 0 );
  141.  
  142.         sMessage += "The checksum for the new data is 0x" +
  143.                     IntToHex( ( int )lNewCRC, 8 ) + ".\r\n";
  144.  
  145.         if( lNewCRC == lCRC )
  146.         {
  147.           sMessage += "The data is identical.\r\n";
  148.         }
  149.         else
  150.         {
  151.           sMessage += "The data was corrupted\r\n";
  152.         }
  153.  
  154.         // Redisplay the text!
  155.         sMessage += "\r\n";
  156.         sMessage += ( char* )pcBackData;
  157.         mmoDest->Text = sMessage;
  158.  
  159.         // The data pcBackData points to was allocated with CoTaskMemAlloc by
  160.         // the library. In order to free this data, you must either use an
  161.         // explicit call to CoTaskMemFree, or use the exported API XzFree.
  162.         m_psExportedApi->lpfnXzFree( pcBackData );
  163.       }
  164.       else
  165.       {
  166.         sMessage += "An error occured while uncompressing the data.";
  167.         mmoDest->Text = sMessage;
  168.       }
  169.  
  170.       // The data pcDestData points to was allocated with CoTaskMemAlloc by
  171.       // the library. In order to free this data, you must either use an
  172.       // explicit call to CoTaskMemFree, or use the exported API XzFree.
  173.       m_psExportedApi->lpfnXzFree( pcDestData );
  174.     }
  175.     else if( xResult == xceNotLicensed )
  176.     {
  177.       mmoDest->Text = "The library is not licensed properly.\r\n\r\n"
  178.                       "If you have a registered version, you must insert "
  179.                       "your license string in the YOUR_LICENSE_STRING macro.\r\n";
  180.     }
  181.     else
  182.     {
  183.       mmoDest->Text = "An error occured while compressing the data.\r\n";
  184.     }
  185.  
  186.     delete [] pcOrigData;
  187.  
  188.     // When we are done with an instance, we must not forget to free it.
  189.     m_psExportedApi->lpfnXcDestroyXceedCompression( hComp );
  190.   }
  191. }
  192.  
  193. void __fastcall TfrmCompress::FormCreate(TObject *Sender)
  194. {
  195.   // Fill both CompressionMethod and CompressionLevel combo boxes
  196.   cbMethod->Items->AddObject( "Deflate", ( TObject* )xcmDeflated );
  197.   cbMethod->Items->AddObject( "Burrow-Wheeler", ( TObject* )xcmBurrowWheeler );
  198.  
  199.   cbLevel->Items->AddObject( "None", ( TObject* )xclNone );
  200.   cbLevel->Items->AddObject( "Low",  ( TObject* )xclLow );
  201.   cbLevel->Items->AddObject( "Medium", ( TObject* )xclMedium );
  202.   cbLevel->Items->AddObject( "High", ( TObject* )xclHigh );
  203. }
  204. //---------------------------------------------------------------------------
  205.  
  206.