2017-05-01 64 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($_) } 

或許它可以簡化一點。