Xceed Zip Compression Library Documentation
Compress method example for Delphi
Example topics > Compress method example for Delphi

This example shows how to use the Compress and Uncompress methods. The example compresses (and for good measure, encrypts) some text from a string to an OleVariant variable, and then decrypts and decompresses it back. To try the example, place a button and an XceedCompression control on a form, and put the following code in the button's click event handler:

Delphi Copy Code

procedure TForm1.Button1Click(Sender: TObject);

var

   CompressedData : OleVariant;
   OriginalData : OleVariant; 
   ResultCode : XcdError; 

begin

   {Set the pasword to encrypt files with}
   XceedCompression1.EncryptionPassword := 'the password';  

   {Put the text to compress in a string variable}  
   OriginalData := 'This is some text to compress';  

   {Perform the compression} 
   XceedCompression1.License( 'your license key' );

   ResultCode := XceedCompression1.Compress(OriginalData,CompressedData,True);   

   {Check if it worked successfully}  
   if ResultCode <> 0 then 
      showmessage ('Error while Compressing. Error #'+inttostr(ResultCode));  

   {The EncryptionPassword property still contains the password so we can decrypt} 
   {and uncompress the data}

   ResultCode := XceedCompression1.Uncompress (CompressedData,OriginalData,True);  

   {Check if it worked successfully} 
   if ResultCode <> 0 then 
      showmessage ('Error while Decompressing. Error #' + inttostr(ResultCode)); 
end; 
end.