Skip to content

Commit

Permalink
PowerShell script for listing file type counts in JAR files
Browse files Browse the repository at this point in the history
  • Loading branch information
szeweq committed May 21, 2024
1 parent b948e0f commit 9470e64
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions listfiles.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
param($dir)

# Initialize an empty dictionary to store extension counts
$extensionCounts = @{}

# Example paths in format (<jar file name>: <entry path>) found for each extension
$exemplePaths = @{}

# Get all JAR files in the directory
Get-ChildItem -Path $dir -Filter "*.jar" | ForEach-Object {

# Extract the JAR file path
$jarPath = $_.FullName
$jarName = $_.Name

# Open the JAR file for reading using ZipFile
$zipFile = [IO.Compression.ZipFile]::OpenRead($jarPath)

# Loop through each entry in the ZIP file
$zipFile.Entries | Where-Object { !$_.IsDirectory } | ForEach-Object {
# Get the entry name and potential extension
$entryName = $_.Name
$entryPath = $_.FullName

# Check if the entry name contains a dot (indicating extension)
# Extract the extension (lowercase for case-insensitivity)
$extension = $entryName.Split('.')[-1].ToLower()

# Check if the extension is not empty
if (($extension -ne "") -and ($extension -ne $entryName)) {
# Add or increment the count for the extension in the dictionary
if ($extensionCounts.ContainsKey($extension)) {
$extensionCounts[$extension]++
} else {
$extensionCounts[$extension] = 1
$exemplePaths[$extension] = $jarName + ": " + $entryPath
}
}
}

# Close the ZIP file (important for proper resource handling)
$zipFile.Dispose()
}

$keysSorted = $extensionCounts.GetEnumerator() | Sort-Object -Property Value -Descending

# Display the extension counts
foreach ($ext in $keysSorted) {
if ($ext.Value -gt 1) {
Write-Host "$($ext.Key): $($ext.Value)"
Write-Host "> Example: $($exemplePaths[$ext.Key])"
}
}

0 comments on commit 9470e64

Please sign in to comment.