How To Automatically Zip ASP.NET File System Publish Output

If you use the File System publish method to publish an ASP.NET web application, you will get the output in a folder you choose. But what if you want the output in a zip compressed folder instead? I got tired of manually zipping the output folder, so I added a step to the end of the publish process to automatically compress the output folder into a zip file.

I did this by adding a custom target to my project’s pubxml file. The custom target executes the compress-archive powershell command (see highlighted section below):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <publishUrl>C:\Publish\SimpleExample</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>    
  </PropertyGroup>
  <Target Name="ZipPublishOutput" AfterTargets="GatherAllFilesToPublish">
    <Exec Command='powershell -nologo -noprofile -command "compress-archive -force -path $(WPPAllFilesInSingleFolder)\* -destinationpath $(publishUrl).zip"' />
  </Target>
</Project>

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *