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 Format7zaac- Advanced Audio Codingac3- ATSC A/52 Audio Fileai- Adobe Illustrator Artworkaifalias- macOS Alias fileamrape- Monkey's Audioapng- Animated Portable Network Graphicsararrow- Columnar format for tables of dataarw- Sony Alpha Raw image fileasar- Simple extensive tar-like archive format with indexingasf- Advanced Systems Formataviavif- AV1 Image File Formatblendbmpbpgbz2cabcfbchm- Microsoft Compiled HTML Helpcr2- Canon Raw image file (v2)cr3- Canon Raw image file (v3)crxcurdcm- DICOM Image Filedebdmgdng- Adobe Digital Negative image filedocxdsf- Sony DSD Stream File (DSF)elf- Unix Executable and Linkable Formateoteps- Encapsulated PostScriptepubexef4a- 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 Playerflacflifflvgifglb- GL Transmission Formatgzheicicnsicoics- iCalendarinddit- Audio module format: Impulse Trackerjp2- JPEG 2000jpgjpm- JPEG 2000jpx- JPEG 2000jxl- JPEG XL image formatjxrktxlnk- Microsoft Windows file shortcutlzlzh- 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 bitstreamsmidmie- Dedicated meta information format which supports storage of binary as well as textual meta informationmj2- Motion JPEG 2000mkvmobi- Mobipocketmovmp1- MPEG-1 Audio Layer Imp2mp3mp4mpc- Musepack (SV7 & SV8)mpgmts- MPEG-2 Transport Stream, both raw and Blu-ray Disc Audio-Video (BDAV) versionsmxfnef- Nikon Electronic Format image filenesodp- OpenDocument for presentationsods- OpenDocument for spreadsheetsodt- OpenDocument for word processingogaoggogmogvogxopusorf- Olympus Raw image fileotfpcap- Libpcap File Formatpdfpgp- Pretty Good Privacypngpptxpspsdqcpraf- Fujifilm RAW image filerarrpmrtfrw2- Panasonic RAW image files3m- Audio module format: ScreamTracker 3shp- Geospatial vector data formatskp- SketchUpspxsqlitestl- Standard Tesselated Geometry File Format (ASCII only)swftartifttfvcf- vCardvoc- Creative Voice Filewasmwavwebmwebpwoff2woffwv- WavPackxcf- eXperimental Computing Facilityxlsxxm- Audio module format: FastTracker 2xmlxpixzZzipzst- Archive file
Pull requests are welcome for additional commonly used file types.
swift test