{"id":13414132,"url":"https://github.com/h2non/filetype","last_synced_at":"2025-05-13T20:03:13.069Z","repository":{"id":37686553,"uuid":"43056808","full_name":"h2non/filetype","owner":"h2non","description":"Fast, dependency-free Go package to infer binary file types based on the magic numbers header signature","archived":false,"fork":false,"pushed_at":"2025-03-15T20:15:36.000Z","size":9561,"stargazers_count":2186,"open_issues_count":46,"forks_count":184,"subscribers_count":30,"default_branch":"master","last_synced_at":"2025-05-06T19:51:56.783Z","etag":null,"topics":["binary","discovery","extension","filetype","golang","lookup","magic-number","magic-numbers","mime","mime-types","validation"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/h2non/filetype?tab=doc","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/h2non.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-24T09:15:51.000Z","updated_at":"2025-05-05T11:30:29.000Z","dependencies_parsed_at":"2023-02-13T04:55:18.634Z","dependency_job_id":"2060748c-3e62-4f49-963a-e9307f7e3d98","html_url":"https://github.com/h2non/filetype","commit_stats":{"total_commits":161,"total_committers":48,"mean_commits":"3.3541666666666665","dds":0.5093167701863355,"last_synced_commit":"6469358c2bcb2c03f72e6262d03aed9d057e9213"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h2non%2Ffiletype","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h2non%2Ffiletype/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h2non%2Ffiletype/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/h2non%2Ffiletype/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/h2non","download_url":"https://codeload.github.com/h2non/filetype/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254020473,"owners_count":22000750,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary","discovery","extension","filetype","golang","lookup","magic-number","magic-numbers","mime","mime-types","validation"],"created_at":"2024-07-30T20:01:58.703Z","updated_at":"2025-05-13T20:03:13.040Z","avatar_url":"https://github.com/h2non.png","language":"Go","readme":"# filetype [![GoDoc](https://godoc.org/github.com/h2non/filetype?status.svg)](https://godoc.org/github.com/h2non/filetype) [![Go Version](https://img.shields.io/badge/go-v1.0+-green.svg?style=flat)](https://github.com/h2non/gentleman)\n\nSmall and dependency free [Go](https://golang.org) package to infer file and MIME type checking the [magic numbers](\u003chttps://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files\u003e) signature.\n\nFor SVG file type checking, see [go-is-svg](https://github.com/h2non/go-is-svg) package. Python port: [filetype.py](https://github.com/h2non/filetype.py).\n\n## Features\n\n- Supports a [wide range](#supported-types) of file types\n- Provides file extension and proper MIME type\n- File discovery by extension or MIME type\n- File discovery by class (image, video, audio...)\n- Provides a bunch of helpers and file matching shortcuts\n- [Pluggable](#add-additional-file-type-matchers): add custom new types and matchers\n- Simple and semantic API\n- [Blazing fast](#benchmarks), even processing large files\n- Only first 262 bytes representing the max file header is required, so you can just [pass a slice](#file-header)\n- Dependency free (just Go code, no C compilation needed)\n- Cross-platform file recognition\n\n## Installation\n\n```bash\ngo get github.com/h2non/filetype\n```\n\n## API\n\nSee [Godoc](https://godoc.org/github.com/h2non/filetype) reference.\n\n### Subpackages\n\n- [`github.com/h2non/filetype/types`](https://godoc.org/github.com/h2non/filetype/types)\n- [`github.com/h2non/filetype/matchers`](https://godoc.org/github.com/h2non/filetype/matchers)\n\n## Examples\n\n#### Simple file type checking\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"io/ioutil\"\n\n  \"github.com/h2non/filetype\"\n)\n\nfunc main() {\n  buf, _ := ioutil.ReadFile(\"sample.jpg\")\n\n  kind, _ := filetype.Match(buf)\n  if kind == filetype.Unknown {\n    fmt.Println(\"Unknown file type\")\n    return\n  }\n\n  fmt.Printf(\"File type: %s. MIME: %s\\n\", kind.Extension, kind.MIME.Value)\n}\n```\n\n#### Check type class\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"io/ioutil\"\n\n  \"github.com/h2non/filetype\"\n)\n\nfunc main() {\n  buf, _ := ioutil.ReadFile(\"sample.jpg\")\n\n  if filetype.IsImage(buf) {\n    fmt.Println(\"File is an image\")\n  } else {\n    fmt.Println(\"Not an image\")\n  }\n}\n```\n\n#### Supported type\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n\n  \"github.com/h2non/filetype\"\n)\n\nfunc main() {\n  // Check if file is supported by extension\n  if filetype.IsSupported(\"jpg\") {\n    fmt.Println(\"Extension supported\")\n  } else {\n    fmt.Println(\"Extension not supported\")\n  }\n\n  // Check if file is supported by mime type\n  if filetype.IsMIMESupported(\"image/jpeg\") {\n    fmt.Println(\"MIME type supported\")\n  } else {\n    fmt.Println(\"MIME type not supported\")\n  }\n}\n```\n\n#### File header\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"os\"\n\n  \"github.com/h2non/filetype\"\n)\n\nfunc main() {\n  // Open a file descriptor\n  file, _ := os.Open(\"movie.mp4\")\n\n  // We only have to pass the file header = first 261 bytes\n  head := make([]byte, 261)\n  file.Read(head)\n\n  if filetype.IsImage(head) {\n    fmt.Println(\"File is an image\")\n  } else {\n    fmt.Println(\"Not an image\")\n  }\n}\n```\n\n#### Add additional file type matchers\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n\n  \"github.com/h2non/filetype\"\n)\n\nvar fooType = filetype.NewType(\"foo\", \"foo/foo\")\n\nfunc fooMatcher(buf []byte) bool {\n  return len(buf) \u003e 1 \u0026\u0026 buf[0] == 0x01 \u0026\u0026 buf[1] == 0x02\n}\n\nfunc main() {\n  // Register the new matcher and its type\n  filetype.AddMatcher(fooType, fooMatcher)\n\n  // Check if the new type is supported by extension\n  if filetype.IsSupported(\"foo\") {\n    fmt.Println(\"New supported type: foo\")\n  }\n\n  // Check if the new type is supported by MIME\n  if filetype.IsMIMESupported(\"foo/foo\") {\n    fmt.Println(\"New supported MIME type: foo/foo\")\n  }\n\n  // Try to match the file\n  fooFile := []byte{0x01, 0x02}\n  kind, _ := filetype.Match(fooFile)\n  if kind == filetype.Unknown {\n    fmt.Println(\"Unknown file type\")\n  } else {\n    fmt.Printf(\"File type matched: %s\\n\", kind.Extension)\n  }\n}\n```\n\n## Supported types\n\n#### Image\n\n- **jpg** - `image/jpeg`\n- **png** - `image/png`\n- **gif** - `image/gif`\n- **webp** - `image/webp`\n- **cr2** - `image/x-canon-cr2`\n- **tif** - `image/tiff`\n- **bmp** - `image/bmp`\n- **heif** - `image/heif`\n- **jxr** - `image/vnd.ms-photo`\n- **psd** - `image/vnd.adobe.photoshop`\n- **ico** - `image/vnd.microsoft.icon`\n- **dwg** - `image/vnd.dwg`\n- **avif** - `image/avif`\n\n#### Video\n\n- **mp4** - `video/mp4`\n- **m4v** - `video/x-m4v`\n- **mkv** - `video/x-matroska`\n- **webm** - `video/webm`\n- **mov** - `video/quicktime`\n- **avi** - `video/x-msvideo`\n- **wmv** - `video/x-ms-wmv`\n- **mpg** - `video/mpeg`\n- **flv** - `video/x-flv`\n- **3gp** - `video/3gpp`\n\n#### Audio\n\n- **mid** - `audio/midi`\n- **mp3** - `audio/mpeg`\n- **m4a** - `audio/mp4`\n- **ogg** - `audio/ogg`\n- **flac** - `audio/x-flac`\n- **wav** - `audio/x-wav`\n- **amr** - `audio/amr`\n- **aac** - `audio/aac`\n- **aiff** - `audio/x-aiff`\n\n#### Archive\n\n- **epub** - `application/epub+zip`\n- **zip** - `application/zip`\n- **tar** - `application/x-tar`\n- **rar** - `application/vnd.rar`\n- **gz** - `application/gzip`\n- **bz2** - `application/x-bzip2`\n- **7z** - `application/x-7z-compressed`\n- **xz** - `application/x-xz`\n- **zstd** - `application/zstd`\n- **pdf** - `application/pdf`\n- **exe** - `application/vnd.microsoft.portable-executable`\n- **swf** - `application/x-shockwave-flash`\n- **rtf** - `application/rtf`\n- **iso** - `application/x-iso9660-image`\n- **eot** - `application/octet-stream`\n- **ps** - `application/postscript`\n- **sqlite** - `application/vnd.sqlite3`\n- **nes** - `application/x-nintendo-nes-rom`\n- **crx** - `application/x-google-chrome-extension`\n- **cab** - `application/vnd.ms-cab-compressed`\n- **deb** - `application/vnd.debian.binary-package`\n- **ar** - `application/x-unix-archive`\n- **Z** - `application/x-compress`\n- **lz** - `application/x-lzip`\n- **rpm** - `application/x-rpm`\n- **elf** - `application/x-executable`\n- **dcm** - `application/dicom`\n\n#### Documents\n\n- **doc** - `application/msword`\n- **docx** - `application/vnd.openxmlformats-officedocument.wordprocessingml.document`\n- **xls** - `application/vnd.ms-excel`\n- **xlsx** - `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet`\n- **ppt** - `application/vnd.ms-powerpoint`\n- **pptx** - `application/vnd.openxmlformats-officedocument.presentationml.presentation`\n\n#### Font\n\n- **woff** - `application/font-woff`\n- **woff2** - `application/font-woff`\n- **ttf** - `application/font-sfnt`\n- **otf** - `application/font-sfnt`\n\n#### Application\n\n- **wasm** - `application/wasm`\n- **dex** - `application/vnd.android.dex`\n- **dey** - `application/vnd.android.dey`\n\n## Benchmarks\n\nMeasured using [real files](https://github.com/h2non/filetype/tree/master/fixtures).\n\nEnvironment: OSX x64 i7 2.7 Ghz\n\n```bash\nBenchmarkMatchTar-8    1000000        1083 ns/op\nBenchmarkMatchZip-8    1000000        1162 ns/op\nBenchmarkMatchJpeg-8   1000000        1280 ns/op\nBenchmarkMatchGif-8    1000000        1315 ns/op\nBenchmarkMatchPng-8    1000000        1121 ns/op\n```\n\n## License\n\nMIT - Tomas Aparicio\n","funding_links":[],"categories":["Misc","开源类库","实用工具","Go","Utilities","Open source library","工具库","公用事业公司","Utility","實用工具","工具库`可以提升效率的通用代码库和工具`"],"sub_categories":["文件/存储","Advanced Console UIs","Utility/Miscellaneous","Files/Storage","HTTP Clients","查询语","实用程序/Miscellaneous","交流","高級控制台界面","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","Fail injection","高级控制台界面"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh2non%2Ffiletype","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fh2non%2Ffiletype","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fh2non%2Ffiletype/lists"}