home *** CD-ROM | disk | FTP | other *** search
- /*
- * Compress Sample for C++ Builder 4
- *
- * This sample demonstrates the following features:
- * - Accessing the DLL API from within C++ Builder.
- * - Compressing and decompressing a memory buffer.
- *
- * This sample is part of the Xceed Zip Compression Library package.
- * Copyright (c) 1999-2001 Xceed Software Inc.
- */
-
- #include <vcl.h>
- #pragma hdrstop
-
- #include "Compress.h"
-
- #pragma package(smart_init)
- #pragma resource "*.dfm"
-
- TfrmCompress *frmCompress;
-
- __fastcall TfrmCompress::TfrmCompress(TComponent* Owner)
- : TForm( Owner )
- {
- // Initialize members
- m_hXceedZipDll = NULL;
- m_psExportedApi = NULL;
-
- // In order to use the DLL API, you must load the library
- m_hXceedZipDll = LoadLibrary( "XceedZip.dll" );
-
-
- if( m_hXceedZipDll )
- {
- // You could get each proc one by one, but I prefer the g_xzFunctions
- // exported symbol. This is a pointer to an XceedZipFunctions structure
- // already holding a pointer to each exported function.
- m_psExportedApi = ( XceedZipFunctions* )GetProcAddress( m_hXceedZipDll, "g_xzFunctions" );
-
- if( m_psExportedApi )
- {
- // In order to use the DLL API, we must initialize the library
- m_psExportedApi->lpfnXceedZipInitDLL();
- }
- }
- }
-
- __fastcall TfrmCompress::~TfrmCompress( void )
- {
- if( m_hXceedZipDll )
- {
- if( m_psExportedApi )
- {
- // For each call to XceedZipInitDLL, there must be a matching call to
- // XceedZipShutdownDLL
- m_psExportedApi->lpfnXceedZipShutdownDLL();
- }
-
- // We release the XceedZip.dll file
- FreeLibrary( m_hXceedZipDll );
- }
- }
-
- void __fastcall TfrmCompress::btCompressClick(TObject *Sender)
- {
- if( !m_hXceedZipDll )
- {
- mmoDest->Text = "Could not load the XCEEDZIP.DLL file";
- }
- else if( !m_psExportedApi )
- {
- mmoDest->Text = "Could not retrieve a pointer to the g_xzFunctions "
- "exported symbol";
- }
- else
- {
- // Reset mmoDest
- mmoDest->Text = "";
-
- // Retrieve a handle on a compression object
- HXCEEDCMP hComp = m_psExportedApi->lpfnXcCreateXceedCompressionA( YOUR_LICENSE_STRING );
-
- if( !hComp )
- {
- mmoDest->Text = "Cannot create an instance of the XceedCompression object";
- return;
- }
-
- // Accumulated messages in output.
- WideString sMessage;
-
- // We want the best compression, so let's set the compression level to high
- m_psExportedApi->lpfnXcSetCompressionLevel( hComp, xclHigh );
-
- // The XcCompress API takes an array of BYTE, and returns a newly allocated
- // array of BYTE representing the compressed data.
- DWORD dwOrigSize = mmoSource->GetTextLen() + 1; // We'll keep the null char;
- BYTE* pcOrigData = new BYTE [ dwOrigSize ];
-
- mmoSource->GetTextBuf( ( char* )pcOrigData, dwOrigSize );
-
- // We'll also take the opportunity to show how to use XcCalculateCrc for
- // data validation! The "lPreviousCrc" parameter is only useful when you
- // want to calculate the CRC of a huge buffer, small blocks at a time, or
- // for streamed data you are accessing only portions at a time.
- long lCRC = m_psExportedApi->lpfnXcCalculateCrc( hComp, pcOrigData, dwOrigSize, 0 );
-
- sMessage += "The checksum for the source data is 0x" + IntToHex( ( int )lCRC, 8 ) + ".\r\n";
- mmoDest->Text = sMessage;
-
- // We compress the data using the XcCompress API. The "bEndOfData"
- // parameter is useful when compressing streamed data, or when you want to
- // compress a huge buffer, but small blocks at a time.
- DWORD dwDestSize;
- BYTE* pcDestData;
-
- xcdCompressionError xResult = m_psExportedApi->lpfnXcCompress(
- hComp, pcOrigData, dwOrigSize, &pcDestData, &dwDestSize, TRUE );
-
- if( xResult == xceSuccess )
- {
- sMessage += "Compressed " + IntToStr( dwOrigSize ) + " bytes "
- "into " + IntToStr( dwDestSize ) + " bytes.\r\n";
- mmoDest->Text = sMessage;
-
- // Now, let's uncompress this back to the original message
- DWORD dwBackSize;
- BYTE* pcBackData;
-
- xResult = m_psExportedApi->lpfnXcUncompress(
- hComp, pcDestData, dwDestSize, &pcBackData, &dwBackSize, TRUE );
-
- if( xResult == xceSuccess )
- {
- sMessage += "Uncompressed " + IntToStr( dwDestSize ) + " bytes "
- "back to " + IntToStr( dwBackSize ) + " bytes.\r\n";
-
- // Let's recheck the CRC! But be aware that the library already keeps
- // the CRC of the data and verifies this CRC when uncompressing.
- long lNewCRC = m_psExportedApi->lpfnXcCalculateCrc( hComp, pcBackData, dwBackSize, 0 );
-
- sMessage += "The checksum for the new data is 0x" +
- IntToHex( ( int )lNewCRC, 8 ) + ".\r\n";
-
- if( lNewCRC == lCRC )
- {
- sMessage += "The data is identical.\r\n";
- }
- else
- {
- sMessage += "The data was corrupted\r\n";
- }
-
- // Redisplay the text!
- sMessage += "\r\n";
- sMessage += ( char* )pcBackData;
- mmoDest->Text = sMessage;
-
- // The data pcBackData points to was allocated with CoTaskMemAlloc by
- // the library. In order to free this data, you must either use an
- // explicit call to CoTaskMemFree, or use the exported API XzFree.
- m_psExportedApi->lpfnXzFree( pcBackData );
- }
- else
- {
- sMessage += "An error occured while uncompressing the data.";
- mmoDest->Text = sMessage;
- }
-
- // The data pcDestData points to was allocated with CoTaskMemAlloc by
- // the library. In order to free this data, you must either use an
- // explicit call to CoTaskMemFree, or use the exported API XzFree.
- m_psExportedApi->lpfnXzFree( pcDestData );
- }
- else if( xResult == xceNotLicensed )
- {
- mmoDest->Text = "The library is not licensed properly.\r\n\r\n"
- "If you have a registered version, you must insert "
- "your license string in the YOUR_LICENSE_STRING macro.\r\n";
- }
- else
- {
- mmoDest->Text = "An error occured while compressing the data.\r\n";
- }
-
- delete [] pcOrigData;
-
- // When we are done with an instance, we must not forget to free it.
- m_psExportedApi->lpfnXcDestroyXceedCompression( hComp );
- }
- }
-
- void __fastcall TfrmCompress::FormCreate(TObject *Sender)
- {
- // Fill both CompressionMethod and CompressionLevel combo boxes
- cbMethod->Items->AddObject( "Deflate", ( TObject* )xcmDeflated );
- cbMethod->Items->AddObject( "Burrow-Wheeler", ( TObject* )xcmBurrowWheeler );
-
- cbLevel->Items->AddObject( "None", ( TObject* )xclNone );
- cbLevel->Items->AddObject( "Low", ( TObject* )xclLow );
- cbLevel->Items->AddObject( "Medium", ( TObject* )xclMedium );
- cbLevel->Items->AddObject( "High", ( TObject* )xclHigh );
- }
- //---------------------------------------------------------------------------
-
-