在Delphi开发中,我们经常需要使用压缩和解压缩功能,Delphi自带的system.zip 单元,完全支持文件、内存流的压缩和解压缩。可是一直不能完整的支持带有密码的压缩和解压缩,好在有大佬做好了,我们拿来使用就是了。
具体的下载地址如下:
GitHub - UweRaabe/EncryptedZipFile: TZipFile with Password Encryption
下载下来后有三个文件,具体如下:
支持D12版本 | ||
支持D11版本 | ||
原始发布,低版本 |
如何使用呢?
一、Delphi 12版本使用方法
需要引用:PKWAREZipCryptor.pas 文件。
uses
PKWAREZipCryptor.pas,
....;
var
LZipFile: TZipFile;
begin
//创建压缩对象
LZipFile := TZipFile.Create;
LZipFile.Cryptor := TPKWAREZipCryptor.Create;
LZipFile.Password := password;
....
end
二、Delphi 11.1 和 11.2 使用方法
需要引用:uEncryptedZipFile.pas 文件。
uses
uEncryptedZipFile,
....;
var
LZipFile: TEncryptedZipFile;
begin
//创建压缩对象
LZipFile := TEncryptedZipFile.Create(password);
LZipFile.Password := password;
.....
end
三、兼容D12 和D11.1 D11.2 的封装函数
//压缩一个目录到一个文件中,包含密码
//ZipDir: 表示需要压缩的目录, ZipFileName:压缩后的zip文件名称 password:密码
function ZipDir2File(const ZipDir, ZipFileName : String; const password : string = ''; Compression: TZipCompression = zcDeflate; ZipProgress: TZipProgressEvent = nil) : Boolean;
//解压一个zip文件到一个指定的目录中,包含压缩文件的目录结构
//ZipFileName:需要解压的zip文件名称, UnZipDir:解压后需要放置的目录 password:密码
function UnZipFile2Dir(const ZipFileName, UnZipDir : String; const password : string = ''; ZipProgress: TZipProgressEvent = nil) : Boolean;
//将zip压缩文件中的一个文件加压成字节流
procedure ExtractFile_Bytes(const ZipFileName : string; const FileName,password : string; var B : TBytes);
//将zip压缩文件中的一个文件加压成字Stream,这样就很容易实现保存成一个文件
procedure ExtractFile_Stream(const ZipFileName : string; const FileName,password : string; var ST : TStream);
以上函数兼容两个版本,直接使用。
源程序下载(收费)