当我尝试用写模式打开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任何帮手都可以!!提前谢谢。
发布于 2017-05-30 13:23:04
我创建了名为config/initializers/patch.rb.的新文件
在下面的代码中添加,解决了这个问题。
Zip::DOSTime.instance_eval do
def now ; Zip::DOSTime.new() ; end
end我从这里取来的补丁
发布于 2020-12-15 08:35:33
在试图将zip条目的日期时间更改为原始文件datetime时,rubyzip和rzip中也存在类似的问题:
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.rbhttps://stackoverflow.com/questions/44259104
复制相似问题