aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-05-28 14:36:56 +0700
committerShulhan <ms@kilabit.info>2019-06-02 08:55:31 +0700
commite320c42fc3e29470fb5faad46f76cbd81520e749 (patch)
treeac293e890ecfa8940c14323e629b8c6cc09a5aa7
parentdcd484c21de6e2b16470987e591f098b1d47e091 (diff)
downloadpakakeh.go-e320c42fc3e29470fb5faad46f76cbd81520e749.tar.xz
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.
-rw-r--r--lib/io/is.go53
-rw-r--r--lib/io/is_example_test.go19
2 files changed, 72 insertions, 0 deletions
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,9 +8,62 @@ 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 <ms@kilabit.info>. 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
+}