简体   繁体   English

PowerShell:从压缩存档中提取特定文件/文件夹

[英]PowerShell: Extract specific files/folders from a zipped archive

Say foo.zip contains: foo.zip包含:

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

where a, b, c are folders. 其中a, b, c是文件夹。

If I 如果我

Expand-Archive .\foo.zip -DestinationPath foo 

all files/folders in foo.zip are extracted. 提取foo.zip中的所有文件/文件夹。
I would like to extract only the c folder. 我想只提取c文件夹。

try this 试试这个

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

This one does not use external libraries: 这个不使用外部库:

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

Perhaps it can be simplified a bit. 也许它可以简化一点。

Here is something that works for me. 这是适合我的东西。 Of Course, you will need to edit the code to fit your objective 当然,您需要编辑代码以适合您的目标

$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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM