这里以获取XFS标准文档为例,直接将远程的XFS目录克隆到本地的t子目录。
#encoding:utf-8
require 'net/ftp'
HOST='ftp.cenorm.be'
REMOTE_PATH='/CEN/WhatWeDo/Fields/ICT/eBusiness/WS/XFS'
LOCAL_PATH='D:/Documents/WOSA/t'
def fetch_file(ftp,path)
$stderr.puts 'get "' + path + '"'
ftp.getbinaryfile(path)
end
def fetch_path(ftp,path)
$stderr.puts 'into "' + path + '"'
ftp.chdir(path)
files = ftp.nlst
files.each do |file|
fetch_file(ftp,path+File::Separator+file)
end
ftp.mlsd do |item|
if item.facts['type'] == 'dir'
lpath=item.pathname
Dir.mkdir(lpath)
Dir.chdir(lpath)
fetch_path(ftp,path+File::Separator+lpath)
Dir.chdir('..')
end
end
end
Net::FTP.open HOST do |ftp|
ftp.login
puts "FTP Server Information:#{ftp.system}"
Dir.chdir(LOCAL_PATH)
fetch_path(ftp,REMOTE_PATH)
end