压缩MSBuild中的所有文件夹

时间:2016-08-17 07:24:42

标签: .net msbuild msbuildcommunitytasks

我可以压缩一个明确的文件夹列表,如下所示:

<ItemGroup>
  <Folder Include="Foo\**\*.*"><Name>Foo</Name></Folder>
  <Folder Include="Bar\**\*.*"><Name>Bar</Name></Folder>
</ItemGroup>
<Zip
  ZipFileName="%(Folder.Name).zip"
  WorkingDirectory="%(Folder.Name)\"
  Files="@(Folder)" />

这会创建两个zip文件Foo.zipBar.zip,每个文件夹Foo\Bar\一个。

如何为所有现有文件夹执行此操作? (不显式列出每个文件夹。当创建新文件夹New\时,应自动创建zip文件New.zip而不更改MSBuild文件。)

(我正在使用MSBuild.Community.Tasks中的Zip任务)

1 个答案:

答案 0 :(得分:1)

我没有使用MSBuild.Community.Tasks但是使用了MSBuild Extension Pack 4.0.12.0我认为这会得到你想要的...将路径$(MSBuildProjectDirectory)改为你需要的地方拉链。

      <?xml version="1.0" encoding="utf-8" ?>        
        <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
             <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks" />

            <PropertyGroup>
                <BuildDependsOn>
                  $(BuildDependsOn);
                  ZipFiles;
                </BuildDependsOn>
              </PropertyGroup>

              <Target Name="ZipFiles">

                <MSBuild.ExtensionPack.FileSystem.FindUnder
                   TaskAction="FindDirectories"
                   Path="$(MSBuildProjectDirectory)\..\"
                   Recursive="false">
                  <Output ItemName="AllFoundDirectories" TaskParameter="FoundItems"/>
                </MSBuild.ExtensionPack.FileSystem.FindUnder>

                <Message Text="===== Found Directories =====" Importance="high"/>        
                <Message Text="AllFoundDirectories:%0d%0a@(AllFoundDirectories,'%0d%0a')"/>

                <MSBuild.ExtensionPack.Compression.Zip
                  TaskAction="Create"
                  CompressPath="%(AllFoundDirectories.FullPath)"
                  ZipFileName="%(AllFoundDirectories.FullPath)\%(AllFoundDirectories.Filename).zip" />

              </Target>

            </Project>