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

  1. /*===========================================================================*/
  2. /* Getting Started sample using The Xceed Zip Compression Library 4          */
  3. /* For C++Builder 4                                                          */
  4. /* Copyright (c) 1999 Xceed Software Inc.                                    */
  5. /*===========================================================================*/
  6.  
  7. #include <vcl.h>
  8. #pragma hdrstop
  9.  
  10. #include "Main.h"
  11.  
  12. #pragma package(smart_init)
  13. #pragma link "XceedZipLib_OCX"
  14. #pragma resource "*.dfm"
  15.  
  16. TfrmMain *frmMain;
  17.  
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // Property hints
  20.  
  21. const char szBasePathHint[] =
  22.   "BasePath property:\n"
  23.   "    This path determines where entries in the FilesToProcess and FilesToExclude\n"
  24.   "    properties are relative to. The base path never appears in the zip file, even if\n"
  25.   "    PreservePaths = True. Only the portion of the path and filename specified in\n"
  26.   "    the FilesToProcess property is actually stored in the zip file. Therefore,\n"
  27.   "    BasePath helps you control what portions of paths are stored in the zip file.\n"
  28.   "    (The BasePath property is irrelevant when you are using absolute paths)";
  29.  
  30. const char szFilesToProcessHint[] =
  31.   "FilesToProcess property:\n"
  32.   "    Multiline string that contains all the filenames and/or file masks to be\n"
  33.   "    processed (zipped, unzipped, etc). If you entered a path in the\n"
  34.   "    BasePath property, all entries with relative paths will be relative to\n"
  35.   "    the specified base path. The pipe character can be used instead of\n"
  36.   "    the linefeed to separate entries for the FilesToProcess property.";
  37.  
  38. const char szFilesToExcludeHint[] =
  39.   "FilesToExclude property:\n"
  40.   "    Multiline string that contains all filenames and/or file masks to exclude\n"
  41.   "    from the files to be processed by the FilesToProcess property. These\n"
  42.   "    entries are also relative to the path specified in the BasePath property\n"
  43.   "    if its not empty.";
  44.  
  45. const char szProcessSubfoldersHint[] =
  46.   "ProcessSubfolders property:\n"
  47.   "    If set to True, the contents of all encoutered subfolders will be processed.";
  48.  
  49. const char szZipFilenameHint[] =
  50.   "ZipFilename property:\n"
  51.   "    The filename of the zip file to work with. When unzipping, this file must\n"
  52.   "    exist. When zipping, if the file exists, it's updated. Otherwise, it is\n"
  53.   "    created. You must enter an absolute path for this property. The\n"
  54.   "    BasePath property does not interfere with the ZipFilename property.";
  55.  
  56. const char szPreservePathsHint[] =
  57.   "PreservePaths property:\n"
  58.   "    If set to True, the zip file will store both the path and the filename of\n"
  59.   "    each file that is being zipped. As usual, the portion of a file's path\n"
  60.   "    that is specified in the BasePath property will not be stored in the\n"
  61.   "    zip file. When PreservePaths is set to False, only filenames (no\n"
  62.   "    paths) are stored.";
  63.  
  64. const char szUseTempFileHint[] =
  65.   "UseTempFile property:\n"
  66.   "    If set to true, all zipping operations will be performed on a temp file\n"
  67.   "    located in the folder specified in the TempFolder property.\n"
  68.   "    Otherwise, the operation is performed directly on the zip file\n"
  69.   "    without using a temp file. You cannot remove files from an existing\n"
  70.   "    zip file, or update files already in an existing zip files without setting\n"
  71.   "    this property to True.";
  72.  
  73. const char szTempFolderHint[] =
  74.   "TempFolder property:\n"
  75.   "    Location of the temp file when the UseTempFile property is set to True.\n"
  76.   "    When you leave this property empty, the Windows default temp\n"
  77.   "    directory is used.";
  78.  
  79. const char szRequiredFileAttributesHint[] =
  80.   "RequiredFileAttributes property:\n"
  81.   "    Bit-field value that specifies all attributes that a file must have in\n"
  82.   "    order to be included in the process.";
  83.  
  84. const char szExcludedFileAttributesHint[] =
  85.   "ExcludedFileAttributes property:\n"
  86.   "    Bit-field value that specifies all attributes that a file must NOT have\n"
  87.   "    in order to be included in the process.";
  88.  
  89. const char szMinDateToProcessHint[] =
  90.   "MinDateToProcess property:\n"
  91.   "    Minimum value of a file's 'Last modifed date' required in order to be\n"
  92.   "    included in the process.";
  93.  
  94. const char szMaxDateToProcessHint[] =
  95.   "MaxDateToProcess property:\n"
  96.   "    Maximum value of a file's 'Last modifed date' required in order to be\n"
  97.   "    included in the process.";
  98.  
  99. const char szMinSizeToProcessHint[] =
  100.   "MinSizeToProcess property:\n"
  101.   "    Minimum file size that a file must have in order to be included in the process.";
  102.  
  103. const char szMaxSizeToProcessHint[] =
  104.   "MaxSizeToProcess property:\n"
  105.   "    Maximum file size that a file must have in order to be included in the process.";
  106.  
  107. const char szUnzipToFolderHint[] =
  108.   "UnzipToFolder property:\n"
  109.   "    Destination folder for files being unzipped. In this sample, the PreservePaths\n"
  110.   "    property is set to True, so if files are stored in the zip file with paths, those\n"
  111.   "    stored paths will be recreated inside the destination folder specified by the\n"
  112.   "    UnzipToFolder property.";
  113.  
  114. const char szSkipIfExistingHint[] =
  115.   "SkipIfExisting property:\n"
  116.   "    If the destination file (located in a zip file that is being updated, or\n"
  117.   "    on disk when a zip file is being unzipped) already exists, and this\n"
  118.   "    property is set to True, then the file won't be overwritten. This has\n"
  119.   "    the effect of only processing files that don't exist in the destination\n"
  120.   "    zip file or unzipping folder.";
  121.  
  122. const char szSkipIfNotExistingHint[] =
  123.   "SkipIfNotExisting property:\n"
  124.   "    Setting this property to True will cause only files that don't already\n"
  125.   "    exist in the destination unzipping location (when unzipping) or the\n"
  126.   "    zip file (when zipping) to be skipped.";
  127.  
  128. const char szSkipIfOlderDateHint[] =
  129.   "SkipIfOlderDate property:\n"
  130.   "    When updating a file (in a zip while zipping, or on disk while unzipping),\n"
  131.   "    the file is skipped if the existing file's 'Last modified date' is greater than\n"
  132.   "    the file being zipped or unzipped.";
  133.  
  134. const char szSkipIfOlderVersionHint[] =
  135.   "SkipIfOlderVersion property:\n"
  136.   "    When updating a file (in a zip while zipping, or on disk while unzipping),\n"
  137.   "    the file is skipped if the existing file's version resource value is greater\n"
  138.   "    than the file being zipped or unzipped.";
  139.  
  140. const char szZipFilenameSfxHint[] =
  141.   "ZipFilename property with Sfx:\n"
  142.   "    The filename of the zip file to work on. When creating or updating\n"
  143.   "    self-extracting zip files, you should enter an executable filename\n"
  144.   "    (use a .EXE extension).";
  145.  
  146. const char szSfxBinaryModuleHint[] =
  147.   "SfxBinaryModule property:\n"
  148.   "    This binary file will be prepended to the zip file, with configuration\n"
  149.   "    data if the binary is one of the Xceed Self-Extractor Module\n"
  150.   "    binaries. If you leave this field empty, a regular (non-sfx) zip file\n"
  151.   "    will be created.";
  152.  
  153. const char szSfxStringsHint[] =
  154.   "SfxStrings property array:\n"
  155.   "    This array contains all the strings displayed by the Xceed Self-Extractor\n"
  156.   "    Module binaries. For example, the 'xssTitle' index contains the title\n"
  157.   "    displayed by all dialog boxes.";
  158.  
  159. const char szSfxMessagesHint[] =
  160.   "SfxMessages property array:\n"
  161.   "    This array contains all messages displayed by the Xceed Self-Extractor Module\n"
  162.   "    binaries. These messages often appear in their own dialog boxes. If a particular\n"
  163.   "    message is left empty, the dialog box won't be displayed. As an example,\n"
  164.   "    leaving this field empty will avoid displaying an introduction message dialog.";
  165.  
  166. ////////////////////////////////////////////////////////////////////////////////
  167. // Method hints
  168.  
  169. const char szPreviewFilesHint[] =
  170.   "PreviewFiles method:\n"
  171.   "    Lets you scan the disk for files that would be zipped with the current property\n"
  172.   "    settings. A PreviewingFile event is triggered for each file that matches an\n"
  173.   "    entry in the FilesToProcess property. Set this method's parameter to True to\n"
  174.   "    have the library calculate the compressed size of the previewed files.";
  175.  
  176. const char szListZipContentsHint[] =
  177.   "ListZipContents method:\n"
  178.   "    Lets you view the zip file's contents. Due to the fact that the FilesToProcess\n"
  179.   "    property and the other filtering properties affect the ListZipContents method,\n"
  180.   "    you can use it to preview which files would be unzipped by the Unzip method\n"
  181.   "    if it were called with the current property settings. A ListingFile event is\n"
  182.   "    triggered for each file in the zip file that is listed.";
  183.  
  184. const char szZipHint[] =
  185.   "Zip method:\n"
  186.   "    Lets you zip files. Only files that match all the entries in the FilesToProcess\n"
  187.   "    and filtering properties will be processed. For each file that matches the\n"
  188.   "    FilesToProcess property, the ZipPreprocessingFile event is triggered. That\n"
  189.   "    event provides you with the chance to change the inclusion state of a file,\n"
  190.   "    or to change its information before it is stored in the zip file.";
  191.  
  192. const char szUnzipHint[] =
  193.   "Unzip method:\n"
  194.   "    Lets you unzip files. Only files that match all the entries in the FilesToProcess\n"
  195.   "    and filtering properties will be processed. For each file that matches the\n"
  196.   "    FilesToProcess property, the UnzipPreprocessingFile event is triggered. That\n"
  197.   "    event provides you with the chance to change the inclusion state of a file, or\n"
  198.   "    to change its information before it is written to the destination unzipping folder.";
  199.  
  200. const char szZipSfxHint[] =
  201.   "Zip method with Sfx:\n"
  202.   "    It's just like creating a regular (non-sfx) zip file, but if you enter a value\n"
  203.   "    for the SfxBinaryModule property, the binary (or any file for that matter)\n"
  204.   "    is prepended to the zip file. The zip file is now self-extracting because\n"
  205.   "    the binary knows how to unzip the rest of the data after itself.\n"
  206.   "    Furthermore, if the library recognises an Xceed binary, it will add\n"
  207.   "    config data to the binary so that the self-extracting zip file can display\n"
  208.   "    intro messages and have custom behavior.";
  209.  
  210. //
  211. // Constructor
  212. //
  213.  
  214. __fastcall TfrmMain::TfrmMain( TComponent* Owner )
  215.  : TForm(Owner)
  216. {
  217.   // We update hints for better look.
  218.   // We cannot put linefeeds in property editor!
  219.   UpdateFieldHints();
  220.  
  221.   // We fill attribute lists
  222.   FillAttributeList( lstPRequiredFileAttributes );
  223.   FillAttributeList( lstPExcludedFileAttributes );
  224.  
  225.   // We set their initial state
  226.   SetSelectedAttributes( lstPRequiredFileAttributes,
  227.                          xZip->RequiredFileAttributes );
  228.   SetSelectedAttributes( lstPExcludedFileAttributes,
  229.                          xZip->ExcludedFileAttributes );
  230. }
  231.  
  232. //
  233. // Utility methods
  234. //
  235.  
  236. void TfrmMain::UpdateFieldHints( void )
  237. {
  238.   // Previewing tab
  239.   edtPFilesToProcess->Hint          = szFilesToProcessHint;
  240.   edtPFilesToExclude->Hint          = szFilesToExcludeHint;
  241.   chkPProcessSubfolders->Hint       = szProcessSubfoldersHint;
  242.   lstPRequiredFileAttributes->Hint  = szRequiredFileAttributesHint;
  243.   lstPExcludedFileAttributes->Hint  = szExcludedFileAttributesHint;
  244.   btPreviewFiles->Hint              = szPreviewFilesHint;
  245.  
  246.   // Zipping tab
  247.   edtZZipFilename->Hint       = szZipFilenameHint;
  248.   edtZBasePath->Hint          = szBasePathHint;
  249.   edtZFilesToProcess->Hint    = szFilesToProcessHint;
  250.   edtZFilesToExclude->Hint    = szFilesToExcludeHint;
  251.   chkZProcessSubfolders->Hint = szProcessSubfoldersHint;
  252.   chkZPreservePaths->Hint     = szPreservePathsHint;
  253.   chkZUseTempFile->Hint       = szUseTempFileHint;
  254.   edtZTempFolder->Hint        = szTempFolderHint;
  255.   btZip->Hint                 = szZipHint;
  256.  
  257.   // Listing tab
  258.   edtLZipFilename->Hint       = szZipFilenameHint;
  259.   dtLMinDateToProcess->Hint   = szMinDateToProcessHint;
  260.   dtLMaxDateToProcess->Hint   = szMaxDateToProcessHint;
  261.   edtLMinSizeToProcess->Hint  = szMinSizeToProcessHint;
  262.   edtLMaxSizeToProcess->Hint  = szMaxSizeToProcessHint;
  263.   btList->Hint                = szListZipContentsHint;
  264.  
  265.   // Unzipping tab
  266.   edtUZipFilename->Hint         = szZipFilenameHint;
  267.   edtUUnzipToFolder->Hint       = szUnzipToFolderHint;
  268.   edtUFilesToProcess->Hint      = szFilesToProcessHint;
  269.   edtUFilesToExclude->Hint      = szFilesToExcludeHint;
  270.   chkUSkipIfExisting->Hint      = szSkipIfExistingHint;
  271.   chkUSkipIfNotExisting->Hint   = szSkipIfNotExistingHint;
  272.   chkUSkipIfOlderDate->Hint     = szSkipIfOlderDateHint;
  273.   chkUSkipIfOlderVersion->Hint  = szSkipIfOlderVersionHint;
  274.   btUnzip->Hint                 = szUnzipHint;
  275.  
  276.   // Zipping Sfx tab
  277.   edtSZipFilename->Hint     = szZipFilenameSfxHint;
  278.   edtSFilesToProcess->Hint  = szFilesToProcessHint;
  279.   edtSSfxBinaryModule->Hint = szSfxBinaryModuleHint;
  280.   edtSTitle->Hint           = szSfxStringsHint;
  281.   edtSIntro->Hint           = szSfxMessagesHint;
  282.   btZipSfx->Hint            = szZipSfxHint;
  283. }
  284.  
  285. void TfrmMain::FillAttributeList( TCheckListBox* pList )
  286. {
  287.   pList->Items->Clear();
  288.   pList->Items->AddObject( "Archive",       ( TObject* ) xfaArchive );
  289.   pList->Items->AddObject( "Read-only",     ( TObject* ) xfaReadOnly );
  290.   pList->Items->AddObject( "Hidden",        ( TObject* ) xfaHidden );
  291.   pList->Items->AddObject( "System",        ( TObject* ) xfaSystem );
  292.   pList->Items->AddObject( "Volume label",  ( TObject* ) xfaVolume );
  293.   pList->Items->AddObject( "Folder",        ( TObject* ) xfaFolder );
  294.   pList->Items->AddObject( "Compressed",    ( TObject* ) xfaCompressed );
  295. }
  296.  
  297. xcdFileAttributes TfrmMain::GetSelectedAttributes( TCheckListBox* pList )
  298. {
  299.   xcdFileAttributes xAttributes = xfaNone;
  300.  
  301.   for( int i = 0; i < pList->Items->Count; i++ )
  302.   {
  303.     if( pList->Checked[ i ] )
  304.     {
  305.       xAttributes += ( xcdFileAttributes ) pList->Items->Objects[ i ];
  306.     }
  307.   }
  308.  
  309.   return xAttributes;
  310. }
  311.  
  312. void TfrmMain::SetSelectedAttributes( TCheckListBox* pList,
  313.                                       xcdFileAttributes xAttribs )
  314. {
  315.   for( int i = 0; i < pList->Items->Count; i++ )
  316.   {
  317.     pList->Checked[ i ] = ( ( ( xcdFileAttributes ) pList->Items->Objects[ i ] ) & xAttribs );
  318.   }
  319. }
  320.  
  321. void TfrmMain::ResetDefaultXceedZipProperties( TXceedZipProxy* pZip )
  322. {
  323.   pZip->BasePath            = "";
  324.   pZip->CompressionLevel    = xclHigh;
  325.   pZip->EncryptionPassword  = "";
  326.   pZip->RequiredFileAttributes  = xfaNone;
  327.   pZip->ExcludedFileAttributes  = xfaNone;
  328.   pZip->FilesToProcess      = "";
  329.   pZip->FilesToExclude      = "";
  330.   pZip->MinDateToProcess    = EncodeDate( 1900, 01, 01 );
  331.   pZip->MaxDateToProcess    = EncodeDate( 9999, 12, 31 );
  332.   pZip->MinSizeToProcess    = 0;
  333.   pZip->MaxSizeToProcess    = 0;      // Zero means no upper limit
  334.   pZip->SplitSize           = 0;      // Zero means no split
  335.   pZip->PreservePaths       = false;
  336.   pZip->ProcessSubfolders   = false;
  337.   pZip->SkipIfExisting      = false;
  338.   pZip->SkipIfNotExisting   = false;
  339.   pZip->SkipIfOlderDate     = false;
  340.   pZip->SkipIfOlderVersion  = false;
  341.   pZip->TempFolder          = "";     // Empty means default Windows temp folder
  342.   pZip->UseTempFile         = true;
  343.   pZip->UnzipToFolder       = "";
  344.   pZip->ZipFilename         = "";
  345.   pZip->SpanMultipleDisks   = xdsRemovableDrivesOnly;
  346.   pZip->ExtraHeaders        = 0;
  347.  
  348.   // General SFX stuff
  349.   pZip->SfxBinaryModule = "";
  350.   pZip->SfxResetButtons();
  351.   pZip->SfxResetMessages();
  352.   pZip->SfxResetStrings();
  353. }
  354.  
  355. //
  356. // PreviewFiles method:
  357. //  This method let's you list all files (through the "OnPreviewingFile" event)
  358. //  that would be processed by the Zip method with the same property values.
  359. //
  360. void __fastcall TfrmMain::btPreviewFilesClick(TObject *Sender)
  361. {
  362.   // To be sure other tabs settings do not interfere with this example, we
  363.   // start by resetting XceedZip properties to default values
  364.   ResetDefaultXceedZipProperties( xZip );
  365.  
  366.   // Clear the result list before starting
  367.   edtResults->Clear();
  368.   edtResults->Lines->Add( "PreviewFiles example:" );
  369.  
  370.   // Set properties displayed on this tab
  371.   xZip->FilesToProcess          = edtPFilesToProcess->Text;   // required
  372.   xZip->FilesToExclude          = edtPFilesToExclude->Text;
  373.   xZip->ProcessSubfolders       = chkPProcessSubfolders->Checked;
  374.   xZip->RequiredFileAttributes  = GetSelectedAttributes( lstPRequiredFileAttributes );
  375.   xZip->ExcludedFileAttributes  = GetSelectedAttributes( lstPExcludedFileAttributes );
  376.  
  377.   // Launch the previewing
  378.   xcdError xErr = xZip->PreviewFiles( false );
  379.  
  380.   // We can check the results here or in the ProcessCompleted event
  381. }
  382.  
  383. //
  384. // Zip method:
  385. //  The files that are matched by the filtering properties will be added to the
  386. //  zip file specified by the ZipFilename property.
  387. //
  388. void __fastcall TfrmMain::btZipClick(TObject *Sender)
  389. {
  390.   // To be sure other tabs settings do not interfere with this example, we
  391.   // start by resetting XceedZip properties to default values
  392.   ResetDefaultXceedZipProperties( xZip );
  393.  
  394.   // Clear the result list before starting
  395.   edtResults->Lines->Clear();
  396.   edtResults->Lines->Add( "Zip example:" );
  397.  
  398.   // Set properties displayed on this tab
  399.   xZip->ZipFilename       = edtZZipFilename->Text;       // required
  400.   xZip->BasePath          = edtZBasePath->Text;
  401.   xZip->FilesToProcess    = edtZFilesToProcess->Text;    // required
  402.   xZip->FilesToExclude    = edtZFilesToExclude->Text;
  403.   xZip->ProcessSubfolders = chkZProcessSubfolders->Checked;
  404.   xZip->PreservePaths     = chkZPreservePaths->Checked;
  405.   xZip->UseTempFile       = chkZUseTempFile->Checked;
  406.   xZip->TempFolder        = edtZTempFolder->Text;
  407.  
  408.   // Launch the zipping
  409.   xcdError xErr = xZip->Zip();
  410.  
  411.   // We can check the results here or in the ProcessCompleted event
  412. }
  413.  
  414. //
  415. // ListZipContents method:
  416. //  Lists (through the OnListingFile event) the files contained in the zip file
  417. //  specified by the ZipFilename property. You can set the filtering propeties
  418. //  to list only particular files, or leave the FilesToProcess empty to list
  419. //  everything.
  420. //
  421. void __fastcall TfrmMain::btListClick(TObject *Sender)
  422. {
  423.   // To be sure other tabs settings do not interfere with this example, we
  424.   // start by resetting XceedZip properties to default values
  425.   ResetDefaultXceedZipProperties( xZip );
  426.  
  427.   // Clear the result list before starting
  428.   edtResults->Lines->Clear();
  429.   edtResults->Lines->Add( "ListZipContents example:" );
  430.  
  431.   // Set properties displayed on this tab
  432.   xZip->ZipFilename       = edtLZipFilename->Text;           // required
  433.   xZip->MinDateToProcess  = dtLMinDateToProcess->Date;
  434.   xZip->MaxDateToProcess  = dtLMaxDateToProcess->Date;
  435.   xZip->MinSizeToProcess  = StrToIntDef( edtLMinSizeToProcess->Text, 0 );
  436.   xZip->MaxSizeToProcess  = StrToIntDef( edtLMaxSizeToProcess->Text, 0 );
  437.  
  438.   // Launch the listing
  439.   xcdError xErr = xZip->ListZipContents();
  440.  
  441.   // We can check the results here or in the ProcessCompleted event
  442. }
  443.  
  444. //
  445. // Unzip method:
  446. //  Unzips from the specified zip file (ZipFilename property) files that match
  447. //  the filtering properties.
  448. //
  449. void __fastcall TfrmMain::btUnzipClick(TObject *Sender)
  450. {
  451.   // To be sure other tabs settings do not interfere with this example, we
  452.   // start by resetting XceedZip properties to default values
  453.   ResetDefaultXceedZipProperties( xZip );
  454.  
  455.   // Clear the result list before starting
  456.   edtResults->Lines->Clear();
  457.   edtResults->Lines->Add( "Unzip example:" );
  458.  
  459.   // Set properties displayed on this tab
  460.   xZip->ZipFilename         = edtUZipFilename->Text;      // required
  461.   xZip->UnzipToFolder       = edtUUnzipToFolder->Text;    // required
  462.   xZip->FilesToProcess      = edtUFilesToProcess->Text;   // required
  463.   xZip->FilesToExclude      = edtUFilesToExclude->Text;
  464.   xZip->SkipIfExisting      = chkUSkipIfExisting->Checked;
  465.   xZip->SkipIfNotExisting   = chkUSkipIfNotExisting->Checked;
  466.   xZip->SkipIfOlderDate     = chkUSkipIfOlderDate->Checked;
  467.   xZip->SkipIfOlderVersion  = chkUSkipIfOlderVersion->Checked;
  468.  
  469.   // Launch the unzipping
  470.   xcdError xErr = xZip->Unzip();
  471.  
  472.   // We can check the results here or in the ProcessCompleted event
  473. }
  474.  
  475. //
  476. // Zip method:
  477. //  When SfxBinaryModule contains a filename, then the resulting zip file is a
  478. //  self-extracting zip file that uses this binary module as a binary header.
  479. //  But appart from that (and changing the Sfx options with the Sfx properties),
  480. //  creating a self-extracting zip file is just like creating a normal zip file.
  481. //
  482. void __fastcall TfrmMain::btZipSfxClick(TObject *Sender)
  483. {
  484.   // To be sure other tabs settings do not interfere with this example, we
  485.   // start by resetting XceedZip properties to default values
  486.   ResetDefaultXceedZipProperties( xZip );
  487.  
  488.   // Clear the result list before starting
  489.   edtResults->Lines->Clear();
  490.   edtResults->Lines->Add( "Zip Sfx example:" );
  491.  
  492.   // Set properties displayed on this tab
  493.   xZip->ZipFilename             = edtSZipFilename->Text;      // required
  494.   xZip->FilesToProcess          = edtSFilesToProcess->Text;   // required
  495.   xZip->SfxBinaryModule         = edtSSfxBinaryModule->Text;  // required to make an EXE
  496.  
  497.   // IMPORTANT: The wrapper classes C++Builder creates around the Xceed Zip
  498.   //            ActiveX does not expose property arrays, like SfxButtons,
  499.   //            SfxMessages and SfxStrings. But it creates methods of the
  500.   //            form "Set_SfxButtons, Get_SfxButtons, etc". They work very
  501.   //            well, but strange enough: they are declared in the "private"
  502.   //            section of the class. In order for these two lines to compile,
  503.   //            you must open "XceedZipLib_TLB.h" and move the six methods
  504.   //            to the public section. The only catch is that the string
  505.   //            parameter is expected to be a BSTR, not a WideString. Just
  506.   //            typecast your AnsiString to a WideString!
  507.   xZip->SfxStrings[ xssTitle ] = WideString( edtSTitle->Text ).c_bstr();
  508.   xZip->SfxMessages[ xsmIntro ] = WideString( edtSIntro->Text ).c_bstr();
  509.  
  510.   // Launch the zipping
  511.   xcdError xErr = xZip->Zip();
  512.  
  513.   // We can check the results here or in the ProcessCompleted event
  514. }
  515.  
  516. //
  517. // Some of the Xceed Zip events:
  518. //
  519.  
  520. //
  521. // OnFileStatus event:
  522. //  Triggered during processing of a file, at every 32k. Gives a status on each
  523. //  file, one at a time, while it is being processed. Perfect for a file by file
  524. //  progress bar. You can use the CurrentOperation property to recall what this
  525. //  XceedZip instance is doing and display a proper status message.
  526. //
  527. void __fastcall TfrmMain::xZipFileStatus(TObject *Sender, BSTR sFilename,
  528.       long lSize, long lCompressedSize, long lBytesProcessed,
  529.       short nBytesPercent, short nCompressionRatio,
  530.       TOLEBOOL bFileCompleted)
  531. {
  532.   if( lBytesProcessed == 0 )
  533.   {
  534.     switch( xZip->CurrentOperation )
  535.     {
  536.       case xcoZipping:
  537.         edtResults->Lines->Add( WideString( "Zipping " ) + sFilename );
  538.         break;
  539.  
  540.       case xcoUnzipping:
  541.         edtResults->Lines->Add( WideString( "unzipping " ) + sFilename );
  542.         break;
  543.     }
  544.   }
  545.  
  546.   barFile->Position = nBytesPercent;
  547. }
  548.  
  549. //
  550. // OnGlobalStatus event:
  551. //  Triggered during processing of files, at every 32k. Gives general status of
  552. //  the complete process. Perfect for a general progress bar.
  553. //
  554. void __fastcall TfrmMain::xZipGlobalStatus(TObject *Sender,
  555.       long lFilesTotal, long lFilesProcessed, long lFilesSkipped,
  556.       short nFilesPercent, long lBytesTotal, long lBytesProcessed,
  557.       long lBytesSkipped, short nBytesPercent, long lBytesOutput,
  558.       short nCompressionRatio)
  559. {
  560.   barGlobal->Position = nBytesPercent;
  561. }
  562.  
  563. //
  564. // OnListingFile event:
  565. //  Triggered while listing the contents of a zip file with the ListZipContents
  566. //  method.
  567. //
  568. void __fastcall TfrmMain::xZipListingFile(TObject *Sender, BSTR sFilename,
  569.       BSTR sComment, long lSize, long lCompressedSize,
  570.       short nCompressionRatio, xcdFileAttributes xAttributes, long lCRC,
  571.       DATE dtLastModified, DATE dtLastAccessed, DATE dtCreated,
  572.       xcdCompressionMethod xMethod, TOLEBOOL bEncrypted, long lDiskNumber,
  573.       TOLEBOOL bExcluded, xcdSkippingReason xReason)
  574. {
  575.   if( bExcluded )
  576.   {
  577.     edtResults->Lines->Add( WideString( "Excluding " ) + sFilename + " (" +
  578.                             xZip->GetErrorDescription( xvtSkippingReason, xReason ) + ")" );
  579.   }
  580.   else
  581.   {
  582.     edtResults->Lines->Add( WideString( "Including " ) + sFilename );
  583.   }
  584. }
  585.  
  586. //
  587. // OnPreviewingFile event:
  588. //  Triggered when previewing files on disk with the PreviewFiles method.
  589. //
  590. void __fastcall TfrmMain::xZipPreviewingFile(TObject *Sender,
  591.       BSTR sFilename, BSTR sSourceFilename, long lSize,
  592.       xcdFileAttributes xAttributes, DATE dtLastModified,
  593.       DATE dtLastAccessed, DATE dtCreated, TOLEBOOL bExcluded,
  594.       xcdSkippingReason xReason)
  595. {
  596.   if( bExcluded )
  597.   {
  598.     edtResults->Lines->Add( WideString( "Excluding " ) + sFilename + " (" +
  599.                             xZip->GetErrorDescription( xvtSkippingReason, xReason ) + ")" );
  600.   }
  601.   else
  602.   {
  603.     edtResults->Lines->Add( WideString( "Including " ) + sFilename );
  604.   }
  605. }
  606.  
  607. //
  608. // OnProcessCompleted event:
  609. //  Triggered when any process submitted terminates. It is highly useful when
  610. //  processing files with BackgroundProcessing set to true. This is how you
  611. //  know the process completed!
  612. //
  613. void __fastcall TfrmMain::xZipProcessCompleted(TObject *Sender,
  614.       long lFilesTotal, long lFilesProcessed, long lFilesSkipped,
  615.       long lBytesTotal, long lBytesProcessed, long lBytesSkipped,
  616.       long lBytesOutput, short nCompressionRatio, xcdError xResult)
  617. {
  618.   // Display the error number and default message
  619.   edtResults->Lines->Add( WideString( "Process completed with error code " ) +
  620.                           IntToStr( ( int )xResult ) );
  621.   edtResults->Lines->Add( WideString( "-=> " ) +
  622.                           xZip->GetErrorDescription( xvtError, xResult ) );
  623.  
  624.   // Display statistics
  625.   switch( xZip->CurrentOperation )
  626.   {
  627.     case xcoZipping:
  628.     case xcoUnzipping:
  629.     case xcoRemoving:
  630.       edtResults->Lines->Add( IntToStr( ( int )lFilesProcessed ) + " file(s) processed for " +
  631.                               IntToStr( ( int )lBytesProcessed ) + " byte(s)" );
  632.       edtResults->Lines->Add( IntToStr( ( int )lFilesSkipped ) + " file(s) skipped for " +
  633.                               IntToStr( ( int )lBytesSkipped ) + " byte(s)" );
  634.       break;
  635.  
  636.     case xcoListing:
  637.       edtResults->Lines->Add( IntToStr( ( int )lFilesTotal ) + " file(s) listed" );
  638.       break;
  639.  
  640.     case xcoPreviewing:
  641.       edtResults->Lines->Add( IntToStr( ( int )lFilesTotal ) + " file(s) previewed" );
  642.       break;
  643.   }
  644. }
  645.  
  646. //
  647. // OnSkippingFile event:
  648. //  Triggered during any processing, when a file is excluded because of the
  649. //  filtering properties (0 < xReason < 100), or because an error prevented this
  650. //  file from being processed (xReason >= 100).
  651. //
  652. void __fastcall TfrmMain::xZipSkippingFile(TObject *Sender, BSTR sFilename,
  653.       BSTR sComment, BSTR sFilenameOnDisk, long lSize,
  654.       long lCompressedSize, xcdFileAttributes xAttributes, long lCRC,
  655.       DATE dtLastModified, DATE dtLastAccessed, DATE dtCreated,
  656.       xcdCompressionMethod xMethod, TOLEBOOL bEncrypted,
  657.       xcdSkippingReason xReason)
  658. {
  659.   edtResults->Lines->Add( WideString( "Skipping " ) + sFilename + " (" +
  660.                           xZip->GetErrorDescription( xvtSkippingReason, xReason ) + ")" );
  661. }
  662.  
  663. //
  664. // OnWarning event:
  665. //  Triggered during processing when a recoverable error is encountered.
  666. //
  667. void __fastcall TfrmMain::xZipWarning(TObject *Sender, BSTR sFilename,
  668.       xcdWarning xWarning)
  669. {
  670.   edtResults->Lines->Add( WideString( "Warning for " ) + sFilename + " (" +
  671.                           xZip->GetErrorDescription( xvtWarning, xWarning ) + ")" );
  672. }
  673.  
  674. //
  675. // OnZipContentsStatus event:
  676. //  Triggered while reading the contents of an existing zip file. This happens
  677. //  when unzipping, adding files to an existing zip file, removing files,
  678. //  testing a zip file, converting a zip file, or getting the zip file's info.
  679. //  This event is useful when dealing with very large zip files, and you want
  680. //  to display a progress status while the zip file is read.
  681. //
  682. void __fastcall TfrmMain::xZipZipContentsStatus(TObject *Sender,
  683.       long lFilesRead, long lFilesTotal, short nFilesPercent)
  684. {
  685.   // We're not doing anything special here!
  686. }
  687.  
  688. //
  689. // OnInsertDisk event:
  690. //  Triggered when processing a spanned zip file, or when creating a spanned zip
  691. //  file and the current disk is filled. When nDiskNumber is zero, it means the
  692. //  last disk of the set is required, in order to read the zip file's list of
  693. //  files.
  694. //
  695. void __fastcall TfrmMain::xZipInsertDisk(TObject *Sender, long lDiskNumber,
  696.       TOLEBOOL *bDiskInserted)
  697. {
  698.   // When nDiskNumber is 0, this means the last disk of the set is required
  699.   int nAnswer;
  700.  
  701.   if( lDiskNumber == 0 )
  702.   {
  703.     nAnswer = MessageDlg( "This file is part of a multidisk zip file. Please "
  704.                           "insert the last disk of the set.", mtInformation,
  705.                           TMsgDlgButtons() << mbOK << mbCancel, 0 );
  706.   }
  707.   else
  708.   {
  709.     nAnswer = MessageDlg( AnsiString( "Please insert disk #" ) +
  710.                           IntToStr( ( int )lDiskNumber ) + ".", mtInformation,
  711.                           TMsgDlgButtons() << mbOK << mbCancel, 0 );
  712.   }
  713.  
  714.   if( nAnswer == mrOk )
  715.   {
  716.     // CBuilder is wrong, the bDiskInserted parameter os a VARIANT_BOOL* NOT
  717.     // TOLEBOOL
  718.     * ( VARIANT_BOOL* ) bDiskInserted = VARIANT_TRUE;
  719.   }
  720. }
  721. //---------------------------------------------------------------------------
  722. //
  723. // OnReplacingFile event:
  724. //  Triggered while zipping or unzipping, when a file is about to be replaced.
  725. //  The default value of the bReplaceFile parameter is true, so you do not need
  726. //  to implement anything if you always want to replace files.
  727. //
  728.  
  729. void __fastcall TfrmMain::xZipReplacingFile(TObject *Sender,
  730.       BSTR sFilename, BSTR sComment, long lSize,
  731.       xcdFileAttributes xAttributes, DATE dtLastModified,
  732.       DATE dtLastAccessed, DATE dtCreated, BSTR sOrigFilename,
  733.       long lOrigSize, xcdFileAttributes xOrigAttributes,
  734.       DATE dtOrigLastModified, DATE dtOrigLastAccessed, DATE dtOrigCreated,
  735.       TOLEBOOL *bReplaceFile)
  736. {
  737.   edtResults->Lines->Add( WideString( "Replacing " ) + sOrigFilename );
  738. }
  739. //---------------------------------------------------------------------------
  740.  
  741.