How do I zip a folder in MSBuild?

喜你入骨 提交于 2019-12-20 01:39:43

问题


How do I zip an output folder in MSBuild? For the filename I need to use a variable that gets set elsewhere.


回答1:


"MSBuild.Community.Tasks.Zip" is one way. WorkingCheckout and OutputDirectory are not defined.

But you can get the drift below.

The below will get all files that are not .config files for my zip.

Note "Host" is my custom csproj folder name, yours will be different.

<ItemGroup>
    <ZipFilesHostNonConfigExcludeFiles Include="$(WorkingCheckout)\Host\bin\$(Configuration)\**\*.config" />
</ItemGroup>
<!-- -->
<ItemGroup>
    <ZipFilesHostNonConfigIncludeFiles Include="$(WorkingCheckout)\Host\bin\$(Configuration)\**\*.*" Exclude="@(ZipFilesHostNonConfigExcludeFiles)" />
</ItemGroup>
<MSBuild.Community.Tasks.Zip Files="@(ZipFilesHostNonConfigIncludeFiles)" ZipFileName="$(OutputDirectory)\MyZipFileNameHere_$(Configuration).zip" WorkingDirectory="$(WorkingCheckout)\Host\bin\$(Configuration)\" />
<!-- -->

Here is the other main-stream option:

http://msbuildextensionpack.codeplex.com/discussions/398966




回答2:


If you want to package all files and without preserving folder structure.

<ItemGroup>
  <ZipFiles Include="$(OutDir)\**\*.*" />
</ItemGroup>
<Target Name="AfterBuild" Condition="'$(Configuration)'=='Release'">
  <Zip ZipFileName="$(OutDir)\output.zip" WorkingDirectory="$(OutDir)" Files="@(ZipFiles)" Flatten="True" Quiet="true" />
</Target>

If you want to preserve folder structure

<Target Name="AfterBuild" Condition="'$(Configuration)'=='Release'">
  <Zip ZipFileName="$(OutDir)\output.zip" WorkingDirectory="$(OutDir)" Files="@(ZipFiles)" Flatten="False" Quiet="true" />
</Target>

Flatten="True" means that all directories will be removed and the files will be placed at the root of the zip file.

WorkingDirectory is the base of the zip file. All files will be made relative from the working directory.



来源:https://stackoverflow.com/questions/15917803/how-do-i-zip-a-folder-in-msbuild

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!