2

I received a complain about a zip file I created with rubyzip 1.2.0:

The text flag for text.pdf corrupted the pdf after unpacking.

I create the zip under windows, the recipient works with Unix/Linux.

You can see the flag with zipinfo (or unzip -Z with windows). unzip -Z -l test.zip results with:

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

This is the related testcode:

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

I checked the source code of Zip::Entry and found the definition of @internal_file_attributes = 1, but no possibility to influence this value.

Based on this information, I made a patch Zip::Entry like this:

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

When I add this patch in my test code above, then I get:

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

So I found a work around for my problem - but is there another and better solution?

Is there a possibility to set the binary flag during adding the file?

knut
  • 27,320
  • 6
  • 84
  • 112
  • Why do you use `__FILE__`? Your code is adding the ruby source file itself to the zip archive, not the pdf file. – matt Oct 01 '16 at 22:38
  • @matt My original code adds multiple files, and the pdf-file get's the `t`-flag. I use `__FILE__` here only to build a MWE (minimal working example). You don't need additional files to run an example. I added acomment in my example to point this out. – knut Oct 02 '16 at 12:05

0 Answers0