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

时间:2013-01-06 23:46:15

标签: powershell zip extract unzip extraction

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

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

使用DotNetZip有similar one,但不能完全检测包含Style.css的未知文件夹名称,然后只提取该文件夹。

请指教。谢谢。

2 个答案:

答案 0 :(得分:5)

使用.Net Framework 4.5的ZipFile and ZipFileExtensions类,这是一种开始使用的方法:

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 ...

答案 1 :(得分:1)

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