2017-05-01 63 views
1

foo.zip包含提取特定的文件/文件夹:的PowerShell:从压缩文档

a 
b 
c 
|- c1.exe 
|- c2.dll 
|- c3.dll 

其中a, b, c是文件夹。

如果我

Expand-Archive .\foo.zip -DestinationPath foo 

所有文件/文件在foo.zip文件夹提取。
我想只提取c文件夹。

回答

2

试试这个

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() 
0

这一个不使用外部库:

$shell= New-Object -Com Shell.Application 
$shell.NameSpace("$(resolve-path foo.zip)").Items() | where Name -eq "c" | ? { 
    $shell.NameSpace("$PWD").copyhere($_) } 

或许它可以简化一点。