Jonathan Allen
Jonathan Allen

Reputation: 70287

How do I zip a folder in MSBuild?

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

Upvotes: 5

Views: 7401

Answers (2)

Kellen
Kellen

Reputation: 149

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.

Upvotes: 4

granadaCoder
granadaCoder

Reputation: 27842

"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

Upvotes: 7

Related Questions