如何使用rubyzip在Zip文件中设置二进制标志?

时间:2016-09-30 22:10:22

标签: ruby zip binaryfiles rubyzip

我收到了一封关于我用rubyzip 1.2.0创建的zip文件的抱怨:

  

text.pdf的文本标志在解压缩后损坏了pdf。

我在windows下创建zip,收件人使用Unix / Linux。

您可以看到zipinfo(或unzip -Z带有窗口)的标记。 unzip -Z -l test.zip结果为:

Archive:  test.zip
Zip file size: 1666 bytes, number of entries: 2
-rw----     5.2 fat     1521 t-      730 defN 16-Sep-30 23:37 test.dtx
-rw----     5.2 fat     1521 t-      730 defN 16-Sep-30 23:37 test.pdf
2 files, 3042 bytes uncompressed, 1460 bytes compressed:  52.0%
                             +
                             +--- This is the t-flag

这是相关的测试代码:

require "zip"
###add here my patch####

Zip::File.open('test.zip', Zip::File::CREATE) do |zipfile|
    %w{
      test.dtx
      test.pdf
      }.each{|filename| 
      file = begin
        #I use __FILE__, so you can run this example out of the box.
        #My original code uses the dtx and pdf file.
        zipfile.add(filename, __FILE__)   #Zip::ZipEntryExistsError if already in zip
      rescue Zip::EntryExistsError
        zipfile.replace(filename, __FILE__)
      end
      #This is part of my work around
      file.internal_file_attributes = 0 if filename =~ /pdf$/ and file.respond_to?(:internal_file_attributes)
    }
end #Zip::ZipFile.open

我检查了Zip::Entry的源代码并找到了它的定义 @internal_file_attributes = 1,但不可能影响这个价值。

根据这些信息,我做了一个补丁Zip :: Entry,如下所示:

module Zip
  class Entry
    #my work around to set @internal_file_attributes
    attr_accessor :internal_file_attributes
  end
end

当我在上面的测试代码中添加此补丁时,我得到:

Archive:  test.zip
Zip file size: 1726 bytes, number of entries: 2
-rw----     5.2 fat     1666 t-      760 defN 16-Sep-30 23:40 test.dtx
-rw----     5.2 fat     1666 b-      760 defN 16-Sep-30 23:40 test.pdf
2 files, 3332 bytes uncompressed, 1520 bytes compressed:  54.4%
                             +
                             +--- There is a b-flag for the pdf

所以我找到了解决问题的方法 - 但还有另一个更好的解决方案吗?

是否有可能在添加文件时设置二进制标志?

0 个答案:

没有答案
相关问题