2

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

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

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

请指教。谢谢。

4

2 回答 2

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

只需定义一些targetroot或组成另一个targetpath……</p>

于 2013-01-07T23:24:30.343 回答
1

如果 .NET 4.5 不是一个选项,请使用 7zip: http ://sevenzip.sourceforge.jp/chm/cmdline/commands/extract.htm

于 2013-01-08T13:32:48.597 回答