isBinaryFile function
Check if a file is binary (non-text).
Implementation
Future<bool> isBinaryFile(String path) async {
final file = File(path);
if (!await file.exists()) return false;
// Check extension first
final ext = p.extension(path).replaceFirst('.', '').toLowerCase();
const binaryExts = {
'png',
'jpg',
'jpeg',
'gif',
'webp',
'bmp',
'ico',
'mp3',
'wav',
'ogg',
'mp4',
'avi',
'mov',
'zip',
'tar',
'gz',
'7z',
'rar',
'pdf',
'doc',
'docx',
'xls',
'xlsx',
'exe',
'dll',
'so',
'dylib',
'woff',
'woff2',
'ttf',
'otf',
'sqlite',
'db',
};
if (binaryExts.contains(ext)) return true;
// Read first 8KB and check for null bytes
try {
final raf = file.openSync();
try {
final bytes = raf.readSync(8192);
return bytes.any((b) => b == 0);
} finally {
raf.closeSync();
}
} catch (_) {
return false;
}
}