ทำ Zip File โดยการใส่ Password ด้วย Abbrevia (ฉบับ Github)

Piyanat Nimkhuntod
2 min read5 days ago

--

เมื่อ TZip ไฟล์ไม่เพียงพอต่อการนำเอาไปใช้เนื่องจาก ข้อมูลด้านใน sensitive มาก หรือ ใช้เพื่อเป็นการ lock สองขั้นตอน จึงมาแนะนำ Component Zip File ที่ผมเคยใช้ในสมัยก่อนแต่เมื่อก่อนบริษัทหยุดพัฒนาทำให้ไม่ได้ไปต่อ แต่ปัจจุบันมีการนำกลับมาพัฒนาต่อแล้วแจกจ่ายในรูปแบบ Opensource นั้นคือเจ้า Abbrevia นั่นเอง

เริ่มแรกก็คงต้องไป Download เจ้า Abbrevia มาซะก่อน

นำ Path Source มาใส่ใน Library เช่น ผมเก็บไว้ใน D:\DevTools65\Abbrevia-master\Source

ทำการ Build แล้ว Install ให้เรียบร้อย

เอาละมา code กัน

ตัวอย่างแรก คือการแตกไฟล์แบบปกติ

procedure TForm1.Button2Click(Sender: TObject);
var
zipFile :TZipFile;
filename,Extractdir : String;
AbUnZip : TAbUnZipper;
begin
AbUnZip := TAbUnZipper.Create(nil);
try
AbUnZip.FileName := Edit1.Text;
// Clean out old Directory and create a new one.
Extractdir := Edit2.Text;

CreateDir(ExtractDir);
// Extract Files.
AbUnZip.BaseDirectory := ExtractDir;
AbUnZip.ExtractFiles('*.*');
// Compare Extracted Files
finally
AbUnZip.Free;
end;
end;

ตัวอย่างสอง คือการแนบไฟล์แบบใส่ Password

procedure TForm1.Button4Click(Sender: TObject);
var
zipFile :TZipFile;
filename : String;
Component : TAbZipper;
begin
zipFile := TZipFile.Create;
try
Component := TAbZipper.Create(nil);
Component.BaseDirectory := 'D:\Zip\';
Component.FileName := 'D:\Zip\AddZipDelphi.zip';
Component.AddFiles(Edit3.Text,faAnyFile);
Component.AddFiles(Edit4.Text,faAnyFile);
Component.Password := 'IamGroot' ;
Component.StoreOptions := [];
Component.Save;
Component.CloseArchive;


finally
zipFile.Free;
Component.Free;

end;

end;

ตัวอย่างสอง คือการแตกไฟล์แบบใส่ Password

procedure TForm1.Button5Click(Sender: TObject);
var
zipFile :TZipFile;
filename,Extractdir : String;
AbUnZip : TAbUnZipper;
begin
AbUnZip := TAbUnZipper.Create(nil);
try
AbUnZip.FileName := Edit5.Text;
// Clean out old Directory and create a new one.
Extractdir := Edit6.Text;

CreateDir(ExtractDir);
// Extract Files.
AbUnZip.BaseDirectory := ExtractDir;
AbUnZip.Password := Edit7.Text ;
AbUnZip.ExtractFiles('*.*');
// Compare Extracted Files
finally
AbUnZip.Free;
end;
end;

ดู code ได้ที่ Git hub https://github.com/piyanatn/Abbreviaexample

--

--