To create a password-protected ZIP file in Delphi, you can use the TZipFile class from the System.Zip unit. Here is an example of how you can use TZipFile to create a password-protected ZIP file
First, include the System.Zip unit in your code by adding the following line at the top of your program:
1 |
uses System.Zip; |
Declare a variable of type TZipFile:
1 2 |
var ZipFile: TZipFile; |
Create an instance of TZipFile:
1 |
ZipFile := TZipFile.Create; |
Set the password for the ZIP file:
1 |
ZipFile.Password := ‘password’; |
Add the files you want to include in the ZIP file:
1 2 |
ZipFile.AddFile(‘CMyDirectoryFile1.txt’, ‘File1.txt’); ZipFile.AddFile(‘CMyDirectoryFile2.txt’, ‘File2.txt’); |
Save the ZIP file to a specified location:
1 |
ZipFile.SaveToFile(‘CMyDirectoryMyZipFile.zip’); |
Free the ZIP file when it is no longer needed:
1 |
ZipFile.Free; |
This example shows how to create a ZIP file with two text files, but you can add other types of files as well. You can find more information about the TZipFile class and the various options it provides in the Delphi documentation.
Leave a Reply