2007/8/21, Rebhan, Gilbert <Gilbert.Rebhan / huk-coburg.de>:
>
> Hi ,
>
> how to test if a file is binary or not ?
>
> There ain't something like File.binary =
> NoMethodError: undefined method `binary?' for File:Class
>
> Any ideas or libraries available ?

If I'd really need it I'd probably do a heuristic based on
distribution of byte values across an initial portion of the file.
Something like this:

class File
  def self.binary?(name)
    ascii = control = binary = 0

    File.open(name, "rb") {|io| io.read(1024)}.each_byte do |bt|
      case bt
        when 0...32
          control += 1
        when 32...128
          ascii += 1
        else
          binary += 1
      end
    end

    control.to_f / ascii > 0.1 || binary.to_f / ascii > 0.05
  end
end

Kind regards

robert