home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedZip.Cab / F112441_DllAPI.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-11-10  |  10.4 KB  |  297 lines

  1. unit DllAPI;
  2. {------------------------------------------------------------------------------}
  3. { Xceed Zip 4 Dll API Sample for Delphi 2, 3, 4 and 5.                         }
  4. { Copyright (c) 1998-1999 Xceed Software Inc.                                  }
  5. {------------------------------------------------------------------------------}
  6.  
  7. interface
  8.  
  9. uses
  10.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  11.   StdCtrls, ComCtrls;
  12.  
  13. const
  14.   { This is the message sent to the associated window when an event occurs. }
  15.   WM_USER_XCEEDZIPEVENT = WM_USER + 1555;
  16.  
  17.   { These are some of the possible values for wParam when an event occurs. }
  18.   XM_SKIPPINGFILE = 6;
  19.   XM_FILESTATUS = 9;
  20.   XM_GLOBALSTATUS = 10;
  21.   XM_WARNING = 17;
  22.  
  23.   { If you have a registered version, paste your license number below. You
  24.     will find this string in the LICENSE.TXT file. }
  25.   cLicenseString = '';
  26.  
  27.   { These constants represent the type of value you want to retrieve a
  28.     description for, using the XzGetErrorDescription API. }
  29.   xvtError          = 0;
  30.   xvtWarning        = 1;
  31.   xvtSkippingReason = 2;
  32.  
  33.   { Number of chars in filename fields. }
  34.   MAX_PATH = 260;
  35.  
  36. type
  37.   { The following structures were translated from the XceedZipAPI.h file. }
  38.   PXcdSkippingFileParamsA = ^TXcdSkippingFileParamsA;
  39.   TXcdSkippingFileParamsA = record
  40.     wStructSize       : SmallInt;
  41.     hZip              : LongInt;
  42.     szFilename        : array[ 1..MAX_PATH ] of Char;
  43.     szComment         : array[ 1..1024 ] of Char;
  44.     szFilenameOnDisk  : array[ 1..MAX_PATH ] of Char;
  45.     lSize             : LongInt;
  46.     lCompressedSize   : LongInt;
  47.     xAttributes       : LongInt;
  48.     lCRC              : LongInt;
  49.     stModified        : TSystemTime;
  50.     stAccessed        : TSystemTime;
  51.     stCreated         : TSystemTime;
  52.     xMethod           : LongInt;
  53.     bEncrypted        : LongInt;
  54.     xReason           : LongInt;
  55.   end;
  56.  
  57.   PXcdFileStatusParamsA = ^TXcdFileStatusParamsA;
  58.   TXcdFileStatusParamsA = record
  59.     wStructSize       : SmallInt;
  60.     hZip              : LongInt;
  61.     szFilename        : array[ 1..MAX_PATH ] of Char;
  62.     lSize             : LongInt;
  63.     lCompressedSize   : LongInt;
  64.     lBytesProcessed   : LongInt;
  65.     nBytesPercent     : SmallInt;
  66.     nCompressionRatio : SmallInt;
  67.     bFileCompleted    : LongInt;
  68.   end;
  69.  
  70.   PXcdGlobalStatusParams = ^TXcdGlobalStatusParams;
  71.   TXcdGlobalStatusParams = record
  72.     wStructSize       : SmallInt;
  73.     hZip              : LongInt;
  74.     lFilesTotal       : LongInt;
  75.     lFilesProcessed   : LongInt;
  76.     lFilesSkipped     : LongInt;
  77.     nFilesPercent     : SmallInt;
  78.     lBytesTotal       : LongInt;
  79.     lBytesProcessed   : LongInt;
  80.     lBytesSkipped     : LongInt;
  81.     nBytesPercent     : SmallInt;
  82.     lBytesOutput      : LongInt;
  83.     nCompressionRatio : SmallInt;
  84.   end;
  85.  
  86.   PXcdWarningParamsA = ^TXcdWarningParamsA;
  87.   TXcdWarningParamsA = record
  88.     wStructSize : SmallInt;
  89.     hZip        : LongInt;
  90.     szFilename  : array[ 1..MAX_PATH ] of Char;
  91.     xWarning    : LongInt;
  92.   end;
  93.  
  94.   TfrmDllAPI = class(TForm)
  95.     cmdZip: TButton;
  96.     cmdUnzip: TButton;
  97.     cmdAbort: TButton;
  98.     txtZipFile: TEdit;
  99.     txtFiles: TEdit;
  100.     txtExtract: TEdit;
  101.     Label1: TLabel;
  102.     Label2: TLabel;
  103.     Label3: TLabel;
  104.     txtPassword: TEdit;
  105.     Label4: TLabel;
  106.     barProgress: TProgressBar;
  107.     lstResults: TListBox;
  108.     Label5: TLabel;
  109.     chkSubfolders: TCheckBox;
  110.     procedure cmdZipClick(Sender: TObject);
  111.     procedure cmdUnzipClick(Sender: TObject);
  112.     procedure FormDestroy(Sender: TObject);
  113.     procedure FormCreate(Sender: TObject);
  114.     procedure cmdAbortClick(Sender: TObject);
  115.   private
  116.     { This is a handle to your instance of the Xceed Zip object. }
  117.     hZip : LongInt;
  118.     { This method will be called each time the associated Xceed Zip object
  119.       triggers an event, after setting the Window handle to notify. }
  120.     procedure XceedZipMessage( var xMsg : TMessage ); message WM_USER_XCEEDZIPEVENT;
  121.   public
  122.   end;
  123.  
  124.   { These prototypes were translated from the XceedZipAPI.h file.
  125.     Make sure to use stdcall naming for "C" style prototypes! }
  126.   function  XceedZipInitDLL : LongInt; stdcall; external 'XceedZip.DLL';
  127.   function  XceedZipShutdownDLL : LongInt; stdcall; external 'XceedZip.DLL';
  128.  
  129.   function  XzCreateXceedZipA( pszLicense : PChar) : LongInt; stdcall; external 'XceedZip.DLL';
  130.   procedure XzDestroyXceedZip( hZip : LongInt ); stdcall; external 'XceedZip.DLL';
  131.   procedure XzSetXceedZipWindow( hZip : LongInt; hWnd : LongInt ); stdcall; external 'XceedZip.DLL';
  132.  
  133.   procedure XzSetZipFilenameA( hZip : LongInt; pszValue : PChar); stdcall; external 'XceedZip.DLL';
  134.   procedure XzSetFilesToProcessA( hZip : LongInt; pszValue : PChar ); stdcall; external 'XceedZip.DLL';
  135.   procedure XzSetUnzipToFolderA( hZip : LongInt; pszValue : PChar); stdcall; external 'XceedZip.DLL';
  136.   procedure XzSetEncryptionPasswordA ( hZip : LongInt; pszValue : PChar ); stdcall; external 'XceedZip.DLL';
  137.   procedure XzSetProcessSubfolders( hZip : LongInt; bValue : LongInt ); stdcall; external 'XceedZip.DLL';
  138.   procedure XzSetAbort( hZip : LongInt; bValue : LongInt ); stdcall; external 'XceedZip.DLL';
  139.  
  140.   function  XzZip( hZip : LongInt ) : LongInt; stdcall; external 'XceedZip.DLL';
  141.   function  XzUnzip( hZip : LongInt ) : LongInt; stdcall; external 'XceedZip.DLL';
  142.   function  XzGetErrorDescriptionA( hZip : LongInt; xType : LongInt; xCode : LongInt; pszBuffer : PChar; uMaxLength : LongInt ) : LongInt; stdcall; external 'XceedZip.DLL';
  143.  
  144. var
  145.   frmDllAPI: TfrmDllAPI;
  146.  
  147. implementation
  148.  
  149. {$R *.DFM}
  150.  
  151. procedure TfrmDllAPI.FormCreate(Sender: TObject);
  152. begin
  153.   { Initialize library. }
  154.   XceedZipInitDLL;
  155.  
  156.   { Create one instance for this form. }
  157.   hZip := XzCreateXceedZipA( cLicenseString );
  158.  
  159.   { Associate this instance with this window. }
  160.   XzSetXceedZipWindow( hZip, Self.Handle );
  161. end;
  162.  
  163. procedure TfrmDllAPI.FormDestroy(Sender: TObject);
  164. begin
  165.   { Free instance and shutdown library. }
  166.   XzDestroyXceedZip( hZip );
  167.   XceedZipShutdownDll;
  168. end;
  169.  
  170. procedure TfrmDllAPI.cmdZipClick(Sender: TObject);
  171. var
  172.   xResult : LongInt;
  173.   szMessage : array[ 0..200 ] of Char;
  174. begin
  175.   { Set the zip file you wish to create or update. }
  176.   XzSetZipFilenameA( hZip, PChar( txtZipFile.text ) );
  177.  
  178.   { The "FilesToProcess" property is a string representing a list of CR/LF
  179.     delimited filenames or file masks. You can also use the AddFilesToProcess
  180.     method to add multiple entries. }
  181.   XzSetFilesToProcessA( hZip, PChar( txtFiles.text ) );
  182.  
  183.   { You can set other properties as you wish. }
  184.   XzSetEncryptionPasswordA( hZip, PChar( txtPassword.Text ) );
  185.  
  186.   if chkSubfolders.Checked then
  187.     XzSetProcessSubfolders( hZip, 1 )
  188.   else
  189.     XzSetProcessSubfolders( hZip, 0 );
  190.  
  191.   { Reset window contents. }
  192.   barProgress.Position := 0;
  193.   lstResults.Clear;
  194.  
  195.   { To start zipping, just call the Zip method. }
  196.   xResult := XzZip( hZip );
  197.  
  198.   { The GetErrorDescription function is very useful for displaying a default
  199.     message for any error code, skipping reason or warning. }
  200.   XzGetErrorDescriptionA( hZip, xvtError, xResult, szMessage, 200 );
  201.   ShowMessage( string( szMessage ) );
  202. end;
  203.  
  204. procedure TfrmDllAPI.cmdUnzipClick(Sender: TObject);
  205. var
  206.   xResult : LongInt;
  207.   szMessage : array[ 0..200 ] of Char;
  208. begin
  209.   { Set the zip filename you want to unzip from. }
  210.   XzSetZipFilenameA( hZip, PChar( txtZipFile.Text ) );
  211.  
  212.   { Set the files you wish to unzip. Again, this is a list of CR/LF delimited
  213.     filenames or file masks. You can also use the AddFilesToProcess method to
  214.     add multiple entries. }
  215.   XzSetFilesToProcessA( hZip, PChar( txtFiles.Text ) );
  216.  
  217.   { You can set other properties too. }
  218.   XzSetUnzipToFolderA( hzip, PChar( txtExtract.text ) );
  219.   XzSetEncryptionPasswordA( hZip, PChar( txtPassword.Text ) );
  220.  
  221.   if chkSubfolders.Checked then
  222.     XzSetProcessSubfolders( hZip, 1 )
  223.   else
  224.     XzSetProcessSubfolders( hZip, 0 );
  225.  
  226.   { Reset window contents. }
  227.   barProgress.Position := 0;
  228.   lstResults.Clear;
  229.  
  230.   { To unzip, just call the Unzip method. }
  231.   xResult := XzUnzip( hZip );
  232.  
  233.   XzGetErrorDescriptionA( hZip, xvtError, xResult, szMessage, 200 );
  234.   ShowMessage( string( szMessage ) );
  235. end;
  236.  
  237. procedure TfrmDllAPI.cmdAbortClick(Sender: TObject);
  238. begin
  239.   { To abort the current operation, set the Abort property to TRUE }
  240.   XzSetAbort( hZip, 1 );
  241. end;
  242.  
  243. { When using the Dll API in conjunction with the "XzSetXceedZipWindow"
  244.   function, the specified window will receive the special WM_USER_XCEEDZIPEVENT
  245.   message. You just need to implement a handler for this message. }
  246. procedure TfrmDllAPI.XceedZipMessage( var xMsg : TMessage );
  247. var
  248.   pSkipping : PXcdSkippingFileParamsA;
  249.   pFile : PXcdFileStatusParamsA;
  250.   pGlobal : PXcdGlobalStatusParams;
  251.   pWarning : PXcdWarningParamsA;
  252.   szMessage : array[ 0..200 ] of Char;
  253. begin
  254.   { wParam contains the ID of the triggered event. lParam contains a pointer to
  255.     the proper parameter structure (see XceedZipAPI.h). }
  256.   case xMsg.WParam of
  257.     XM_SKIPPINGFILE: { A file is being skipped. }
  258.     begin
  259.       pSkipping := PXcdSkippingFileParamsA( xMsg.LParam );
  260.       XzGetErrorDescriptionA( hZip, xvtSkippingReason, pSkipping^.xReason,
  261.                               szMessage, 200 );
  262.       lstResults.Items.Add( 'Skipping file '
  263.                           + string( pSkipping^.szFilename )
  264.                           + ' [' + string( szMessage ) + ']' );
  265.     end;
  266.  
  267.     XM_FILESTATUS: { A file is being processed further. }
  268.     begin
  269.       pFile := PXcdFileStatusParamsA( xMsg.LParam );
  270.       if pFile^.lBytesProcessed = 0 then
  271.         lstResults.Items.Add( 'Processing file '
  272.                             + string( pFile^.szFilename ) );
  273.     end;
  274.  
  275.     XM_GLOBALSTATUS: { The Zip file is being processed further. }
  276.     begin
  277.       pGlobal := PXcdGlobalStatusParams( xMsg.LParam );
  278.       barProgress.Position := pGlobal^.nBytesPercent;
  279.     end;
  280.  
  281.     XM_WARNING: { A warning occured. }
  282.     begin
  283.       pWarning := PXcdWarningParamsA( xMsg.LParam );
  284.       XzGetErrorDescriptionA( hZip, xvtWarning, pWarning^.xWarning,
  285.                               szMessage, 200 );
  286.       lstResults.Items.Add( 'Warning '
  287.                           + IntToStr( pWarning^.xWarning )
  288.                           + ' on file '
  289.                           + string( pWarning^.szFilename )
  290.                           + ' [' + string( szMessage ) + ']' );
  291.     end;
  292.   end;
  293. end;
  294.  
  295. end.
  296.  
  297.