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;

作者: 林壽山

目前任職於軟體公司研究開發部門,擔任專業處長,專注於.NET C# 開發,並具備豐富的POS 收銀系統與金流整合開發經驗。我精通各類支付系統的設計與開發,包含第三方支付(如綠界、藍新、歐付寶、速買配、馬來西亞 ePay/HappyPay、台新 One 碼)、行動支付(悠遊卡、一卡通、支付寶、微信支付、街口支付)、以及信用卡支付(聯合信用卡)。 熟悉多種開發技術,擅長PHP 網頁開發(CodeIgniter、Laravel 框架)、Delphi 程式設計、資料庫設計、C# WinForm/WebForm 應用開發、ASP.NET MVC、API 串接設計,並具備LINE 串接開發的豐富經驗。 除了技術開發之外,我也熱衷於技術分享,曾擔任台中學校產業學院講師 5 年,培育新一代的軟體開發人才,致力於推動軟體技術的應用與創新。 我對技術充滿熱忱,始終保持學習與探索的心態,期望透過軟體開發為企業與社會創造更大的價值。