Project

General

Profile

RE: when trying to open files in repository,but downloadi... ยป binary.rb

Victor Gavro, 2010-08-31 17:35

 
1
#!/usr/bin/env ruby
2

    
3
# run me like ruby binary.rb /PATH/TO/FILE
4
# this scripts checks if the file contains binary or textual data.
5
# thanks meineerde for help
6

    
7
require 'yaml'
8

    
9
FILENAME = ARGV[0]
10
f = File.read(FILENAME)
11
binary = f.is_binary_data?
12
binary_koef = f.count( "^ -~", "^\r\n" ).fdiv(f.size)
13

    
14
puts "The file #{FILENAME} contains #{binary ? "BINARY"  : "TEXTUAL"} data."
15
puts "file is binary if #{binary_koef} > 0.3"
    (1-1/1)