您只显示您的解压缩/从 zip 文件中提取的工作,而不是删除工作,这是您要询问的部分。
但您所要求的似乎与此讨论和答案重复。
Remove files from .zip file with Powershell
您也已经在调用 System.IO.Compression 命名空间,因此 System.IO.Compression.Filesystem 有一个更新和删除方法。
示例:(忽略/删除暂停 - 它就在那里,因此人们可以看到每个阶段的结果。)
# Zip file path
$zip = 'D:\Temp\MyFile.zip'
# Instantiate the .Net namespace
add-type -AssemblyName 'System.IO.Compression.filesystem'
# Remove a file from a zip archive
foreach ($z in $zip)
{
# Open the zip for updating
$tempz = [io.compression.zipfile]::Open($z,'Update')
"`nShow all files in the zip"
$tempz.Entries
Pause
"`nDelete a specific file"
($tempz.Entries | Where FullName -Match 'Test.clixml').Delete()
Pause
"`nValidate remove"
$tempz.Entries
Pause
# Clean up / close the zip
$tempz.Dispose()
}
Show all files in the zip
Archive : System.IO.Compression.ZipArchive
CompressedLength : 69
ExternalAttributes : 32
FullName : newfile.txt
LastWriteTime : 30-Sep-18 20:52:08 -07:00
Length : 116
Name : newfile.txt
Archive : System.IO.Compression.ZipArchive
CompressedLength : 41438
ExternalAttributes : 32
FullName : ps-gps.xml
LastWriteTime : 02-Oct-18 19:29:44 -07:00
Length : 767464
Name : ps-gps.xml
Archive : System.IO.Compression.ZipArchive
CompressedLength : 45
ExternalAttributes : 32
FullName : MyFile.txt
LastWriteTime : 30-Sep-18 23:31:08 -07:00
Length : 55
Name : MyFile.txt
Archive : System.IO.Compression.ZipArchive
CompressedLength : 132
ExternalAttributes : 32
FullName : Test.clixml
LastWriteTime : 02-Oct-18 17:26:00 -07:00
Length : 202
Name : Test.clixml
Press Enter to continue...:
Delete a specific file
Press Enter to continue...:
Validate remove
Archive : System.IO.Compression.ZipArchive
CompressedLength : 69
ExternalAttributes : 32
FullName : newfile.txt
LastWriteTime : 30-Sep-18 20:52:08 -07:00
Length : 116
Name : newfile.txt
Archive : System.IO.Compression.ZipArchive
CompressedLength : 41438
ExternalAttributes : 32
FullName : ps-gps.xml
LastWriteTime : 02-Oct-18 19:29:44 -07:00
Length : 767464
Name : ps-gps.xml
Archive : System.IO.Compression.ZipArchive
CompressedLength : 45
ExternalAttributes : 32
FullName : MyFile.txt
LastWriteTime : 30-Sep-18 23:31:08 -07:00
Length : 55
Name : MyFile.txt
Press Enter to continue...:
根据 OP 请求更新/多个文件和代码合并
您可能需要处理的 zip 文件的数量真的不是问题。
尽可能多地通过。
您没有说的是,您是如何获得这些 zip 文件名的。意义通过
Get-ChildItem,或从某个文本文件中,使用相同的 cmdlet 搜索它们。
不管怎样,过程都是一样的。
如果我直接修改您的功能,并在此过程中修复一些问题。
试试这个...
function Expand-ZipFilesWithCleanUp
{
[cmdletbinding()]
[Alias('ezc')]
Param
(
[string[]]$ZipFiles,
[string]$Outdir,
[string] $FilenameToRemove
)
# Instantiate the .Net namespace
add-type -AssemblyName 'System.IO.Compression.filesystem'
"The number of zip files passed in was $($ZipFiles.Count)"
# Remove unwanted files
foreach ($ZipFile in $ZipFiles)
{
# Open the zip for updating
$ProcessZipFile = [io.compression.zipfile]::Open($ZipFile,'Update')
"`nShow all files in the zip"
$ProcessZipFile.Entries | Out-GridView -PassThru
"`nDeleting unwanted file $FilenameToRemove from $ZipFile"
($ProcessZipFile.Entries | Where FullName -Match $FilenameToRemove).Delete()
"`nValidate remove"
$ProcessZipFile.Entries | Out-GridView -PassThru
# Clean up / close the zip
$ProcessZipFile.Dispose()
}
#//Begin unzip code
#//End unzip code
}
Expand-ZipFilesWithCleanUp -ZipFiles (Get-ChildItem -Path 'D:\Temp' -Filter '*.zip').FullName -FilenameToRemove 'Test.clixml'
再一次,那些..
$ProcessZipFile.Entries | Out-GridView -PassThru
... 行不是必需的,它们只是一种弹出对话框的方式,以显示在删除或给定文件之前和之后 zip 中发生的情况。
您现在要做的就是在提供的空白处添加您的其他代码。