|
param ( |
|
[Parameter(Mandatory = $true)] |
|
[string]$FolderPath, |
|
[Parameter(Mandatory = $true)] |
|
[string]$SearchString |
|
) |
|
|
|
Add-Type -AssemblyName System.IO.Compression.FileSystem |
|
|
|
$matchedPaths = [System.Collections.Generic.List[string]]::new() |
|
|
|
function Search-Jar { |
|
param ( |
|
[System.IO.Stream]$Stream, |
|
[string]$JarPath, |
|
[string]$SearchString |
|
) |
|
|
|
try { |
|
$zip = New-Object System.IO.Compression.ZipArchive($Stream, [System.IO.Compression.ZipArchiveMode]::Read) |
|
|
|
foreach ($entry in $zip.Entries) { |
|
if ([string]::IsNullOrWhiteSpace($entry.Name)) { |
|
continue |
|
} |
|
|
|
$entryFullPath = "$JarPath -> $($entry.FullName)" |
|
|
|
if ($entry.FullName -imatch '\.jar$') { |
|
Write-Output "Searching nested JAR: $entryFullPath" |
|
try { |
|
$nestedStream = $entry.Open() |
|
$memStream = New-Object System.IO.MemoryStream |
|
$nestedStream.CopyTo($memStream) |
|
$nestedStream.Close() |
|
$memStream.Position = 0 |
|
Search-Jar -Stream $memStream -JarPath $entryFullPath -SearchString $SearchString |
|
$memStream.Dispose() |
|
} |
|
catch { |
|
Write-Warning "Failed to process nested JAR: $entryFullPath" |
|
} |
|
continue |
|
} |
|
|
|
# Search all other entries as text |
|
try { |
|
$entryStream = $entry.Open() |
|
$reader = New-Object System.IO.StreamReader($entryStream) |
|
$content = $reader.ReadToEnd() |
|
if ($content -imatch [regex]::Escape($SearchString)) { |
|
Write-Host "Match found: $entryFullPath" -ForegroundColor Green |
|
$matchedPaths.Add($entryFullPath) |
|
} |
|
$reader.Close() |
|
$entryStream.Close() |
|
} |
|
catch { |
|
continue |
|
} |
|
} |
|
|
|
$zip.Dispose() |
|
} |
|
catch { |
|
Write-Warning "Failed to read ZIP/JAR stream at: $JarPath" |
|
} |
|
} |
|
|
|
$jarFiles = Get-ChildItem -Path $FolderPath -Filter *.jar -File |
|
|
|
foreach ($jar in $jarFiles) { |
|
Write-Output "Searching JAR: $($jar.Name)" |
|
try { |
|
$fileStream = [System.IO.File]::OpenRead($jar.FullName) |
|
Search-Jar -Stream $fileStream -JarPath $jar.Name -SearchString $SearchString |
|
$fileStream.Dispose() |
|
} |
|
catch { |
|
Write-Warning "Failed to open JAR: $($jar.FullName)" |
|
} |
|
} |
|
|
|
Write-Output "" |
|
if ($matchedPaths.Count -eq 0) { |
|
Write-Output "No matches found in any scanned JAR." |
|
} else { |
|
Write-Output "Found $($matchedPaths.Count) match(es):" |
|
foreach ($path in $matchedPaths) { |
|
Write-Host " $path" -ForegroundColor Green |
|
} |
|
} |