Ruby: How to determine if file being read is binary or text

前端 未结 3 1856
广开言路
广开言路 2021-02-06 08:42

I am writing a program in Ruby which will search for strings in text files within a directory - similar to Grep.

I don\'t want it to attempt to search in binary files bu

相关标签:
3条回答
  • 2021-02-06 08:53

    libmagic is a library which detects filetypes. For this solution I assume, that all mimetype's which start with text/ represent text files. Eveything else is a binary file. This assumption is not correct for all mime types (eg. application/x-latex, application/json), but libmagic detect's these as text/plain.

    require "filemagic"
    
    def binary?(filename)
      begin
        fm= FileMagic.new(FileMagic::MAGIC_MIME)
        !(fm.file(filename)=~ /^text\//)
      ensure
        fm.close
      end
    end
    
    0 讨论(0)
  • 2021-02-06 09:10
    gem install ptools
    require 'ptools'
    File.binary?(file)
    
    0 讨论(0)
  • 2021-02-06 09:10

    An alternative to using the ruby-filemagic gem is to rely on the file command that ships with most Unix-like operating systems. I believe it uses the same libmagic library under the hood but you don't need the development files required to compile the ruby-filemagic gem. This is helpful if you're in an environment where it's a bit of work to install additional libraries (e.g. Heroku).

    According to man file, text files will usually contain the word text in their description:

    $ file Gemfile
    Gemfile: ASCII text
    

    You can run the file command through Ruby can capture the output:

    require "open3"
    
    def text_file?(filename)
      file_type, status = Open3.capture2e("file", filename)
      status.success? && file_type.include?("text")
    end
    
    0 讨论(0)
提交回复
热议问题