A Go library for detecting file types by content (magic bytes) and filename. Zero dependencies outside the standard library.
- Detects 30+ file formats from binary content alone
- Parses OLE2 (Compound Binary File) internal directory to distinguish
.doc/.xls/.ppt - Identifies ZIP-based subtypes: OOXML (docx/xlsx/pptx), ODF (odt/ods/odp)
- Supports WPS Office formats (wps/et/dps) via filename hint
- Falls back to
net/http.DetectContentTypefor common MIME types - Filename suffix fallback for ambiguous or content-undetectable formats
go get github.com/servicewall/filetypeimport "github.com/servicewall/filetype"
// Detect using both filename and content (recommended)
ext := filetype.Detect("report.docx", fileBytes)
// ext == "docx"
// Detect using content only
ext := filetype.DetectByContent(fileBytes)
// ext == "pdf"
// Check specific format
if filetype.IsZip(fileBytes) {
subtype := filetype.DetectZipSubtype(fileBytes)
}
if filetype.IsOLE2(fileBytes) {
subtype := filetype.DetectOLE2Type(fileBytes)
}| Category | Extensions |
|---|---|
| Office (OOXML) | docx, xlsx, pptx |
| Office (OLE2) | doc, xls, ppt |
| Office (ODF) | odt, ods, odp |
| WPS Office | wps, et, dps |
| Documents | pdf, rtf |
| Images | jpg, png, gif, bmp, webp, tif, pcx |
| Archives | zip, gz, tar, rar, 7z, xz |
| Markup | html, xml |
| Text | txt, csv |
Primary detection function. Uses content-based detection with filename as fallback for ambiguous formats (WPS Office, unrecognized OLE2). Returns the file extension (e.g. "pdf", "docx") or "" if unknown.
Detects file type purely from content bytes, without any filename hint.
Checks if data starts with the PK (ZIP) magic bytes.
Inspects ZIP content to identify OOXML, ODF, or plain zip.
Checks if data starts with the OLE2 magic signature.
Parses OLE2 directory entries to identify doc/xls/ppt.
- WPS Office filename extensions (wps/et/dps) with matching OLE2/ZIP content
- ZIP magic bytes -> OOXML / ODF / plain zip
- OLE2 magic bytes -> parse directory for doc/xls/ppt
- Custom magic byte table (RTF, 7z, XZ, TIFF, PCX)
- TAR magic at offset 257
net/http.DetectContentTypeMIME-based detection- Filename suffix fallback (txt, csv, htm, xhtml, jpeg, tiff, tgz, xml)
MIT