The file type is detected by checking the magic number of the data.
This is swift port of file-type
import PackageDescription
let package = Package(
name: "MyApp",
dependencies: [
.package(url: "https://github.com/velocityzen/FileType", from: "1.0.3")
]
)
Inspect mime type
import FileType
let path = "/path/to/some-file.jpg"
let url = URL(fileURLWithPath: path, isDirectory: false)
let data = try! Data(contentsOf: url)
let fileType = FileType.getFor(data: data)
fileType?.type == .jpg // true
fileType! // FileType(type: .jpg, ext: "jpg", mime: "image/jpeg")
returns all file types and mime information
returns file type detected by checking the magic number
returns bytes count needed to detect file type
returns max bytes count needed to detect file types
3g2
- Multimedia container format defined by the 3GPP2 for 3G CDMA2000 multimedia services3gp
- Multimedia container format defined by the Third Generation Partnership Project (3GPP) for 3G UMTS multimedia services3mf
- 3D Manufacturing Format7z
aac
- Advanced Audio Codingac3
- ATSC A/52 Audio Fileai
- Adobe Illustrator Artworkaif
alias
- macOS Alias fileamr
ape
- Monkey's Audioapng
- Animated Portable Network Graphicsar
arrow
- Columnar format for tables of dataarw
- Sony Alpha Raw image fileasar
- Simple extensive tar-like archive format with indexingasf
- Advanced Systems Formatavi
avif
- AV1 Image File Formatblend
bmp
bpg
bz2
cab
cfb
chm
- Microsoft Compiled HTML Helpcr2
- Canon Raw image file (v2)cr3
- Canon Raw image file (v3)crx
cur
dcm
- DICOM Image Filedeb
dmg
dng
- Adobe Digital Negative image filedocx
dsf
- Sony DSD Stream File (DSF)elf
- Unix Executable and Linkable Formateot
eps
- Encapsulated PostScriptepub
exe
f4a
- Audio-only ISO base media file format used by Adobe Flash Playerf4b
- Audiobook and podcast ISO base media file format used by Adobe Flash Playerf4p
- ISO base media file format protected by Adobe Access DRM used by Adobe Flash Playerf4v
- ISO base media file format used by Adobe Flash Playerflac
flif
flv
gif
glb
- GL Transmission Formatgz
heic
icns
ico
ics
- iCalendarindd
it
- Audio module format: Impulse Trackerjp2
- JPEG 2000jpg
jpm
- JPEG 2000jpx
- JPEG 2000jxl
- JPEG XL image formatjxr
ktx
lnk
- Microsoft Windows file shortcutlz
lzh
- LZH archivem4a
- Audio-only MPEG-4 filesm4b
- Audiobook and podcast MPEG-4 files, which also contain metadata including chapter markers, images, and hyperlinksm4p
- MPEG-4 files with audio streams encrypted by FairPlay Digital Rights Management as were sold through the iTunes Storem4v
- MPEG-4 Visual bitstreamsmid
mie
- Dedicated meta information format which supports storage of binary as well as textual meta informationmj2
- Motion JPEG 2000mkv
mobi
- Mobipocketmov
mp1
- MPEG-1 Audio Layer Imp2
mp3
mp4
mpc
- Musepack (SV7 & SV8)mpg
mts
- MPEG-2 Transport Stream, both raw and Blu-ray Disc Audio-Video (BDAV) versionsmxf
nef
- Nikon Electronic Format image filenes
odp
- OpenDocument for presentationsods
- OpenDocument for spreadsheetsodt
- OpenDocument for word processingoga
ogg
ogm
ogv
ogx
opus
orf
- Olympus Raw image fileotf
pcap
- Libpcap File Formatpdf
pgp
- Pretty Good Privacypng
pptx
ps
psd
qcp
raf
- Fujifilm RAW image filerar
rpm
rtf
rw2
- Panasonic RAW image files3m
- Audio module format: ScreamTracker 3shp
- Geospatial vector data formatskp
- SketchUpspx
sqlite
stl
- Standard Tesselated Geometry File Format (ASCII only)swf
tar
tif
ttf
vcf
- vCardvoc
- Creative Voice Filewasm
wav
webm
webp
woff2
woff
wv
- WavPackxcf
- eXperimental Computing Facilityxlsx
xm
- Audio module format: FastTracker 2xml
xpi
xz
Z
zip
zst
- Archive file
Pull requests are welcome for additional commonly used file types.
swift test