2009-09-05

Download and Uncompress zip file

在使用的過程裡,我發覺 vfs 1.4.0 應該是有 bug 的,如果要使用 vfs::zip 來解壓縮,要把版本昇到 1.4.1 才行。

下面是從臺灣證券交易所下載檔案,然後使用 vfs::zip 解壓縮的範例:

#!c:/tcl/bin/tclsh86.exe
package require http
package require vfs::zip

########################################################################
# Now download file from network
########################################################################

set day [clock format [clock seconds] -format {%d}]
set now [clock seconds]

if {$day > 7} {
set value [clock format [clock add $now -30 day] -format {%Y%m}]
} else {
set value [clock format [clock add $now -60 day] -format {%Y%m}]
}

set remoteFile "http://www.twse.com.tw/ch/statistics/download/04/001/"
append remoteFile $value
append remoteFile "_C04001.zip"

puts "======================================================"
puts "URL: $remoteFile"

set localFile $value
append localFile "_C04001.zip"

puts "Download File name: $localFile"
puts "======================================================"

set token [::http::geturl $remoteFile -binary 1]
set data [::http::data $token]

set channel [open $localFile w+]
fconfigure $channel -encoding binary -translation binary
puts -nonewline $channel $data
close $channel

::http::cleanup $token

########################################################################
# Now handle zip file
########################################################################

set mnt_file [vfs::zip::Mount $localFile $localFile]
file copy -force [glob $localFile/*] ./
vfs::zip::Unmount $mnt_file $localFile
file delete $localFile
幫朋友寫的,這樣再做一些適當的設定就可以定時下載最近的企業月報,而季報也可以比照辦理。

沒有留言: