Remove-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
#Set-StrictMode -Version Latest
#####################################################
# Remove-ArchiveEntries
#####################################################
<#PSScriptInfo
 
.VERSION 0.1
.GUID c87a9681-72b8-4100-90e4-1bc2834ee7e1
 
.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 remove
 
.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 search/remove entries/files in Zip package
 
.PARAMETER name
Path of package
 
#>
 
#####################################################
# Remove-ArchiveEntries
#####################################################
Param(
    [Parameter(Mandatory=$true)]
    [string] $path,
    [Parameter(Mandatory=$true)]
    [string[]] $search
)
function Remove-ArchiveEntries
{
    Param(
        [Parameter(Mandatory=$true)]
        [string] $path,    
        [Parameter(Mandatory=$true)]
        [string[]] $search
    )
    $ProgressPreference = "SilentlyContinue"
    $results = @()
    Write-Verbose "Remove-ArchiveEntries $path $search"
    try {
        if (($path.IndexOf("/") -eq -1) -or (-not (Test-Path $path)) ) {
            if (Test-Path (Join-Path (Get-Location) $path)) {
                $path = Join-Path (Get-Location) $path
                Write-Verbose "path:$path"
            }
            if (!(Test-Path $path)) {
                throw "ERROR Remove-ArchiveEntries - file not found: $path"
            }
        }
        
        $file = (Split-Path $path -leaf).Replace('.zip', '')
        Write-Verbose "file:$file"
        $tempPackagePath = Join-Path $ENV:TEMP $file
        Write-Verbose "tempPackagePath:$tempPackagePath"
        if (Test-Path $tempPackagePath) { Remove-Item $tempPackagePath -Recurse -Force }
        if (!(Test-Path $tempPackagePath)) { New-Item $tempPackagePath -ItemType Directory | Out-Null }

        Add-Type -AssemblyName System.IO.Compression
        $stream = New-Object IO.FileStream($path, [IO.FileMode]::Open)
        $zip = New-Object IO.Compression.ZipArchive($stream, [IO.Compression.ZipArchiveMode]::Update)
        foreach($query in $search) {
            Write-Verbose "query:$query"
            $queryResults = @()
            ($zip.Entries | Where-Object { $_.FullName -Like $query }) | ForEach-Object {
                Write-Host "Found:$($_.FullName)"
                $_.Delete()
                $queryResults += $_                
            }
            Write-Verbose "query.count:$($queryResults.Length)"
            $results += $queryResults
        }
        Write-Verbose "files:$results"

        $zips = @()
        if ($file -ne 'package') { #SearchStax.zip causes issues
            ($zip.Entries | Where-Object { $_.Name -Like '*.zip' }) | ForEach-Object { 
                Write-Host "Found:$($_.FullName)"
                [IO.Compression.ZipFileExtensions]::ExtractToFile($_,"$tempPackagePath\$_",$Overwrite)
                $zips += $_
            }
        }

        if ($zip) {    $zip.Dispose() }
        if ($stream) {
            $stream.Close()
            $stream.Dispose()
        }
    
        Write-Verbose "zips:$zips"

        if ($zips) {
            $tempFolder = $tempPackagePath
            ($zips | Where-Object { $_.Name -Like '*.zip' }) | ForEach-Object {
                $tempZipPath = "$tempFolder\$($_.FullName)"
                $resultsNested = Remove-ArchiveEntries $tempZipPath $search
            
                if ($resultsNested.count -gt 0) { 
                    Write-Verbose "Changes made to $tempZipPath. Updating $path"
                     $compress = @{
                         Path = "$tempZipPath"
                         DestinationPath = $path }
            
                     #Compress-Archive -Path $destination\temp\metadata\* -Update -DestinationPath $path -Force
                     Compress-Archive -Update @compress
                     Write-Verbose "$path updated."
                }

                $results += $resultsNested
            }
        }
        if (Test-Path $tempPackagePath) { Remove-Item $tempPackagePath -Recurse -Force }
    }
    catch {
        Write-Error "ERROR Remove-ArchiveEntries $($path) $($search):$_"

        if ($zip) {    $zip.Dispose() }
        if ($stream) {
            $stream.Close()
            $stream.Dispose()
        }
    }
    Write-Verbose "results:$results"
    Write-Verbose "results.count:$($results.Length)"
    return $results
}
#cls
Remove-ArchiveEntries $path $search