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

  1. /*
  2.   Xceed Streaming Compression Library - Memory Compress sample
  3.   Copyright (c) 2001 Xceed Software Inc.
  4.  
  5.   [unMain.cpp]
  6.  
  7.   This CPP demonstrates how to compress a chunk of memory data using
  8.   different kinds of compression formats, and decompress compressed
  9.   memory data. It specifically uses:
  10.     - The Compress and Decompress methods
  11.     - The CompressionFormat property
  12.  
  13.   This file is part of the Xceed Streaming Compression Library sample
  14.   applications. The source code in this file is only intended as a
  15.   supplement to the Xceed Streaming Compression Library's documentation,
  16.   and is provided "as is", without warranty of any kind, either expressed
  17.   or implied.
  18. */
  19.  
  20. //---------------------------------------------------------------------------
  21.  
  22. #include <vcl.h>
  23. #pragma hdrstop
  24.  
  25. #include "unMain.h"
  26. //---------------------------------------------------------------------------
  27. #pragma package(smart_init)
  28. #pragma link "XceedStreamingCompressionLib_OCX"
  29. #pragma resource "*.dfm"
  30. TfrmMain *frmMain;
  31.  
  32. const char* szTextToCompress = "This is a little test to show you how the memory "
  33.                                "compression works. And it is very easy to use. "
  34.                                "And there is some repeating repeating repeating "
  35.                                "repeating repeating repeating repeating "
  36.                                "repeating text.";
  37.  
  38. //---------------------------------------------------------------------------
  39. // Initialize the original size label. The combo box was filled manully using
  40. // the property box.
  41. //---------------------------------------------------------------------------
  42. __fastcall TfrmMain::TfrmMain(TComponent* Owner)
  43.   : TForm(Owner)
  44. {
  45.   mmoTextToCompress->Text          = AnsiString( szTextToCompress );
  46.  
  47.   // We add 1 to the length since we will be compressing the null char
  48.   lblOrigSize->Caption             = IntToStr( AnsiString( szTextToCompress ).Length() + 1 );
  49.   cboCompressionFormats->ItemIndex = 0;
  50.  
  51.   // We recommend never dropping the XceedStreamingCompression control on a form
  52.   // since it exposes dispatch interfaces by default, and it is much easier to
  53.   // use the IXceedStreamingCompression interface from C++.
  54.   m_piStreamComp.CreateInstance( CLSID_XceedStreamingCompression );
  55. }
  56.  
  57. //---------------------------------------------------------------------------
  58. // Call FreeBuffer to free any used memory.
  59. //---------------------------------------------------------------------------
  60. __fastcall TfrmMain::~TfrmMain()
  61. {
  62.   FreeBuffer();
  63. }
  64.  
  65. //---------------------------------------------------------------------------
  66. // Create and prepare the compression method/format depending on the item
  67. // selected in the combo box.
  68. //---------------------------------------------------------------------------
  69. void TfrmMain::PrepareFormat( int nFormat )
  70. {
  71.   try
  72.   {
  73.     switch( nFormat )
  74.     {
  75.       case 0:
  76.       {
  77.         IXceedBZip2CompressionFormatPtr piBZip2;
  78.         piBZip2.CreateInstance( CLSID_XceedBZip2CompressionFormat );
  79.  
  80.         m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piBZip2 );
  81.         break;
  82.       }
  83.       case 1:
  84.       {
  85.         IXceedGZipCompressionFormatPtr piGZip;
  86.         piGZip.CreateInstance( CLSID_XceedGZipCompressionFormat );
  87.  
  88.         m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piGZip );
  89.         break;
  90.       }
  91.       case 2:
  92.       {
  93.         IXceedStandardCompressionFormatPtr piStandard;
  94.         piStandard.CreateInstance( CLSID_XceedStandardCompressionFormat );
  95.  
  96.         m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piStandard );
  97.         break;
  98.       }
  99.       case 3:
  100.       {
  101.         IXceedZip3CompressionFormatPtr piZip3;
  102.         piZip3.CreateInstance( CLSID_XceedZip3CompressionFormat );
  103.  
  104.         m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piZip3 );
  105.         break;
  106.       }
  107.       case 4:
  108.       {
  109.         IXceedZLibCompressionFormatPtr piZLib;
  110.         piZLib.CreateInstance( CLSID_XceedZLibCompressionFormat );
  111.  
  112.         m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piZLib );
  113.         break;
  114.       }
  115.       // The next three items are compression methods, not formats!
  116.       case 5:
  117.       {
  118.         IXceedBWTCompressionMethodPtr piBWT;
  119.         piBWT.CreateInstance( CLSID_XceedBWTCompressionMethod );
  120.  
  121.         m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piBWT );
  122.         break;
  123.       }
  124.       case 6:
  125.       {
  126.         IXceedDeflateCompressionMethodPtr piDeflate;
  127.         piDeflate.CreateInstance( CLSID_XceedDeflateCompressionMethod );
  128.  
  129.         m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piDeflate );
  130.         break;
  131.       }
  132.       case 7:
  133.       {
  134.         IXceedStoreCompressionMethodPtr piStore;
  135.         piStore.CreateInstance( CLSID_XceedStoreCompressionMethod );
  136.  
  137.         m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piStore );
  138.         break;
  139.       }
  140.     }
  141.   }
  142.   catch( Exception& xErr )
  143.   {
  144.     ShowMessage( xErr.Message );
  145.   }
  146.   catch( ... )
  147.   {
  148.     ShowMessage( "An unexpected error occurred while preparing the Compression Format" );
  149.   }
  150. }
  151.  
  152. //---------------------------------------------------------------------------
  153. // Free the compressed data if necessary and initialize our
  154. // member variables.
  155. //---------------------------------------------------------------------------
  156. void TfrmMain::FreeBuffer()
  157. {
  158.   if( m_pcCompressed )
  159.     CoTaskMemFree( m_pcCompressed );
  160.  
  161.   m_pcCompressed = NULL;
  162.   m_dwCompressedSize = 0;
  163. }
  164.  
  165. //---------------------------------------------------------------------------
  166. // Compress the data!
  167. //---------------------------------------------------------------------------
  168. void __fastcall TfrmMain::btCompressClick(TObject *Sender)
  169. {
  170.   char* szSource     = mmoTextToCompress->Text.c_str();
  171.   // We add 1 because we want to compress the null-char too
  172.   DWORD dwSourceSize = mmoTextToCompress->Text.Length() + 1;
  173.  
  174.   FreeBuffer();
  175.   PrepareFormat( cboCompressionFormats->ItemIndex );
  176.  
  177.   try
  178.   {
  179.     // Compress the data in a single block (bEndOfData is TRUE).
  180.     m_piStreamComp->Compress( ( BYTE* )szSource, dwSourceSize, TRUE,
  181.                               &m_pcCompressed, &m_dwCompressedSize );
  182.  
  183.     // Display the size of the compressed data
  184.     lblCompressedSize->Caption = IntToStr( m_dwCompressedSize );
  185.   }
  186.   catch( Exception& xErr )
  187.   {
  188.     ShowMessage( xErr.Message );
  189.   }
  190.   catch( ... )
  191.   {
  192.     ShowMessage( "An unexpected error occurred while compressing" );
  193.   }
  194. }
  195.  
  196. //---------------------------------------------------------------------------
  197. // Decompress the data!
  198. //---------------------------------------------------------------------------
  199. void __fastcall TfrmMain::btDecompressClick(TObject *Sender)
  200. {
  201.   BYTE* pcDecompressed     = NULL;
  202.   DWORD dwDecompressedSize = 0;
  203.  
  204.   PrepareFormat( cboCompressionFormats->ItemIndex );
  205.  
  206.   try
  207.   {
  208.     // Decompress the data in a single block (bEndOfData is TRUE)
  209.     m_piStreamComp->Decompress( m_pcCompressed, m_dwCompressedSize, TRUE,
  210.                                 &pcDecompressed, &dwDecompressedSize );
  211.  
  212.     lblDecompressedSize->Caption = IntToStr( dwDecompressedSize );
  213.     mmoDecompressedText->Text = AnsiString( ( char* )pcDecompressed );
  214.  
  215.     FreeBuffer();
  216.     CoTaskMemFree( pcDecompressed );
  217.   }
  218.   catch( Exception& xErr )
  219.   {
  220.     ShowMessage( xErr.Message );
  221.   }
  222.   catch( ... )
  223.   {
  224.     ShowMessage( "An unexpected error occurred while decompressing" );
  225.   }
  226. }
  227. //---------------------------------------------------------------------------
  228. // Update the original size label when the user modifies the text to 
  229. // compress.
  230. //---------------------------------------------------------------------------
  231. void __fastcall TfrmMain::mmoTextToCompressChange(TObject *Sender)
  232. {
  233.   lblOrigSize->Caption = IntToStr( mmoTextToCompress->Text.Length() + 1 );
  234. }
  235.  
  236.