【发布时间】:2017-09-28 16:22:28
【问题描述】:
说foo.zip 包含:
a
b
c
|- c1.exe
|- c2.dll
|- c3.dll
a, b, c 是文件夹。
如果我
Expand-Archive .\foo.zip -DestinationPath foo
foo.zip 中的所有文件/文件夹都已提取。
我只想提取c 文件夹。
【问题讨论】:
标签: powershell zip archive unzip
说foo.zip 包含:
a
b
c
|- c1.exe
|- c2.dll
|- c3.dll
a, b, c 是文件夹。
如果我
Expand-Archive .\foo.zip -DestinationPath foo
foo.zip 中的所有文件/文件夹都已提取。
我只想提取c 文件夹。
【问题讨论】:
标签: powershell zip archive unzip
试试这个
Add-Type -Assembly System.IO.Compression.FileSystem
#extract list entries for dir myzipdir/c/ into myzipdir.zip
$zip = [IO.Compression.ZipFile]::OpenRead("c:\temp\myzipdir.zip")
$entries=$zip.Entries | where {$_.FullName -like 'myzipdir/c/*' -and $_.FullName -ne 'myzipdir/c/'}
#create dir for result of extraction
New-Item -ItemType Directory -Path "c:\temp\c" -Force
#extraction
$entries | foreach {[IO.Compression.ZipFileExtensions]::ExtractToFile( $_, "c:\temp\c\" + $_.Name) }
#free object
$zip.Dispose()
【讨论】:
$zip.Entries | Where-Object { $_ -match 'myzipdir/c/+.' }
这个不使用外部库:
$shell= New-Object -Com Shell.Application
$shell.NameSpace("$(resolve-path foo.zip)").Items() | where Name -eq "c" | ? {
$shell.NameSpace("$PWD").copyhere($_) }
也许可以简化一点。
【讨论】:
这里有一些对我有用的东西。当然,您需要编辑代码以符合您的目标
$results =@()
foreach ($p in $Path)
{
$shell = new-object -Comobject shell.application
$fileName = $p
$zip = $shell.namespace("$filename")
$Results += $zip.items()| where-object { $_.Name -like "*C*" -or $_.Name -like
"*b*" }
}
foreach($item in $Results )
{
$shell.namespace($dest).copyhere($item)
}
【讨论】: