file_magic_number 0.7.2
file_magic_number: ^0.7.2 copied to clipboard
A Flutter package to detect file types based on their magic number instead of MIME types. Supports Flutter on mobile, desktop, and web without native code.
File Magic Number
A Dart & Flutter package to detect file types based on their magic number instead of relying on MIME types. Works on Flutter for mobile, desktop, and web without requiring native code.
🚀 Features #
- Detects file types using their magic number (signature bytes)
- Supports Flutter on Android, iOS, macOS, Windows, Linux, and Web
- No need for native plugins
- Lightweight and easy to extend with custom signatures
📌 Installation #
Add the dependency to your pubspec.yaml
:
dependencies:
file_magic_number: latest_version
Then for Dart, run:
dart pub get
Or for Flutter, run:
flutter pub get
🛠️ Usage #
Detect a file type from bytes #
import 'package:file_magic_number/file_magic_number.dart';
void main() async {
final bytes = Uint8List.fromList([0x25, 0x50, 0x44, 0x46]);
final MagicNumberType fileType = MagicNumber.detectFileType(bytes);
print(fileType);
}
Detect a file type from file_picker #
Integrating file_magic_number with file_picker allows you to easily detect the type of a file selected by the user without relying on MIME types. You can use file_picker to open the file dialog and then pass the file's bytes to MagicNumber.detectFileType to identify its type. Here's how you can do it:
import 'package:file_magic_number/file_magic_number.dart';
import 'package:file_picker/file_picker.dart';
void main() async {
FilePickerResult? result = await FilePicker.platform.pickFiles(withData: true);
if (result != null) {
final MagicNumberType fileType = MagicNumber.detectFileType(result.files.single.bytes!);
print(fileType);
}
}
🎯 Supported File Types #
File Type | Magic Number (Hex) |
---|---|
ZIP | 50 4B 03 04 |
RAR | 52 61 72 21 1A 07 00 |
RAR | 52 61 72 21 1A 07 |
7Z | 37 7A BC AF 27 1C |
25 50 44 46 | |
PNG | 89 50 4E 47 0D 0A 1A 0A |
JPG | FF D8 FF |
GIF | 47 49 46 38 |
TIFF | 49 49 2A 00 / 4D 4D 00 2A |
BMP | 42 4D |
MP3 | 49 44 33 |
WAV | 52 49 46 46 |
MP4 | 66 74 79 70 |
ELF | 7F 45 4C 46 |
EXE | 4D 5A |
TAR | 75 73 74 61 72 |
SQLite | 53 51 4C 69 74 65 |
📌 Contributing #
Feel free to contribute by adding more file signatures or improving the implementation. Fork the repo and submit a PR!
📜 License #
This project is licensed under the MIT License.