:paw_prints: Detect if a file is binary or text

Binary

Go module and command line utility for checking if the given file or data is likely to be binary or text.

  • It does so by reading the first, middle and last 24 bytes of the file and trying to convert the data to utf8.
  • If one of the 24 byte blocks can not be converted to utf8, it's considered to be a binary file.
  • Also, if one of the blocks have more than 33% zero bytes, it's considered to be a binary file.
  • If the file is empty, it's considered to be a text file.
  • The binary utility has overlapping functionality with the file utility, but with a more limited focus.
  • If the first 24 bytes indicates that it's a binary file, the deduction is done, and no more seeking or reading will happen.

Installing the utility

With Go 1.17 or later:

go install github.com/xyproto/binary/cmd/binary@latest

Example use

  • binary /usr/bin/ls returns binary.
  • binary /etc/fstab returns text.

Using the Go module

package main

import (
    "fmt"
    "os"

    "github.com/xyproto/binary"
)

func main() {
    filename := os.Args[0]
    isBinary, err := binary.File(filename)
    if err != nil {
        fmt.Fprintf(os.Stderr, "%s\n", err)
        os.Exit(1)
    }
    fmt.Printf("%s is a binary file: %v\n", filename, isBinary)
}

The binary.Data function can be used to determine if a byte slice contains binary data or not.

General info

Owner
Alexander F. Rødseth
Software Engineer, Arch Linux TU, Go/Python/C++/C developer and open source enthusiast.
Alexander F. Rødseth
Similar Resources

A binary to control the Z-Cam line of cameras via API

The Z-Cam flagship line has an API of sorts. This can be used to control the camera--via a StreamDeck, say. This seems like a good enough reason to me

Nov 12, 2022

Extract text from plaintext, .docx, .odt and .rtf files. Pure go.

cat This is a simple libary to extract text from plaintext, .docx, .odt, .pdf and .rtf files. Install go get -u github.com/lu4p/cat Basic Usage packag

Nov 18, 2022

Custom text condition for filters

Custom text condition for filters Install go get -v github.com/blins/condition Use func init() { condition.RegisterConditionFabric("prefix", cond

Nov 23, 2021

Turn a heterogeneous pile of text docs into a single web page with good search.

Turn a heterogeneous pile of text docs into a single web page with good search.

Codex Codex turns an unstructured pile of heterogeneous documents into a single interactive web document. Your input documents maybe in markdown, TeX,

Jan 2, 2022

Easysearch - Easy Full-Text Search Engine in golang

Easysearch - Easy Full-Text Search Engine in golang

Easy Full-Text Search Engine Overview EasySearch是一个分布式的全文检索搜索引擎,同时支持内存检索与磁盘检索,并针

Dec 20, 2022

Ttsaas-clone - Text To Speech As A Service

TTSAAS Text To Speech As A Service Dependencies Be sure to install the following

Feb 2, 2022

Monitoring changes in the source file and automatically compile and run (restart).

Monitoring changes in the source file and automatically compile and run (restart).

dogo Monitoring changes in the source file and automatically compile and run (restart). 中文 Install go get github.com/liudng/dogo Create config Here's

Dec 28, 2022

OpenAPI Terraform Provider that configures itself at runtime with the resources exposed by the service provider (defined in a swagger file)

OpenAPI Terraform Provider that configures itself at runtime with the resources exposed by the service provider (defined in a swagger file)

Terraform Provider OpenAPI This terraform provider aims to minimise as much as possible the efforts needed from service providers to create and mainta

Dec 26, 2022

signature-based file format identification

signature-based file format identification

Siegfried Siegfried is a signature-based file format identification tool, implementing: the National Archives UK's PRONOM file format signatures freed

Dec 15, 2022
APKrash is an Android APK security analysis toolkit focused on comparing APKs to detect tampering and repackaging.
 APKrash is an Android APK security analysis toolkit focused on comparing APKs to detect tampering and repackaging.

APKrash APKrash is an Android APK security analysis toolkit focused on comparing APKs to detect tampering and repackaging. Features Able to analyze pu

Nov 8, 2022
Copy files and artifacts via SSH using a binary, docker or Drone CI.

drone-scp Copy files and artifacts via SSH using a binary, docker or Drone CI. Feature Support routines. Support wildcard pattern on source list. Supp

Dec 6, 2022
Go package that aids in binary analysis and exploitation

sploit Sploit is a Go package that aids in binary analysis and exploitation. The motivating factor behind the development of sploit is to be able to h

Jan 1, 2023
Common Expression Language -- specification and binary representation

The Common Expression Language (CEL) implements common semantics for expression evaluation, enabling different applications to more easily interoperate.

Jan 8, 2023
Nvidia GPU exporter for prometheus using nvidia-smi binary
Nvidia GPU exporter for prometheus using nvidia-smi binary

nvidia_gpu_exporter Nvidia GPU exporter for prometheus, using nvidia-smi binary to gather metrics. Introduction There are many Nvidia GPU exporters ou

Jan 5, 2023
Binary program to restart unhealthy Docker containers

DeUnhealth Restart your unhealthy containers safely Features Restart unhealthy containers marked with deunhealth.restart.on.unhealthy=true label Recei

Dec 22, 2022
k6 extension supporting avro textual and binary representations

xk6-avro This extension wraps the goavro library into a k6 extension. You can build the extension using: xk6 build --with github.com/xvzf/xk6-avro Exa

Dec 8, 2021
Simple binary reader and writer
Simple binary reader and writer

Simple Binary Stream Reader/Writer This package contains a set of simple utility reader and writer that can be used to efficiently read/write binary i

Oct 30, 2022
Lightweight, single-binary Backup Repository client. Part of E2E Backup Architecture designed by RiotKit

Backup Maker Tiny backup client packed in a single binary. Interacts with a Backup Repository server to store files, uses GPG to secure your backups e

Apr 4, 2022
Running Go binary into Docker

This go file make a get into an API, that API provides a JSON with a cat information

Feb 7, 2022