From: Lloyd Zusman Date: 2002-10-05T15:38:40+09:00 Subject: Re: Ruby to approximate 'file'? > Is there a file-type detection script available for Ruby, similar to > the unix 'file' program? At a minimum, is there a way in Ruby to > reliably tell the difference between a binary file format and a text > file format? Here's something in Ruby that I wrote to do that. It's based on the algorithm that Perl uses to determine "textness" and "binariness": module FileTest private def self.isText(block) return (block.count("^ -~", "^\b\f\t\r\n") < (block.size / 3.0) && block.count("\x00") < 1) end public # The textfile? and binaryfile? methods are not inverses of each # other. Both return false if the item is not a file, or if the # item is a zero-length file. The "textness" or "binariness" of # an item can only be determined if it's a file that contains at # least one byte. def self.textfile?(item) size = self.size(item) blksize = File.stat(item).blksize if size < 1 then return false end begin open(item) { |file| block = file.read(blksize < size ? blksize : size) return self.isText(block) } rescue return false end end def self.binaryfile?(item) size = self.size(item) blksize = File.stat(item).blksize if size < 1 then return false end begin open(item) { |file| block = file.read(blksize < size ? blksize : size) return !self.isText(block) } rescue return false end end end > [ ... ] -- Lloyd Zusman ljz@asfast.com