home *** CD-ROM | disk | FTP | other *** search
- /*
- Xceed Streaming Compression Library - Memory Compress sample
- Copyright (c) 2001 Xceed Software Inc.
-
- [unMain.cpp]
-
- This CPP demonstrates how to compress a chunk of memory data using
- different kinds of compression formats, and decompress compressed
- memory data. It specifically uses:
- - The Compress and Decompress methods
- - The CompressionFormat property
-
- This file is part of the Xceed Streaming Compression Library sample
- applications. The source code in this file is only intended as a
- supplement to the Xceed Streaming Compression Library's documentation,
- and is provided "as is", without warranty of any kind, either expressed
- or implied.
- */
-
- //---------------------------------------------------------------------------
-
- #include <vcl.h>
- #pragma hdrstop
-
- #include "unMain.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma link "XceedStreamingCompressionLib_OCX"
- #pragma resource "*.dfm"
- TfrmMain *frmMain;
-
- const char* szTextToCompress = "This is a little test to show you how the memory "
- "compression works. And it is very easy to use. "
- "And there is some repeating repeating repeating "
- "repeating repeating repeating repeating "
- "repeating text.";
-
- //---------------------------------------------------------------------------
- // Initialize the original size label. The combo box was filled manully using
- // the property box.
- //---------------------------------------------------------------------------
- __fastcall TfrmMain::TfrmMain(TComponent* Owner)
- : TForm(Owner)
- {
- mmoTextToCompress->Text = AnsiString( szTextToCompress );
-
- // We add 1 to the length since we will be compressing the null char
- lblOrigSize->Caption = IntToStr( AnsiString( szTextToCompress ).Length() + 1 );
- cboCompressionFormats->ItemIndex = 0;
-
- // We recommend never dropping the XceedStreamingCompression control on a form
- // since it exposes dispatch interfaces by default, and it is much easier to
- // use the IXceedStreamingCompression interface from C++.
- m_piStreamComp.CreateInstance( CLSID_XceedStreamingCompression );
- }
-
- //---------------------------------------------------------------------------
- // Call FreeBuffer to free any used memory.
- //---------------------------------------------------------------------------
- __fastcall TfrmMain::~TfrmMain()
- {
- FreeBuffer();
- }
-
- //---------------------------------------------------------------------------
- // Create and prepare the compression method/format depending on the item
- // selected in the combo box.
- //---------------------------------------------------------------------------
- void TfrmMain::PrepareFormat( int nFormat )
- {
- try
- {
- switch( nFormat )
- {
- case 0:
- {
- IXceedBZip2CompressionFormatPtr piBZip2;
- piBZip2.CreateInstance( CLSID_XceedBZip2CompressionFormat );
-
- m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piBZip2 );
- break;
- }
- case 1:
- {
- IXceedGZipCompressionFormatPtr piGZip;
- piGZip.CreateInstance( CLSID_XceedGZipCompressionFormat );
-
- m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piGZip );
- break;
- }
- case 2:
- {
- IXceedStandardCompressionFormatPtr piStandard;
- piStandard.CreateInstance( CLSID_XceedStandardCompressionFormat );
-
- m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piStandard );
- break;
- }
- case 3:
- {
- IXceedZip3CompressionFormatPtr piZip3;
- piZip3.CreateInstance( CLSID_XceedZip3CompressionFormat );
-
- m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piZip3 );
- break;
- }
- case 4:
- {
- IXceedZLibCompressionFormatPtr piZLib;
- piZLib.CreateInstance( CLSID_XceedZLibCompressionFormat );
-
- m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piZLib );
- break;
- }
- // The next three items are compression methods, not formats!
- case 5:
- {
- IXceedBWTCompressionMethodPtr piBWT;
- piBWT.CreateInstance( CLSID_XceedBWTCompressionMethod );
-
- m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piBWT );
- break;
- }
- case 6:
- {
- IXceedDeflateCompressionMethodPtr piDeflate;
- piDeflate.CreateInstance( CLSID_XceedDeflateCompressionMethod );
-
- m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piDeflate );
- break;
- }
- case 7:
- {
- IXceedStoreCompressionMethodPtr piStore;
- piStore.CreateInstance( CLSID_XceedStoreCompressionMethod );
-
- m_piStreamComp->CompressionFormat = IXceedCompressDataPtr( piStore );
- break;
- }
- }
- }
- catch( Exception& xErr )
- {
- ShowMessage( xErr.Message );
- }
- catch( ... )
- {
- ShowMessage( "An unexpected error occurred while preparing the Compression Format" );
- }
- }
-
- //---------------------------------------------------------------------------
- // Free the compressed data if necessary and initialize our
- // member variables.
- //---------------------------------------------------------------------------
- void TfrmMain::FreeBuffer()
- {
- if( m_pcCompressed )
- CoTaskMemFree( m_pcCompressed );
-
- m_pcCompressed = NULL;
- m_dwCompressedSize = 0;
- }
-
- //---------------------------------------------------------------------------
- // Compress the data!
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::btCompressClick(TObject *Sender)
- {
- char* szSource = mmoTextToCompress->Text.c_str();
- // We add 1 because we want to compress the null-char too
- DWORD dwSourceSize = mmoTextToCompress->Text.Length() + 1;
-
- FreeBuffer();
- PrepareFormat( cboCompressionFormats->ItemIndex );
-
- try
- {
- // Compress the data in a single block (bEndOfData is TRUE).
- m_piStreamComp->Compress( ( BYTE* )szSource, dwSourceSize, TRUE,
- &m_pcCompressed, &m_dwCompressedSize );
-
- // Display the size of the compressed data
- lblCompressedSize->Caption = IntToStr( m_dwCompressedSize );
- }
- catch( Exception& xErr )
- {
- ShowMessage( xErr.Message );
- }
- catch( ... )
- {
- ShowMessage( "An unexpected error occurred while compressing" );
- }
- }
-
- //---------------------------------------------------------------------------
- // Decompress the data!
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::btDecompressClick(TObject *Sender)
- {
- BYTE* pcDecompressed = NULL;
- DWORD dwDecompressedSize = 0;
-
- PrepareFormat( cboCompressionFormats->ItemIndex );
-
- try
- {
- // Decompress the data in a single block (bEndOfData is TRUE)
- m_piStreamComp->Decompress( m_pcCompressed, m_dwCompressedSize, TRUE,
- &pcDecompressed, &dwDecompressedSize );
-
- lblDecompressedSize->Caption = IntToStr( dwDecompressedSize );
- mmoDecompressedText->Text = AnsiString( ( char* )pcDecompressed );
-
- FreeBuffer();
- CoTaskMemFree( pcDecompressed );
- }
- catch( Exception& xErr )
- {
- ShowMessage( xErr.Message );
- }
- catch( ... )
- {
- ShowMessage( "An unexpected error occurred while decompressing" );
- }
- }
- //---------------------------------------------------------------------------
- // Update the original size label when the user modifies the text to
- // compress.
- //---------------------------------------------------------------------------
- void __fastcall TfrmMain::mmoTextToCompressChange(TObject *Sender)
- {
- lblOrigSize->Caption = IntToStr( mmoTextToCompress->Text.Length() + 1 );
- }
-
-