Skip to content

Instantly share code, notes, and snippets.

@chaliy
Created August 6, 2012 09:08
Show Gist options
  • Save chaliy/3272433 to your computer and use it in GitHub Desktop.
Save chaliy/3272433 to your computer and use it in GitHub Desktop.
Code task to ZIP something
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0" DefaultTargets="Sample" >
<Import Project="Zip.targets" />
<Target Name="Sample" >
<Zip SourceFolder="C:\Users\Administrator.WIN-BOAS4C4GH8K\Projects\Temp" OutputFileName="package.zip" />
</Target>
</Project>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Simple ZIP task that utilize .NET 4.5 Zip Compression -->
<!--
Example
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0" DefaultTargets="Sample" >
<Import Project="Zip.targets" />
<Target Name="Sample" >
<Zip SourceFolder="C:\SomeFolder\" OutputFileName="output.zip" />
</Target>
</Project>
you can run this project with msbuild
-->
<UsingTask TaskName="Zip" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<SourceFolder ParameterType="System.String" Required="true"/>
<OutputFileName ParameterType="System.String" Required="true" />
<NoBackup ParameterType="System.Boolean" Required="false" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Net" />
<Using Namespace="System.Linq" />
<Using Namespace="System.Reflection" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Using Namespace="System.IO.Compression" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
SourceFolder = Path.GetFullPath(SourceFolder);
OutputFileName = Path.GetFullPath(OutputFileName);
Log.LogMessage("Package zip... (" + OutputFileName + " )");
// Prepare output temp file
var tmpFile = Path.ChangeExtension(OutputFileName, ".zip.tmp");
File.Delete(tmpFile);
// Zip folder
ZipFile.CreateFromDirectory(SourceFolder, tmpFile);
// Replace output file
File.Delete(OutputFileName);
File.Move(tmpFile, OutputFileName);
return true;
}
catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>
@umar26
Copy link

umar26 commented May 6, 2019

thank for the post this help me a lot .. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment