Remove-FromArchive.psm1
<#---------------------------------------------------------------------------
Author: Detlef Schneider Desc: Module that allows removing soecific files from existing arcives Date: 2/11/2017 Site: https://blog.schneide-r.de --------------------------------------------------------------------------- #> <# MIT License ----------- Copyright (c) 2017 by DEtlef Schneider Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #> function Remove-FromArchive { <# .SYNOPSIS Removes files from archives .DESCRIPTION Specified file or files are removed from existing archive files. Multiple files to remove can be specified. Multiple archive files to process can be specified. .PARAMETER Path Specify the path to archive files. Accepts placeholders. .PARAMETER FileName Specify the file names to be removed. If file is not found in the archive, nothing gets removed. .EXAMPLE Remove-FromArchive -Path *.zip -FileName oldReleaseNotes.txt Removes the file oldReleaseNotes.txt from all zip files in current context -Path is specified with pattern, so all zip files are found .EXAMPLE Remove-FromArchive -Path test.zip -FileName ReleaseNotes.txt, readme.md Removes the files ReleaseNotes.txt and readme.md from the archive test.zip -Path specifies a single file -FileName is a comma separated list of several file names .EXAMPLE Remove-FromArchive -Path C:\tmp\test*.zip, C:\tmp\archive.zip -FileName readme.txt Removes a file from all archive files beginning with 'test' and from an archive with name archive.zip -Path is specified as a comma separated list, with a file name pattern and a specific file name -FileName is a single file name .EXAMPLE Get-ChildItem test?.zip | Remove-FromArchive -FileName readme.txt Removes a specific file from all archives that are piped from the Get-ChildItem command -Path is not specified, but the result of Get-ChildItem is piped to the command -FileName is secified as a single file name .NOTES Version history --------------- v1.0.0.0 - Initial release #> [CmdletBinding()] Param ( [Parameter( Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, HelpMessage="Enter one or more paths to archive files, separated by commas." )] [ValidateNotNullorEmpty()][Alias('p')][string[]]$Path, [Parameter( Position=1, ValueFromPipelineByPropertyName=$true, HelpMessage="Enter one of more file names to be deleted, separated by commas." )] [ValidateNotNullorEmpty()][Alias('f')][string[]]$FileName ) Begin { Add-Type -AssemblyName 'System.IO.Compression' } Process { Foreach ($entry in $Path) { $zips = Get-ChildItem -Path $entry Foreach ($zip in $zips) { If (Test-Path $zip) { try { $zipStream = New-Object IO.FileStream($zip, [IO.FileMode]::Open) $zipMode = [IO.Compression.ZipArchiveMode]::Update $zipContent = New-Object IO.Compression.ZipArchive($zipStream, $zipMode) Write-Verbose "Processing archive: $zip" } catch { Write-Error "Error accessing archive $($zip): $_" } try { # Remove the specified files Write-Verbose "`tRemoving files: $FileName" ($zipContent.Entries | Where-Object { $FileName -contains $_.Name }) | ForEach-Object { $_.Delete() } } catch { Write-Error "Error removing file(s) from archive $($zip): $_" } $zipContent.Dispose() $zipStream.Close() $zipStream.Dispose() } else { Write-Warning "Archive $zip not found." } } } } End { } } # SIG # Begin signature block # MIIFfwYJKoZIhvcNAQcCoIIFcDCCBWwCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU0uXNvtcjIrXmOB4YMoKeyBMd # FsegggMbMIIDFzCCAgOgAwIBAgIQ6hVf5UOybptKtjs12/oyVTAJBgUrDgM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