Skip to content

Defacto2/magicnumber

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Defacto2 / magicnumber

Go Reference

The magicnumber package reads the bytes within a file to learn of the format or type of file, being useful for identifying the file without relying on the file extension or MIME type. See the reference documentation for additional usage and examples.

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/Defacto2/magicnumber"
)

func main() {
    file, err := os.Open("example.exe")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    // Option 1.
    result := magicnumber.Find(file)
    fmt.Printf("File type: %s\n", result)

    // Option 2.
    valid, result, err := magicnumber.MatchExt("example.exe", file)
    if err != nil {
		    fmt.Fprintln(os.Stderr, err)
    }
    fmt.Printf("File type: %s\n", result)
    fmt.Printf("File extension valid: %v\n", valid)
}

About

A magic number / magic byte Go package to read files and discover the format type.

Topics

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages