home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / zkuste / delphi / komprese / zip / DELZIP12.ZIP / CDEMO1.ZIP / UNIT1.CPP < prev    next >
C/C++ Source or Header  |  1997-09-28  |  5KB  |  153 lines

  1. // Unit1.cpp   Zip Demo 1  for C++ Builder    by Eric W. Engler
  2. //---------------------------------------------------------------------------
  3. #include <vcl\vcl.h>
  4. #include <sharemem.hpp>
  5. #pragma hdrstop
  6.  
  7. // In C++ Builder, your own include files should be below the system
  8. // and VCL include files.
  9. #include "Unit1.h"
  10.  
  11. //---------------------------------------------------------------------------
  12. #pragma link "ZipMstr"
  13. #pragma resource "*.dfm"
  14.  
  15. TForm1 *Form1;
  16.  
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19.     : TForm(Owner)
  20. {
  21. }
  22.  
  23. //---------------------------------------------------------------------------
  24. void __fastcall TForm1::ZipButClick(TObject *Sender)
  25. {
  26.    Memo1->Clear();
  27.    Memo1->Lines->Add("Adding test*.txt to test1.zip");
  28.    ZipMaster1->ZipFilename="test1.zip";
  29.    ZipMaster1->Verbose=TRUE;
  30.  
  31.    // Add as many filespecs as we want to:
  32.    // (MS-DOS Wildcards are OK)
  33.    ZipMaster1->FSpecArgs->Add("test*.txt");
  34.    // Set the AddOptions; these also appear in Property Inspector:
  35.    ZipMaster1->AddOptions << AddZipTime << AddHiddenFiles;
  36.    ZipMaster1->Add();
  37. }
  38.  
  39. //---------------------------------------------------------------------------
  40. void __fastcall TForm1::ZipMaster1Message(TObject *Sender, int ErrCode,
  41.     AnsiString Message)
  42. {
  43.    // Add a message from the DLLs to the memo box
  44.    Memo1->Lines->Add(Message);
  45. }
  46.  
  47. //---------------------------------------------------------------------------
  48. void __fastcall TForm1::Button2Click(TObject *Sender)
  49. {
  50.    Close();  // exit program
  51. }
  52.  
  53. //---------------------------------------------------------------------------
  54. void __fastcall TForm1::UnzButClick(TObject *Sender)
  55. {
  56.    Memo1->Clear();
  57.    if (!FileExists("test1.zip"))
  58.    {
  59.       ShowMessage("Error - test1.zip not found; do a Zip Test first");
  60.       return;
  61.    }
  62.    Memo1->Lines->Add("Extracting from test1.zip");
  63.    ZipMaster1->ZipFilename="test1.zip";
  64.    ZipMaster1->Verbose=TRUE;
  65.  
  66.    // We can specify as many filespecs as we want to extract:
  67.    // (MS-DOS Wildcards are OK)
  68.    ZipMaster1->FSpecArgs->Add("test*.txt");
  69.  
  70.    // Set the ExtrOptions; these also appear in Property Inspector:
  71.    ZipMaster1->ExtrOptions << ExtrOverWrite;
  72.    ZipMaster1->Extract();
  73. }
  74.  
  75. //---------------------------------------------------------------------------
  76. void __fastcall TForm1::VersButClick(TObject *Sender)
  77. {
  78.    char outbuf[80];
  79.  
  80.    // When using a C string for output, I can use sprintf to format, and
  81.    // MessageBox to display.
  82.    // I could have used an AnsiString, and it's Format method, and
  83.    // ShowMessage to display.
  84.    sprintf(outbuf,"ZIPDLL.DLL = %d,  UNZDLL.DLL = %d",
  85.        ZipMaster1->ZipVers, ZipMaster1->UnzVers);
  86.    MessageBox(Handle, outbuf, "DLL Versions", MB_OK + MB_ICONINFORMATION);
  87. }
  88.  
  89. //---------------------------------------------------------------------------
  90. void __fastcall TForm1::ListButClick(TObject *Sender)
  91. {
  92.    int i;
  93.    ZipDirEntry *dirp;
  94.    char fname[80], datetime[20], outbuf[120];
  95.  
  96.    Memo1->Clear();
  97.    if (!FileExists("test1.zip"))
  98.    {
  99.       ShowMessage("Error - test1.zip not found; do a Zip Test first");
  100.       return;
  101.    }
  102.    Memo1->Lines->Add("Listing test1.zip");
  103.    // The ZipContents TList is automatically filled with the contents of the
  104.    // zipfile when the filename is assigned.
  105.    ZipMaster1->ZipFilename="test1.zip";
  106.  
  107.    if (ZipMaster1->Count == 0)
  108.       return;
  109.  
  110.    // I normally prefer to use TSortGrid for showing the user this info.
  111.    // In this demo, I'm just using a plain memo so I can demonstrate how
  112.    // to manipulate the strings and integers for display.  I want to
  113.    // emphasize how to use C with the VCL components.
  114.  
  115.    // Print some column headers
  116.    Memo1->Lines->Add("Filename         Cmp byt  Unc byt      Date/Time");
  117.    for (i=0; i < ZipMaster1->Count; i++)
  118.    {
  119.       // ZipContnets is a TList, which contains an array of pointers
  120.       // to ZipDirEntry structs.
  121.       dirp=(ZipDirEntry *)(ZipMaster1->ZipContents->Items[i]);
  122.  
  123.       // In this demo, I'm going to convert the AnsiStrings to C strings
  124.       // in order to use sprintf for the output formatting.  It would have
  125.       // also been possible to use the Format method of AnsiString to do
  126.       // output formatting.
  127.       strcpy(fname, dirp->FileName.c_str());
  128.       strcpy(datetime,
  129.          FormatDateTime("ddddd  t",FileDateToDateTime(dirp->DateTime)).c_str());
  130.       sprintf(outbuf, "%-16s %6d   %6d    %-16s",
  131.          fname, dirp->CompressedSize, dirp->UncompressedSize, datetime);
  132.       Memo1->Lines->Add(outbuf);
  133.    } // end for
  134. }
  135.  
  136. //---------------------------------------------------------------------------
  137. void __fastcall TForm1::FormCreate(TObject *Sender)
  138. {
  139.    // The easiest way to handle DLL loading and unloading is to load them in
  140.    // the form's OnCreate event handler, and unload them in the form's
  141.    // OnDestroy event handler.
  142.    ZipMaster1->Load_Zip_Dll();
  143.    ZipMaster1->Load_Unz_Dll();
  144. }
  145.  
  146. //---------------------------------------------------------------------------
  147. void __fastcall TForm1::FormDestroy(TObject *Sender)
  148. {
  149.    ZipMaster1->Unload_Zip_Dll();
  150.    ZipMaster1->Unload_Unz_Dll();
  151. }
  152. //---------------------------------------------------------------------------
  153.