Delphi 7 從健保局下載zip檔案順便解壓縮

由於學校還使用Delphi 7,為了要教同學從網路上下載檔案加上解壓縮費了不少力氣。順便就記錄一下如何從健保局抓下檔案後,然後一道解壓縮。

1.Delphi7沒有壓縮元件,所以要尋求免費的資源。幸好有Opensource的Delphi 7zip元件
網址:https://code.google.com/p/d7zip/
下載之後解壓縮會有三個檔案
7z.dll
readme.html
sevenzip.pas

2.把7z.dll跟sevenzip.pas放到程式目錄下(散佈程式時記得.exe檔跟7z.dll要一起copy)。
3.回到Delphi7的Project→add to project把sevenzip.pas加進專案
4.File→Use Unit選Sevenzip.pas後就可以開始寫code了
5.畫面上佈置如下圖
(text設為http://www.nhi.gov.tw/Resource/webdata/2976_1_hospbsc.zip)
delphi7zip02
6.ButtonClick程式如下
[pascal]
procedure TForm1.Button1Click(Sender: TObject);
var
ms:TMemoryStream;
i:integer;
tempfile,extname,filename1:String;
begin
ms := Tmemorystream.Create;
ms.Clear;
ms.Position :=0;
try
idhttp1.Get(edit1.text,ms);
ms.SaveToFile(ExtractFilePath(Application.ExeName)+’\test.zip’);
except
end;
if fileexists(ExtractFilePath(Application.ExeName)+’\test.zip’) then
begin
with CreateInArchive(CLSID_CFormatZip) do
begin
OpenFile(ExtractFilePath(Application.ExeName)+’\test.zip’);
for i := 0 to NumberOfItems – 1 do
if not ItemIsFolder[i] then begin
tempfile := ItemPath[i];
extname:=ExtractFileExt(tempfile);
if extname =’.txt’ then
filename1:=tempfile;
end;
ExtractTo(ExtractFilePath(Application.ExeName));
end;
Memo1.Lines.LoadFromFile(Filename1);
end;
end;
[/pascal]
藥品健保藥價資料集
P.S idhttp1.HandleRedirects := True;