需要Powershell脚本来仅提取包含Style.CSS的特定文件夹的ZIP提取?

Ale*_*x S 2 powershell zip extract unzip extraction

需要Powershell脚本来仅提取包含Style.CSS的特定文件夹的ZIP提取?

我有很多ZIP文件,包含文件夹和文件的混合.我需要一个脚本来仅从ZIP文件中过滤和提取包含Style.CSS的整个FOLDER.

有一个类似的使用DotNetZip,但不完全检测包含Style.css的未知文件夹名称,然后只提取该文件夹.

请指教.谢谢.

mou*_*sio 5

以下是使用.Net Framework 4.5的ZipFileZipFileExtensions类开始的一种方法:

Add-Type -Assembly System.IO.Compression.FileSystem

foreach($sourcefile in (Get-ChildItem -filter "*.zip"))
{
    Write-Host "Processing $sourcefile"
    $entries = [IO.Compression.ZipFile]::OpenRead($sourcefile.FullName).Entries

    #first pass to collect the paths of the folders with a Style.CSS file 
    $folders = $entries |
        ?{ $_.Name -eq 'Style.CSS' } |
        %{ split-path $_.FullName } | unique

    #second pass to extract just the files in those folders
    $entries |
        ?{ (split-path $_.FullName) -in @($folders) -and $_.Name } |
        %{
            #compose some target path
            $targetpath = join-path "$targetroot" (join-path $sourcefile.Name $_.FullName)
            #create its directory
            md (split-path $targetfilename) >$null 2>&1
            #extract the file (and overwrite)
            [IO.Compression.ZipFileExtensions]::ExtractToFile($_, $targetfilename, $true)
        }
}
Run Code Online (Sandbox Code Playgroud)

只需定义一些targetroot或组成另一个targetpath......