分享

ruby版本的ftp批量下载

 CJOL藏经阁 2014-11-04

#
# To change this template, choose Tools | Templates
# and open the template in the editor.
 
require 'net/ftp'


#include Tests

def upload_file(host,user,password,local_folder,remote_folder,size)
  ftp_client=Net::FTP.new(host)
  ftp_client.login(user,password)
  get_folder("*",remote_folder,ftp_client,create_folder(local_folder),size)
  ftp_client.close
end

#
def create_folder(local_folder)
  folder=local_folder+'\\'+Time.now.strftime("%y%m%d")
  unless File.exist?(folder)
    Dir.mkdir(folder, 0)
  end
  folder
end

def get_folder(file_mark,path,ftp_client,folder,size)
  #dirs=ftp_client.dir path
  dirs=ftp_client.ls path
  ftp_client.chdir(path)
  dirs.each { |dir|
    infos=dir.split(' ')
    info=infos.last.sub("http://zsbfree.blog.163.com/blog/static/23400688200811315157711/file://n/","")
    if dir[0,1]=="d"
      unless info=='.'||info=='..'
        folder_file=folder+"http://zsbfree.blog.163.com/blog/static/23400688200811315157711/file://%22+info/
        unless File.exist?(folder_file)
          Dir.mkdir(folder_file,1)
        end
       
        get_folder(file_mark,path+"/"+info,ftp_client,folder_file,size)
        ftp_client.chdir(path)
      end
    elsif dir[0,2]=="-r"
      file=folder+"http://zsbfree.blog.163.com/blog/static/23400688200811315157711/file://%22+info/
      if(File.exist?(file))
        remoting_size=ftp_client.size(info)
        local_size=File.size(file)
       
        if local_size<remoting_size
          ftp_client.sendcmd("REST "+local_size.to_s)
          open(flie,'ab') {|f|
            ftp_client.getbinaryfile(info, file,size) { |i|
                f<<i
             }
          }
        elsif local_size>remoting_size
          getfile(info,file,ftp_client,size)
        end
      else
       get_file(info,file,ftp_client,size)
      end
    end
  }
end

def get_file(info,file,ftp_client,size)
   open(file,'wb') {|f|
     ftp_client.getbinaryfile(info, file,size) { |i|
       f<<i
       }
    }
    puts file+" has download"
end


#puts Test.new(super_class)
upload_file('219.234.89.155','tianc','zhuzhu','c:\\temp','/temp',1024)


 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约