From e320c42fc3e29470fb5faad46f76cbd81520e749 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 28 May 2019 14:36:56 +0700 Subject: io: add function to check if content of file is binary Basically, the function count the ratio between printable characters for the first 512 bytes or more, excluding spaces. If the ratio is greater than 75% then its a text; otherwise its a binary. --- lib/io/is.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++ lib/io/is_example_test.go | 19 +++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 lib/io/is_example_test.go diff --git a/lib/io/is.go b/lib/io/is.go index 01e4de95..9b2368a1 100644 --- a/lib/io/is.go +++ b/lib/io/is.go @@ -8,8 +8,61 @@ import ( "io" "os" "path/filepath" + + libbytes "github.com/shuLhan/share/lib/bytes" ) +// +// IsBinary will return true if content of file is binary. +// If file is not exist or there is an error when reading or closing the file, +// it will return false. +// +func IsBinary(file string) bool { + var ( + total int + printable int + ) + + f, err := os.Open(file) + if err != nil { + return false + } + + content := make([]byte, 768) + + for total < 512 { + n, err := f.Read(content) + if err != nil { + break + } + + content = content[:n] + + for x := 0; x < len(content); x++ { + if libbytes.IsSpace(content[x]) { + continue + } + if content[x] >= 33 && content[x] <= 126 { + printable++ + } + total++ + } + } + + err = f.Close() + if err != nil { + return false + } + + ratio := float64(printable) / float64(total) + + if ratio > float64(0.75) { + return false + } + + return true +} + // // IsDirEmpty will return true if directory is not exist or empty; otherwise // it will return false. diff --git a/lib/io/is_example_test.go b/lib/io/is_example_test.go new file mode 100644 index 00000000..eca07a4d --- /dev/null +++ b/lib/io/is_example_test.go @@ -0,0 +1,19 @@ +// Copyright 2019, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package io + +import ( + "fmt" +) + +func ExampleIsBinary() { + fmt.Println(IsBinary("/dev/null")) + fmt.Println(IsBinary("/bin/bash")) + fmt.Println(IsBinary("io.go")) + // Output: + // true + // true + // false +} -- cgit v1.3