Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I zip a folder in MSBuild?

Tags:

msbuild

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

like image 523
Jonathan Allen Avatar asked Apr 10 '13 05:04

Jonathan Allen


2 Answers

"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

like image 112
granadaCoder Avatar answered Sep 26 '22 16:09

granadaCoder


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.

like image 34
Kellen Avatar answered Sep 23 '22 16:09

Kellen