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

  1. /*
  2.   Xceed Streaming Compression Library - Compression Manager sample
  3.   Copyright (c) 2001 Xceed Software Inc.
  4.  
  5.   This sample demonstrates how to compress a file using different kinds of
  6.   compression formats and methods, and how to decompress a compressed file.
  7.   It specifically uses:
  8.     - The ProcessFile method
  9.     - The CompressionFormat property
  10.  
  11.   This file is part of the Xceed Streaming Compression Library sample
  12.   applications. The source code in this file is only intended as a supplement
  13.   to the Xceed Streaming Compression Library's documentation and is provided 
  14.   "as is" without warranty of any kind, either expressed or implied.
  15. */
  16.  
  17. #include <vcl.h>
  18. #pragma hdrstop
  19.  
  20. #include "unManager.h"
  21. //---------------------------------------------------------------------------
  22. #pragma package(smart_init)
  23. #pragma link "XceedStreamingCompressionLib_OCX"
  24. #pragma resource "*.dfm"
  25. TfrmManager *frmManager;
  26.  
  27. //---------------------------------------------------------------------------
  28. // Initialize the form's controls. For the purposes of this example, the
  29. // combo box was filled using C++Builder property menu.
  30. //---------------------------------------------------------------------------
  31. __fastcall TfrmManager::TfrmManager(TComponent* Owner)
  32.   : TForm(Owner)
  33. {
  34.   cboCompressionFormats->ItemIndex = 0;
  35. }
  36.  
  37. //---------------------------------------------------------------------------
  38. // Assign a default value to the destination filename if the destination
  39. // edit box is empty.
  40. //---------------------------------------------------------------------------
  41. void TfrmManager::SetDestinationFilename()
  42. {
  43.   AnsiString sCompressedFilename = edtDestinationFile->Text;
  44.  
  45.   if( sCompressedFilename.Length() == 0 )
  46.   {
  47.     sCompressedFilename = RemoveFileExtension( edtSourceFile->Text );
  48.     if( sCompressedFilename.Length() != 0 )
  49.     {
  50.       switch( ECompressionFormat( cboCompressionFormats->ItemIndex ) )
  51.       {
  52.         case cfBZip2:
  53.         {
  54.           edtDestinationFile->Text = sCompressedFilename + ".bz2";
  55.           break;
  56.         }
  57.         case cfGZip:
  58.         {
  59.           edtDestinationFile->Text = sCompressedFilename + ".gz";
  60.           break;
  61.         }
  62.         case cfStandard:
  63.         {
  64.           edtDestinationFile->Text = sCompressedFilename + ".std";
  65.           break;
  66.         }
  67.         case cfZip3:
  68.         {
  69.           edtDestinationFile->Text = sCompressedFilename + ".zp3";
  70.           break;
  71.         }
  72.         case cfZLib:
  73.         {
  74.           edtDestinationFile->Text = sCompressedFilename + ".zl";
  75.           break;
  76.         }
  77.         case cfBWT:
  78.         {
  79.           edtDestinationFile->Text = sCompressedFilename + ".bwt";
  80.           break;
  81.         }
  82.         case cfDeflate:
  83.         {
  84.           edtDestinationFile->Text = sCompressedFilename + ".dfl";
  85.           break;
  86.         }
  87.         case cfStore:
  88.         {
  89.           edtDestinationFile->Text = sCompressedFilename + ".sto";
  90.           break;
  91.         }
  92.       }
  93.     }
  94.   }
  95. }
  96.  
  97. //---------------------------------------------------------------------------
  98. // Removes the file extension from the provided filename and returns the
  99. // the path and filename without its extension.
  100. //---------------------------------------------------------------------------
  101. AnsiString TfrmManager::RemoveFileExtension( AnsiString sFilename )
  102. {
  103.   int i;
  104.   int nFilenameLen;
  105.   int nLenToRemove;
  106.  
  107.   nFilenameLen = sFilename.Length();
  108.   i = nFilenameLen;
  109.   nLenToRemove = -1;
  110.  
  111.   while( ( i > 0 ) && ( nLenToRemove == -1 ) )
  112.   {
  113.     if( sFilename.SubString( i, 1 ) == "." )
  114.       nLenToRemove = i - 1;
  115.  
  116.     if( sFilename.SubString( i, 1 ) == "\\" )
  117.       nLenToRemove = nFilenameLen;
  118.  
  119.     i = i -1;
  120.   }
  121.  
  122.   if( nLenToRemove == -1 )
  123.     return AnsiString();
  124.   else
  125.     return sFilename.SubString( 1, nLenToRemove );
  126. }
  127.  
  128. //---------------------------------------------------------------------------
  129. // Prepare the compression format according to the user selection.
  130. // Return true if all succeeded.
  131. //---------------------------------------------------------------------------
  132. bool TfrmManager::PrepareCompressionFormat( IXceedStreamingCompressionPtr& piCompressor )
  133. {
  134.   // We instantiate a new compression format, assigning it directly to the
  135.   // CompressionFormat property of the Xceed Streaming Compression object.
  136.   try
  137.   {
  138.     switch( ECompressionFormat( cboCompressionFormats->ItemIndex ) )
  139.     {
  140.       case cfBZip2:
  141.       {
  142.         IXceedBZip2CompressionFormatPtr piBZip2;
  143.         piBZip2.CreateInstance( CLSID_XceedBZip2CompressionFormat );
  144.         piCompressor->CompressionFormat = piBZip2;
  145.         break;
  146.       }
  147.       case cfGZip:
  148.       {
  149.         IXceedGZipCompressionFormatPtr piGZip;
  150.         piGZip.CreateInstance( CLSID_XceedGZipCompressionFormat );
  151.         piCompressor->CompressionFormat = piGZip;
  152.         break;
  153.       }
  154.       case cfStandard:
  155.       {
  156.         IXceedStandardCompressionFormatPtr piStandard;
  157.         piStandard.CreateInstance( CLSID_XceedStandardCompressionFormat );
  158.         piCompressor->CompressionFormat = piStandard;
  159.         break;
  160.       }
  161.       case cfZip3:
  162.       {
  163.         IXceedZip3CompressionFormatPtr piZip3;
  164.         piZip3.CreateInstance( CLSID_XceedZip3CompressionFormat );
  165.         piCompressor->CompressionFormat = piZip3;
  166.         break;
  167.       }
  168.       case cfZLib:
  169.       {
  170.         IXceedZLibCompressionFormatPtr piZLib;
  171.         piZLib.CreateInstance( CLSID_XceedZLibCompressionFormat );
  172.         piCompressor->CompressionFormat = piZLib;
  173.         break;
  174.       }
  175.       // The next three are not compression formats. They are compression
  176.       // methods that can be assigned to the CompressionFormat property of
  177.       // the Xceed Streaming Compression object. In these cases, the
  178.       // resulting compressed streams will have no formatting (header,
  179.       // footer, checksum, ... ).
  180.       case cfBWT:
  181.       {
  182.         IXceedBWTCompressionMethodPtr piBWT;
  183.         piBWT.CreateInstance( CLSID_XceedBWTCompressionMethod );
  184.         piCompressor->CompressionFormat = piBWT;
  185.         break;
  186.       }
  187.       case cfDeflate:
  188.       {
  189.         IXceedDeflateCompressionMethodPtr piDeflate;
  190.         piDeflate.CreateInstance( CLSID_XceedDeflateCompressionMethod );
  191.         piCompressor->CompressionFormat = piDeflate;
  192.         break;
  193.       }
  194.       // Using store as the compression method will produce an output
  195.       // compressed stream identical to the text to compress.
  196.       case cfStore:
  197.       {
  198.         IXceedStoreCompressionMethodPtr piStore;
  199.         piStore.CreateInstance( CLSID_XceedStoreCompressionMethod );
  200.         piCompressor->CompressionFormat = piStore;
  201.         break;
  202.       }
  203.     }
  204.     return true;
  205.   }
  206.   catch( Exception& xErr )
  207.   {
  208.     lstMessages->Items->Add( " PREPARE FORMAT : " + xErr.Message );
  209.     return false;
  210.   }
  211.   catch( ... )
  212.   {
  213.     lstMessages->Items->Add( "An unexpected error occured while preparing the compression format" );
  214.     return false;
  215.   }
  216. }
  217.  
  218. //---------------------------------------------------------------------------
  219. // Perform the actual compression of the source file to a destination file.
  220. //---------------------------------------------------------------------------
  221. bool TfrmManager::CompressFile( AnsiString sSourceFilename, AnsiString sCompressedFilename )
  222. {
  223.   // Initialize the compressor and set the compression format the user chose.
  224.   IXceedStreamingCompressionPtr piCompressor;
  225.   piCompressor.CreateInstance( CLSID_XceedStreamingCompression );
  226.  
  227.   if( PrepareCompressionFormat( piCompressor ) )
  228.   {
  229.     lstMessages->Clear();
  230.  
  231.     try
  232.     {
  233.       // Process the file:
  234.       //  - Compress the entire file (no offset or size)
  235.       //  - Compress it in a single call (bEndOfData is TRUE)
  236.       //  - Overwrite the destination file (bAppend is FALSE)
  237.  
  238.       wchar_t wszSourceFilename[ MAX_PATH ];
  239.       wchar_t wszCompressedFilename[ MAX_PATH ];
  240.  
  241.       sSourceFilename.WideChar( wszSourceFilename, MAX_PATH );
  242.       sCompressedFilename.WideChar( wszCompressedFilename, MAX_PATH );
  243.  
  244.       DWORD dwBytesRead;
  245.       DWORD dwBytesWritten;
  246.  
  247.       piCompressor->ProcessFile( wszSourceFilename, 0, 0, cfpCompress, TRUE,
  248.                                  wszCompressedFilename, FALSE, &dwBytesRead,
  249.                                  &dwBytesWritten );
  250.  
  251.       lstMessages->Items->Add( sSourceFilename + " successfully compressed to " +
  252.                                sCompressedFilename );
  253.     }
  254.     catch( Exception& xErr )
  255.     {
  256.       lstMessages->Items->Add( " COMPRESS : " + xErr.Message );
  257.       return false;
  258.     }
  259.     catch( ... )
  260.     {
  261.       lstMessages->Items->Add( "An unexpected error occured while compressing" );
  262.       return false;
  263.     }
  264.   }
  265.   return true;
  266. }
  267.  
  268. //---------------------------------------------------------------------------
  269. // Perform the actual decompression of a source file to a destination file
  270. //---------------------------------------------------------------------------
  271. bool TfrmManager::DecompressFile( AnsiString sSourceFilename, AnsiString sDecompressedFilename )
  272. {
  273.   // Initialize the compressor and set the compression format the user chose
  274.   IXceedStreamingCompressionPtr piCompressor;
  275.   piCompressor.CreateInstance( CLSID_XceedStreamingCompression );
  276.  
  277.   if( PrepareCompressionFormat( piCompressor ) )
  278.   {
  279.     lstMessages->Clear();
  280.  
  281.     try
  282.     {
  283.       // Process the file:
  284.       //  - Decompress the entire file (no offset or size)
  285.       //  - Decompress it in a single call (bEndOfData is TRUE)
  286.       //  - Overwrite the destination file (bAppend is FALSE)
  287.  
  288.       wchar_t wszSourceFilename[ MAX_PATH ];
  289.       wchar_t wszDecompressedFilename[ MAX_PATH ];
  290.  
  291.       sSourceFilename.WideChar( wszSourceFilename, MAX_PATH );
  292.       sDecompressedFilename.WideChar( wszDecompressedFilename, MAX_PATH );
  293.  
  294.       DWORD dwBytesRead;
  295.       DWORD dwBytesWritten;
  296.  
  297.       piCompressor->ProcessFile( wszSourceFilename, 0, 0, cfpCompress, TRUE,
  298.                                  wszDecompressedFilename, FALSE, &dwBytesRead,
  299.                                  &dwBytesWritten );
  300.  
  301.       lstMessages->Items->Add( sSourceFilename + " successfully decompressed to " +
  302.                                sDecompressedFilename );
  303.     }
  304.     catch( Exception& xErr )
  305.     {
  306.       lstMessages->Items->Add( " DECOMPRESS : " + xErr.Message );
  307.       return false;
  308.     }
  309.     catch( ... )
  310.     {
  311.       lstMessages->Items->Add( "An unexpected error occured while decompressing" );
  312.       return false;
  313.     }
  314.   }
  315.   return true;
  316. }
  317.  
  318. //---------------------------------------------------------------------------
  319. // Let the user select the destination filename and path using a file
  320. // save dialog.
  321. //---------------------------------------------------------------------------
  322. void __fastcall TfrmManager::btBrowseForDestinationClick(TObject *Sender)
  323. {
  324.   xSaveDialog->Files->Clear();
  325.   xSaveDialog->Title = "Destination File";
  326.   xSaveDialog->Filter = "Compressed (*.bz2;*.gz;*.std;*.zp3;*.zl;*.bwt;*.dfl;"
  327.                         "*.sto)|*.bz2;*.gz;*.std;*.zp3;*.zl;*.bwt;*.dfl;*.sto|"
  328.                         "All Files (*.*)|*.*";
  329.   xSaveDialog->FilterIndex = 0;
  330.  
  331.   if( xSaveDialog->Execute() )
  332.     edtDestinationFile->Text = xSaveDialog->Files->Text.Trim();
  333. }
  334.  
  335. //---------------------------------------------------------------------------
  336. // Let the user select a source filename and path using a file open dialog.
  337. //---------------------------------------------------------------------------
  338. void __fastcall TfrmManager::btBrowseForSourceClick(TObject *Sender)
  339. {
  340.   xOpenDialog->Files->Clear();
  341.   xOpenDialog->Title = "Source File";
  342.   xOpenDialog->Filter = "All Files (*.*)|*.*";
  343.   xOpenDialog->FilterIndex = 0;
  344.  
  345.   if( xOpenDialog->Execute() )
  346.   {
  347.     edtSourceFile->Text = xOpenDialog->Files->Text.Trim();
  348.     SetDestinationFilename();
  349.   } 
  350. }
  351.  
  352. //---------------------------------------------------------------------------
  353. // Compress the selected source file to the destination file.
  354. //---------------------------------------------------------------------------
  355. void __fastcall TfrmManager::btCompressClick(TObject *Sender)
  356. {
  357.   if( CompressFile( edtSourceFile->Text, edtDestinationFile->Text ) )
  358.   {
  359.     // If the compression was successful, empty the source and destination
  360.     // edit boxes to simplify subsequent compression/decompression
  361.     edtSourceFile->Text = "";
  362.     edtDestinationFile->Text = "";
  363.   }
  364. }
  365.  
  366. //---------------------------------------------------------------------------
  367. // Decompress the selected source file to the specified destination file.
  368. //---------------------------------------------------------------------------
  369. void __fastcall TfrmManager::btDecompressClick(TObject *Sender)
  370. {
  371.   if( DecompressFile( edtSourceFile->Text, edtDestinationFile->Text ) )
  372.   {
  373.     // If the decompression was successful, empty the source and destination
  374.     // edit boxes to simplify subsequent compression/decompression
  375.     edtSourceFile->Text = "";
  376.     edtDestinationFile->Text = "";
  377.   }
  378. }
  379.  
  380. //---------------------------------------------------------------------------
  381. // Initialize the destination file to a default value if the destination
  382. // edit box is empty.
  383. //---------------------------------------------------------------------------
  384. void __fastcall TfrmManager::edtSourceFileExit(TObject *Sender)
  385. {
  386.   SetDestinationFilename();
  387. }
  388.  
  389. //---------------------------------------------------------------------------
  390. // Initialize the destination file to a default value if the destination
  391. // edit box is empty.
  392. //---------------------------------------------------------------------------
  393. void __fastcall TfrmManager::cboCompressionFormatsChange(TObject *Sender)
  394. {
  395.   SetDestinationFilename();
  396. }
  397. //---------------------------------------------------------------------------
  398.  
  399.