Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Last active December 15, 2015 17:59
Show Gist options
  • Save snipsnipsnip/5300138 to your computer and use it in GitHub Desktop.
Save snipsnipsnip/5300138 to your computer and use it in GitHub Desktop.
ziptask.vcxproj.xml
<Target Name="CreateZip" AfterTargets="Build" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<PropertyGroup>
<StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd-HH-mm-ss'))</StringDate>
</PropertyGroup>
<ItemGroup>
<ReleaseFiles Include="Release\*exe*">
<StripPath>true</StripPath>
</ReleaseFiles>
<ReleaseFiles Include="README.txt;data\**\*" Exclude="**\.svn\**\*"/>
<ReleaseFiles Include="*.vcxproj;*.sln;*.cpp;*.h;include\**\*" Exclude="**\.svn\**\*">
<PrependPath>src/</PrependPath>
</ReleaseFiles>
</ItemGroup>
<Message Text="Creating $(StringDate).zip" />
<MakeDir Directories="$(ProjectDir)zip"/>
<Zip Files="@(ReleaseFiles)" ZipFileName="$(ProjectDir)zip\$(StringDate).zip" />
</Target>
<!-- adopted from http://blog.3d-logic.com/2012/10/16/msbuild-zip-task-without-external-dependencies/ -->
<UsingTask TaskName="Zip" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<ZipFileName ParameterType="System.String" Required="true" />
<OverwriteExistingFile ParameterType="System.Boolean" Required="false" />
</ParameterGroup>
<Task>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.Filesystem" />
<Using Namespace="System.IO.Compression" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var fileMode = OverwriteExistingFile ? FileMode.Create : FileMode.CreateNew;
using (var outputFileStream = new FileStream(ZipFileName, fileMode))
{
using (var archive = new ZipArchive(outputFileStream, ZipArchiveMode.Create))
{
foreach (var inputItem in Files)
{
var inputFileName = inputItem.ItemSpec;
var archivePath = inputFileName;
if (inputItem.GetMetadata("StripPath") == "true")
{
archivePath = Path.GetFileName(archivePath);
}
archivePath = inputItem.GetMetadata("PrependPath") + archivePath;
ZipFileExtensions.CreateEntryFromFile(archive, inputFileName, archivePath);
}
}
}
]]>
</Code>
</Task>
</UsingTask>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment