This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Test-IsFileBinary.ps1
124 lines (101 loc) · 3.2 KB
/
Test-IsFileBinary.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Test-IsFileBinary.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Test if a file is binary
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Test if a file is binary
.DESCRIPTION
Test if a file is binary like .exe or .zip.
I found this code snippet on Stackoverflow:
https://stackoverflow.com/questions/1077634/powershell-search-script-that-ignores-binary-files
.EXAMPLE
Test-IsFileBinary -FilePath "E:\Temp\Files\File_01.txt"
False
.EXAMPLE
Test-IsFileBinary -FilePath "E:\Temp\Files\File_04.zip"
True
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Function/Test-IsFileBinary.README.md
#>
function Test-IsFileBinary
{
[CmdletBinding()]
[OutputType('System.Boolean')]
Param(
[Parameter(
Position=0,
Mandatory=$true,
HelpMessage="Path to file which should be checked")]
[ValidateScript({
if(Test-Path -Path $_ -PathType Leaf)
{
return $true
}
else
{
throw "Enter a valid file path!"
}
})]
[String]$FilePath
)
Begin{
}
Process{
# Encoding variable
$Encoding = [String]::Empty
# Get the first 1024 bytes from the file
$ByteCount = 1024
$ByteArray = Get-Content -Path $FilePath -Encoding Byte -TotalCount $ByteCount
if($ByteArray.Count -ge $ByteCount)
{
Write-Verbose -Message "Could only read $($ByteArray.Count)/$ByteCount Bytes. File "
}
if(($ByteArray.Count -ge 4) -and (("{0:X}{1:X}{2:X}{3:X}" -f $ByteArray) -eq "FFFE0000"))
{
Write-Verbose -Message "UTF-32 detected!"
$Encoding = "UTF-32"
}
elseif(($ByteArray.Count -ge 4) -and (("{0:X}{1:X}{2:X}{3:X}" -f $ByteArray) -eq "0000FEFF"))
{
Write-Verbose -Message "UTF-32 BE detected!"
$Encoding = "UTF-32 BE"
}
elseif(($ByteArray.Count -ge 3) -and (("{0:X}{1:X}{2:X}" -f $ByteArray) -eq "EFBBBF"))
{
Write-Verbose -Message "UTF-8 detected!"
$Encoding = "UTF-8"
}
elseif(($ByteArray.Count -ge 2) -and (("{0:X}{1:X}" -f $ByteArray) -eq "FFFE"))
{
Write-Verbose -Message "UTF-16 detected!"
$Encoding = "UTF-16"
}
elseif(($ByteArray.Count -ge 2) -and (("{0:X}{1:X}" -f $ByteArray) -eq "FEFF"))
{
Write-Verbose "UTF-16 BE detected!"
$Encoding = "UTF-16 BE"
}
if(-not([String]::IsNullOrEmpty($Encoding)))
{
Write-Verbose -Message "File is text encoded!"
return $false
}
# So now we're done with Text encodings that commonly have '0's
# in their byte steams. ASCII may have the NUL or '0' code in
# their streams but that's rare apparently.
# Both GNU Grep and Diff use variations of this heuristic
if($byteArray -contains 0 )
{
Write-Verbose -Message "File is a binary!"
return $true
}
Write-Verbose -Message "File should be ASCII encoded!"
return $false
}
End{
}
}