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
#Set-StrictMode -Version Latest
#####################################################
# Get-ArchiveEntries
#####################################################
<#PSScriptInfo
 
.VERSION 0.1
.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=$true)]
    [string] $path,
    [Parameter(Mandatory=$false)]
    [string[]] $search = @()
)
function Get-ArchiveEntries
{
    Param(
        [Parameter(Mandatory=$true)]
        [string] $path,    
        [Parameter(Mandatory=$false)]
        [string[]] $search = @()
    )
    $ProgressPreference = "SilentlyContinue"
    $results = @()
    Write-Verbose "Get-ArchiveEntries $path $search"
    try {
        if (!(Test-Path $path)) {
            throw "ERROR Get-ArchiveEntries - file not found: $path"
        }
        $file = (Split-Path $path -leaf).Replace('.zip', '')
        Add-Type -AssemblyName System.IO.Compression
        $stream = New-Object IO.FileStream($path, [IO.FileMode]::Open)
        $zip = New-Object IO.Compression.ZipArchive($stream)

        if (!$search) {
            $results += ($zip.Entries | Where-Object { (-not ($_.Name -Like '*.zip')) }) #| ForEach-Object { $_ }
        } else {
            foreach($query in $search) {
                Write-Verbose "query:$query"
                $queryResults = ($zip.Entries | Where-Object { ( (-not($_.Name -Like '*.zip')) -and ($_.FullName -Like $query)) }) #| ForEach-Object { $_ }
                Write-Verbose "query.count:$($queryResults.Length)"
                $results += $queryResults
            }
        }
        Write-Verbose "files:$results"

        $zips = @()
        if ($file -ne 'package') { #SearchStax.zip causes issues
            $zips = ($zip.Entries | Where-Object { $_.Name -Like '*.zip' }) | ForEach-Object { $_ }
        }
        if ($zip) {    $zip.Dispose() }
        if ($stream) {
            $stream.Close()
            $stream.Dispose()
        }

        Write-Verbose "zips:$zips"

        if ($zips) {
            $tempFolder = Join-Path $ENV:TEMP $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
            }
            ($zips | Where-Object { $_.Name -Like '*.zip' }) | ForEach-Object {
                $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
                }
                $results += Get-ArchiveEntries "$tempFolder\$($_.FullName)" $search
            }
        }
    }
    catch {
        Write-Error "ERROR Get-ArchiveEntries $($path) $($search):$_"
    }
    Write-Verbose "results:$results"
    Write-Verbose "results.count:$($results.Length)"
    return $results
}
Get-ArchiveEntries $path $search