簡體   English   中英

rubyzip 未定義的方法`to_binary_dos_time'

[英]rubyzip undefined method `to_binary_dos_time' for

當我嘗試以寫入模式打開 zip 文件時,會記錄以下消息。

完整的錯誤信息:

undefined method `to_binary_dos_time' for 2017-05-30 15:07:21 +0530:Time

回溯:

    ["/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_entry.rb:286:in `write_local_entry'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:147:in `block in update_local_headers'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_entry_set.rb:35:in `each'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_entry_set.rb:35:in `each'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:145:in `update_local_headers'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:64:in `close'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:50:in `ensure in open'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_output_stream.rb:50:in `open'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:216:in `block in commit'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:324:in `on_success_replace'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:214:in `commit'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:242:in `close'", 
     "/usr/local/lib/ruby/gems/2.1.0/gems/rubyzip-0.9.9/lib/zip/zip_file.rb:92:in `open'", 
     "/app/zipper_job.rb:36:in `perform'",  

我的代碼如下。

path="#{Rails.root}"
new_zip_name= "#{Time.now.to_i.to_s}"
archive = File.join(path,new_zip_name)+'.zip'
Zip::ZipFile.open(archive, 'w') do |zipfile| #Code breaking on this line
    #MY Code
end

任何幫助appriciated ! 提前致謝。

rubyzip 和 rzip 中的類似問題,當嘗試將 zip 條目的日期時間更改為原始文件日期時間時:

zf = Zip::File.open("myzipfile.zip", Zip::File::CREATE)
e = zf.add("myfiletozip.txt","myfiletozip.txt") # will set the zipfile date to now
e.time = File.mtime("myfiletozip.txt") # will set it to the original file's

到目前為止一切順利,但是在關閉 zip 文件時,proc 失敗並出現相同的錯誤。 問題在於“time=”在條目中創建了“額外”結構,然后對其進行進一步處理,並且該處理使用缺少的方法。

但是為什么不設置時間就可以工作呢? 沒有構建額外的結構,也沒有使用缺少的方法。 就這么簡單。

to_binary_dos_time / date 存在於 Gem 中:dos_time.rb 然而,它們似乎被 gem 程序錯誤地引用,在我的例子中是 entry.rb

我的規避。 我只是將這兩個方法復制到我的代碼中,從 gem 模塊 dos_time.rb 中提取。 而且 - 奇跡 - 它有效。 gem程序在本地找到它們並且很高興。

但是,這個錯誤應該報告給作者,但我不知道如何。 也許有人可以做到這一點?

def to_binary_dos_time
    (sec / 2) +
      (min << 5) +
      (hour << 11)
end
    
def to_binary_dos_date
    day +
      (month << 5) +
      ((year - 1980) << 9)
end
# source copied out of entry.rb in rubizyp gem, in my case:
# c:\Ruby27-x64\lib\ruby\gems\2.7.0\gems\rubyzip-2.3.0\lib\zip\dos_time.rb

我創建了名為config/initializers/patch.rb 的新文件。

在其中添加了以下代碼,這解決了問題。

Zip::DOSTime.instance_eval do
  def now ; Zip::DOSTime.new() ; end
end

我從這里獲取的補丁

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM