From: "Rebhan, Gilbert" Date: 2007-08-21T17:21:16+09:00 Subject: Re: Test if file is binary ? -----Original Message----- From: Robert Klemme [mailto:shortcutter@googlemail.com] Sent: Tuesday, August 21, 2007 9:41 AM To: ruby-talk ML Subject: Re: Test if file is binary ? 2007/8/21, Alex Gutteridge : > Sorry for the duplicate! Robert is too fast for me. /* It's always good to see more solutions. I like the conciseness of your solution. But I think this should rather be a class method because you would not do the test on an open stream. Dunno which of the solutions is more realistic. */ you mean it should be something like ? = class File def self.is_binary?(name) ascii = total = 0 File.open(name, "rb") { |io| io.read(1024) }.each_byte do |c| total += 1; ascii +=1 if c >= 128 or c == 0 end ascii.to_f / total.to_f > 0.33 end end /* Might be fun to let both approaches test a large number of files and compare their results (probably also with output from "file"). :-) */ Is there an exisiting standard what is considered as a binary file, means a rule like check the first block from a file and = - if control characters (ASCII 0-32) and "high ASCII" (> 128) are found >30 % it's considered as binary file otherwise textfile - if control characters (ASCII 0-32 and > 128) are found == 0 it's always considered as textfile ?? Regards, Gilbert