Get-ArchiveEntries.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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
#Set-StrictMode -Version Latest ##################################################### # Get-ArchiveEntries ##################################################### <#PSScriptInfo .VERSION 0.3 .GUID 9cc904fc-341c-4873-ace6-c37f8c1e2f13 .AUTHOR David Walker, Sitecore Dave, Radical Dave .COMPANYNAME David Walker, Sitecore Dave, Radical Dave .COPYRIGHT David Walker, Sitecore Dave, Radical Dave .TAGS powershell archive files entries zip get .LICENSEURI https://github.com/SharedSitecore/ConvertTo-Sitecore-WDP/blob/main/LICENSE .PROJECTURI https://github.com/SharedSitecore/ConvertTo-Sitecore-WDP .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .DESCRIPTION PowerShell script to show/search entries/files in Zip package .PARAMETER name Path of package #> ##################################################### # Get-ArchiveEntries ##################################################### Param( [Parameter(Mandatory=$false)] [string] $path, [Parameter(Mandatory=$false)] [string[]] $search = @(), [switch] $recurse = $false ) $ProgressPreference = 'SilentlyContinue' $ErrorActionPreference = 'Stop' $PSScriptVerson = (Test-ScriptFileInfo -Path $MyInvocation.MyCommand.Path | Select-Object -ExpandProperty Version) $PSScriptName = ($MyInvocation.MyCommand.Name.Replace(".ps1","")) $PSCallingScript = if ($MyInvocation.PSCommandPath) { $MyInvocation.PSCommandPath | Split-Path -Parent } else { $null } Write-Verbose "$PSScriptRoot\$PSScriptName v$PSScriptVerson $path called by:$PSCallingScript" $results = [System.Collections.Generic.List[object]]@() if (!$path) {$path = '*.zip'} Write-Verbose "path:$path" $paths = [System.Collections.Generic.List[string]]@() if ($path.IndexOf('*') -ne -1 -or $path.StartsWith('/') -or $path.StartsWith('\') -or $path.StartsWith('./') -or $path.StartsWith('.\')) { if ($path.IndexOf(':') -eq -1) { Write-Verbose "addpath:$PSScriptRoot\$path" $paths.Add((Join-Path $PSScriptRoot $path)) #$addpath = Join-Path $PSScriptRoot $path } Write-Verbose "path:$path" if ($path.IndexOf('*') -ne -1) { #$addpaths = (Get-ChildItem -Path "$path").FullName $paths = (Get-ChildItem -Path "$path").FullName } } else { if (!(Test-Path $path)) { $path = Get-Location $path } if (!(Test-Path $path)) { Write-Verbose "$PSScriptName - path not found:$path" } else { Write-Verbose "addpath:$path" #$addpath = "$PSScriptRoot\$path" #$paths.Add("$PSScriptRoot\$path") $paths.Add("$path") } } #Write-Verbose "addpath:$addpath" #Write-Verbose "3path:$path" #if (!$addpaths) { # Write-Verbose "paths:$($paths.Length)" # $paths.AddRange($addpaths) # Write-Verbose "paths:$($paths.Length)" #} #if (!$addpath) { $paths.Add($addpath) } Write-Verbose "paths:$($paths.Count)" try { foreach ($path in $paths) { #$paths.foreach({ #Write-Verbose "check:$_" Write-Verbose "check:$path" if ($path) { #$path = $_ Write-Verbose "path:$path" if (!(Test-Path $path)) { $currPath = Join-Path (Get-Location) $path Write-Verbose "currPath:$currPath" if (Test-Path $currPath) { $path = $currPath } else { throw "ERROR $PSScriptName - file not found: $path" } } $file = (Split-Path $path -leaf).Replace('.zip', '') Add-Type -AssemblyName System.IO.Compression Write-Verbose '####################################################################################################' Write-Verbose "$PSScriptName:Opening stream for $path" $stream = New-Object IO.FileStream($path, [IO.FileMode]::Open) $zip = New-Object IO.Compression.ZipArchive($stream) if (!$search) { Write-Verbose 'All except .zip' #$results += ($zip.Entries | Where-Object { (-not ($_.Name -Like '*.zip')) }) #| ForEach-Object { $_ } $found = ($zip.Entries | Where-Object { (-not ($_.Name -Like '*.zip')) }) #$found = $zip.Entries Write-Verbose "found.count:$($found.Length)" if ($found) { #$results.AddRange($found) $results += $found } } else { foreach($query in $search) { Write-Verbose "query:$query" $queryResults = ($zip.Entries | Where-Object { $_.FullName -Like $query }) #| ForEach-Object { $_ } Write-Verbose "query.count:$($queryResults.Length)" #$results += $queryResults if ($queryResults-and $queryResults.Length -gt 0) { #$results.AddRange($queryResults) $results += $queryResults } #$results = $results.foreach({ $queryResults }) } } #Write-Verbose "found:$results" $zips = @() if ($PSBoundParameters.ContainsKey('recurse') -and $recurse.IsPresent -eq $true -and $recurse.ToBool() -eq $true) { #if ($file -ne 'package') { #SearchStax.zip causes issues - todo -red flag Write-Verbose 'Recurse passed - checking for zips' $zips = ($zip.Entries | Where-Object { $_.Name -Like '*.zip' }) #| ForEach-Object { $_.FullName } } Write-Verbose "zips:$zips" if ($zips) { $temp = [system.io.path]::GetTempPath() Write-Verbose "temp:$($temp)" $tempFolder = Join-Path (Join-Path $temp $PSScriptName) $file if (-not (Split-Path $tempFolder -Parent) -eq $file) { $tempFolder = Join-Path $tempFolder $file } Write-Verbose "tempFolder:$tempFolder" if (Test-Path $tempFolder) { Remove-Item $tempFolder -Recurse -Force } if (!(Test-Path $tempFolder)) { Write-Verbose "New-Item $tempFolder" New-Item -Path $tempFolder -ItemType Directory | Out-Null } $zips | ForEach-Object { #$zipstream = New-Object IO.MemoryStream($_, [IO.FileMode]::Open) $tempZipName = "$tempFolder/$($_.Name)" Write-Verbose '####################################################################################################' Write-Verbose "ExtractToFile($_,$tempZipName)" if (Test-Path $tempZipName) { Remove-Item $tempZipName -force | Out-Null } [System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $tempZipName) #$zipstream = New-Object IO.FileStream($_, [IO.FileMode]::Open) Write-Verbose '####################################################################################################' $zipstream = New-Object IO.FileStream($tempZipName, [IO.FileMode]::Open) $zipzip = New-Object IO.Compression.ZipArchive($zipstream) if (!$search) { $search = @('*.*')} foreach($query in $search) { Write-Verbose "query:$query" $queryResults = ($zipzip.Entries | Where-Object { $_.FullName -Like $query }) #| ForEach-Object { $_ } Write-Verbose "query.count:$($queryResults.Length)" #$results += $queryResults if ($queryResults -and $queryResults.Length -gt 0) { #$results.AddRange($queryResults) $zipresults += $queryResults } #$results = $results.foreach({ $queryResults }) } if ($zipstream) { Write-Verbose "$($PSScriptName):Closing zipstream" Write-Verbose '####################################################################################################' $zipstream.Close() $zipstream.Dispose() } #$tempZipPath = "$tempFolder\$($_.FullName)" #if (Test-Path $tempZipPath) { Remove-Item $tempZipPath -Recurse -Force } #if (!(Test-Path $tempZipPath)) { # Write-Verbose "Expanding $path" # Expand-Archive -Force -Path $path -DestinationPath $tempFolder # } #$zipResults = .\Get-ArchiveEntries "$tempFolder\$($_.FullName)" $search #$zipstream = New-Object IO.FileStream($path, [IO.FileMode]::Open) Write-Verbose "zipResults:$zipResults" if ($zipResults) { #$results.AddRange($zipResults) $results += $zipresults } } } if ($zip) { $zip.Dispose() } if ($stream) { Write-Verbose "$($PSScriptName):Closing stream" Write-Verbose '####################################################################################################' $stream.Close() $stream.Dispose() } Write-Verbose "$($PSScriptName):$($path):end" Write-Verbose '####################################################################################################' } } } catch { Write-Error "ERROR $PSScriptName $($path) $($search):$_" } Write-Verbose "results:$results" Write-Verbose "results.count:$($results.Length)" return $results |