Created
August 6, 2012 09:08
-
-
Save chaliy/3272433 to your computer and use it in GitHub Desktop.
Code task to ZIP something
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank for the post this help me a lot .. :)