【问题标题】:Get zip from web url automatically and unzip it自动从 web url 获取 zip 并解压缩
【发布时间】:2012-12-13 09:47:54
【问题描述】:

我需要访问这个网址: http://autospace.by/download/prices.zip

然后将此文件解压缩到我的 Rails 应用程序,但我必须在每天下午 2 点进行一次,主要问题是存档中其他 url 的文件每天都在更改名称,所以我必须解压缩文件,名称结尾其中是,并对其进行一些操作?

我现在的代码:

require 'net/http'
    Net::HTTP.start("autospace.by") do |http|
        resp = http.get("/download/prices.zip")
        open(Rails.root.join('public', 'uploads_prices', "autospace.zip"), "wb") do |file|
            file.write(resp.body)
        end
    end

所以:我如何在这个 url 上解压缩文件,并将这个文件名作为参数发送到方法 mymethod(filename)?并提供建议如何每天自动执行此操作?

【问题讨论】:

    标签: ruby-on-rails ruby file-upload zip scheduled-tasks


    【解决方案1】:

    可以直接使用linux zip命令system "cd #{Rails.root}/#{zip_path}; zip -qr #{zip_file_name}.zip #{zip_file_name};

    或使用 ruby​​ zip gems

      gem 'archive-zip'  # Use require_gem for rubygems versions older than 0.9.0.
      require 'archive/zip'
    
      # Add a_directory and its contents to example1.zip.
      Archive::Zip.archive('example1.zip', 'a_directory')
    

    如果你想每天都执行这个,只需在 crontab 中运行这个脚本,例如

       `*/5 * * * * /usr/local/bin/ruby  /local/myuser/rubyscript/ftpscript.rb >> /local/myuser/rubyscript/ftplog.txt`
    

    【讨论】:

    • gem 'archive-zip' 这个按照我的代码写,怎么加入呢?
    • 在你的脚本中执行gem "archive-zip" at your teminate, and add require 'archive/zip'`
    • 那我怎样才能得到文件名呢?如果可能是 13-12name、14-12name 等...我必须将其发送给方法
    • 先下载压缩包,自己给它一个本地文件名,然后解压
    • 如果我现在如何用 ruby​​ 编写代码...我没有在这里问!举个例子!
    猜你喜欢
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多