home *** CD-ROM | disk | FTP | other *** search
- /*
- Xceed Streaming Compression Library - Compression Manager sample
- Copyright (c) 2001 Xceed Software Inc.
-
- This sample demonstrates how to compress a file using different kinds of
- compression formats and methods, and how to decompress a compressed file.
- It specifically uses:
- - The ProcessFile method
- - 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 "unManager.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma link "XceedStreamingCompressionLib_OCX"
- #pragma resource "*.dfm"
- TfrmManager *frmManager;
-
- //---------------------------------------------------------------------------
- // Initialize the form's controls. For the purposes of this example, the
- // combo box was filled using C++Builder property menu.
- //---------------------------------------------------------------------------
- __fastcall TfrmManager::TfrmManager(TComponent* Owner)
- : TForm(Owner)
- {
- cboCompressionFormats->ItemIndex = 0;
- }
-
- //---------------------------------------------------------------------------
- // Assign a default value to the destination filename if the destination
- // edit box is empty.
- //---------------------------------------------------------------------------
- void TfrmManager::SetDestinationFilename()
- {
- AnsiString sCompressedFilename = edtDestinationFile->Text;
-
- if( sCompressedFilename.Length() == 0 )
- {
- sCompressedFilename = RemoveFileExtension( edtSourceFile->Text );
- if( sCompressedFilename.Length() != 0 )
- {
- switch( ECompressionFormat( cboCompressionFormats->ItemIndex ) )
- {
- case cfBZip2:
- {
- edtDestinationFile->Text = sCompressedFilename + ".bz2";
- break;
- }
- case cfGZip:
- {
- edtDestinationFile->Text = sCompressedFilename + ".gz";
- break;
- }
- case cfStandard:
- {
- edtDestinationFile->Text = sCompressedFilename + ".std";
- break;
- }
- case cfZip3:
- {
- edtDestinationFile->Text = sCompressedFilename + ".zp3";
- break;
- }
- case cfZLib:
- {
- edtDestinationFile->Text = sCompressedFilename + ".zl";
- break;
- }
- case cfBWT:
- {
- edtDestinationFile->Text = sCompressedFilename + ".bwt";
- break;
- }
- case cfDeflate:
- {
- edtDestinationFile->Text = sCompressedFilename + ".dfl";
- break;
- }
- case cfStore:
- {
- edtDestinationFile->Text = sCompressedFilename + ".sto";
- break;
- }
- }
- }
- }
- }
-
- //---------------------------------------------------------------------------
- // Removes the file extension from the provided filename and returns the
- // the path and filename without its extension.
- //---------------------------------------------------------------------------
- AnsiString TfrmManager::RemoveFileExtension( AnsiString sFilename )
- {
- int i;
- int nFilenameLen;
- int nLenToRemove;
-
- nFilenameLen = sFilename.Length();
- i = nFilenameLen;
- nLenToRemove = -1;
-
- while( ( i > 0 ) && ( nLenToRemove == -1 ) )
- {
- if( sFilename.SubString( i, 1 ) == "." )
- nLenToRemove = i - 1;
-
- if( sFilename.SubString( i, 1 ) == "\\" )
- nLenToRemove = nFilenameLen;
-
- i = i -1;
- }
-
- if( nLenToRemove == -1 )
- return AnsiString();
- else
- return sFilename.SubString( 1, nLenToRemove );
- }
-
- //---------------------------------------------------------------------------
- // Prepare the compression format according to the user selection.
- // Return true if all succeeded.
- //---------------------------------------------------------------------------
- bool TfrmManager::PrepareCompressionFormat( IXceedStreamingCompressionPtr& piCompressor )
- {
- // We instantiate a new compression format, assigning it directly to the
- // CompressionFormat property of the Xceed Streaming Compression object.
- try
- {
- switch( ECompressionFormat( cboCompressionFormats->ItemIndex ) )
- {
- case cfBZip2:
- {
- IXceedBZip2CompressionFormatPtr piBZip2;
- piBZip2.CreateInstance( CLSID_XceedBZip2CompressionFormat );
- piCompressor->CompressionFormat = piBZip2;
- break;
- }
- case cfGZip:
- {
- IXceedGZipCompressionFormatPtr piGZip;
- piGZip.CreateInstance( CLSID_XceedGZipCompressionFormat );
- piCompressor->CompressionFormat = piGZip;
- break;
- }
- case cfStandard:
- {
- IXceedStandardCompressionFormatPtr piStandard;
- piStandard.CreateInstance( CLSID_XceedStandardCompressionFormat );
- piCompressor->CompressionFormat = piStandard;
- break;
- }
- case cfZip3:
- {
- IXceedZip3CompressionFormatPtr piZip3;
- piZip3.CreateInstance( CLSID_XceedZip3CompressionFormat );
- piCompressor->CompressionFormat = piZip3;
- break;
- }
- case cfZLib:
- {
- IXceedZLibCompressionFormatPtr piZLib;
- piZLib.CreateInstance( CLSID_XceedZLibCompressionFormat );
- piCompressor->CompressionFormat = piZLib;
- break;
- }
- // The next three are not compression formats. They are compression
- // methods that can be assigned to the CompressionFormat property of
- // the Xceed Streaming Compression object. In these cases, the
- // resulting compressed streams will have no formatting (header,
- // footer, checksum, ... ).
- case cfBWT:
- {
- IXceedBWTCompressionMethodPtr piBWT;
- piBWT.CreateInstance( CLSID_XceedBWTCompressionMethod );
- piCompressor->CompressionFormat = piBWT;
- break;
- }
- case cfDeflate:
- {
- IXceedDeflateCompressionMethodPtr piDeflate;
- piDeflate.CreateInstance( CLSID_XceedDeflateCompressionMethod );
- piCompressor->CompressionFormat = piDeflate;
- break;
- }
- // Using store as the compression method will produce an output
- // compressed stream identical to the text to compress.
- case cfStore:
- {
- IXceedStoreCompressionMethodPtr piStore;
- piStore.CreateInstance( CLSID_XceedStoreCompressionMethod );
- piCompressor->CompressionFormat = piStore;
- break;
- }
- }
- return true;
- }
- catch( Exception& xErr )
- {
- lstMessages->Items->Add( " PREPARE FORMAT : " + xErr.Message );
- return false;
- }
- catch( ... )
- {
- lstMessages->Items->Add( "An unexpected error occured while preparing the compression format" );
- return false;
- }
- }
-
- //---------------------------------------------------------------------------
- // Perform the actual compression of the source file to a destination file.
- //---------------------------------------------------------------------------
- bool TfrmManager::CompressFile( AnsiString sSourceFilename, AnsiString sCompressedFilename )
- {
- // Initialize the compressor and set the compression format the user chose.
- IXceedStreamingCompressionPtr piCompressor;
- piCompressor.CreateInstance( CLSID_XceedStreamingCompression );
-
- if( PrepareCompressionFormat( piCompressor ) )
- {
- lstMessages->Clear();
-
- try
- {
- // Process the file:
- // - Compress the entire file (no offset or size)
- // - Compress it in a single call (bEndOfData is TRUE)
- // - Overwrite the destination file (bAppend is FALSE)
-
- wchar_t wszSourceFilename[ MAX_PATH ];
- wchar_t wszCompressedFilename[ MAX_PATH ];
-
- sSourceFilename.WideChar( wszSourceFilename, MAX_PATH );
- sCompressedFilename.WideChar( wszCompressedFilename, MAX_PATH );
-
- DWORD dwBytesRead;
- DWORD dwBytesWritten;
-
- piCompressor->ProcessFile( wszSourceFilename, 0, 0, cfpCompress, TRUE,
- wszCompressedFilename, FALSE, &dwBytesRead,
- &dwBytesWritten );
-
- lstMessages->Items->Add( sSourceFilename + " successfully compressed to " +
- sCompressedFilename );
- }
- catch( Exception& xErr )
- {
- lstMessages->Items->Add( " COMPRESS : " + xErr.Message );
- return false;
- }
- catch( ... )
- {
- lstMessages->Items->Add( "An unexpected error occured while compressing" );
- return false;
- }
- }
- return true;
- }
-
- //---------------------------------------------------------------------------
- // Perform the actual decompression of a source file to a destination file
- //---------------------------------------------------------------------------
- bool TfrmManager::DecompressFile( AnsiString sSourceFilename, AnsiString sDecompressedFilename )
- {
- // Initialize the compressor and set the compression format the user chose
- IXceedStreamingCompressionPtr piCompressor;
- piCompressor.CreateInstance( CLSID_XceedStreamingCompression );
-
- if( PrepareCompressionFormat( piCompressor ) )
- {
- lstMessages->Clear();
-
- try
- {
- // Process the file:
- // - Decompress the entire file (no offset or size)
- // - Decompress it in a single call (bEndOfData is TRUE)
- // - Overwrite the destination file (bAppend is FALSE)
-
- wchar_t wszSourceFilename[ MAX_PATH ];
- wchar_t wszDecompressedFilename[ MAX_PATH ];
-
- sSourceFilename.WideChar( wszSourceFilename, MAX_PATH );
- sDecompressedFilename.WideChar( wszDecompressedFilename, MAX_PATH );
-
- DWORD dwBytesRead;
- DWORD dwBytesWritten;
-
- piCompressor->ProcessFile( wszSourceFilename, 0, 0, cfpCompress, TRUE,
- wszDecompressedFilename, FALSE, &dwBytesRead,
- &dwBytesWritten );
-
- lstMessages->Items->Add( sSourceFilename + " successfully decompressed to " +
- sDecompressedFilename );
- }
- catch( Exception& xErr )
- {
- lstMessages->Items->Add( " DECOMPRESS : " + xErr.Message );
- return false;
- }
- catch( ... )
- {
- lstMessages->Items->Add( "An unexpected error occured while decompressing" );
- return false;
- }
- }
- return true;
- }
-
- //---------------------------------------------------------------------------
- // Let the user select the destination filename and path using a file
- // save dialog.
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btBrowseForDestinationClick(TObject *Sender)
- {
- xSaveDialog->Files->Clear();
- xSaveDialog->Title = "Destination File";
- xSaveDialog->Filter = "Compressed (*.bz2;*.gz;*.std;*.zp3;*.zl;*.bwt;*.dfl;"
- "*.sto)|*.bz2;*.gz;*.std;*.zp3;*.zl;*.bwt;*.dfl;*.sto|"
- "All Files (*.*)|*.*";
- xSaveDialog->FilterIndex = 0;
-
- if( xSaveDialog->Execute() )
- edtDestinationFile->Text = xSaveDialog->Files->Text.Trim();
- }
-
- //---------------------------------------------------------------------------
- // Let the user select a source filename and path using a file open dialog.
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btBrowseForSourceClick(TObject *Sender)
- {
- xOpenDialog->Files->Clear();
- xOpenDialog->Title = "Source File";
- xOpenDialog->Filter = "All Files (*.*)|*.*";
- xOpenDialog->FilterIndex = 0;
-
- if( xOpenDialog->Execute() )
- {
- edtSourceFile->Text = xOpenDialog->Files->Text.Trim();
- SetDestinationFilename();
- }
- }
-
- //---------------------------------------------------------------------------
- // Compress the selected source file to the destination file.
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btCompressClick(TObject *Sender)
- {
- if( CompressFile( edtSourceFile->Text, edtDestinationFile->Text ) )
- {
- // If the compression was successful, empty the source and destination
- // edit boxes to simplify subsequent compression/decompression
- edtSourceFile->Text = "";
- edtDestinationFile->Text = "";
- }
- }
-
- //---------------------------------------------------------------------------
- // Decompress the selected source file to the specified destination file.
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::btDecompressClick(TObject *Sender)
- {
- if( DecompressFile( edtSourceFile->Text, edtDestinationFile->Text ) )
- {
- // If the decompression was successful, empty the source and destination
- // edit boxes to simplify subsequent compression/decompression
- edtSourceFile->Text = "";
- edtDestinationFile->Text = "";
- }
- }
-
- //---------------------------------------------------------------------------
- // Initialize the destination file to a default value if the destination
- // edit box is empty.
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::edtSourceFileExit(TObject *Sender)
- {
- SetDestinationFilename();
- }
-
- //---------------------------------------------------------------------------
- // Initialize the destination file to a default value if the destination
- // edit box is empty.
- //---------------------------------------------------------------------------
- void __fastcall TfrmManager::cboCompressionFormatsChange(TObject *Sender)
- {
- SetDestinationFilename();
- }
- //---------------------------------------------------------------------------
-
-