Last active
December 27, 2015 04:09
-
-
Save sbezludny/7264822 to your computer and use it in GitHub Desktop.
Zip project output
This file contains hidden or 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" /> | |
| </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 + " )"); | |
| File.Delete(OutputFileName); | |
| // Prepare output temp file | |
| var tmpFile = Path.GetTempFileName(); | |
| File.Delete(tmpFile); | |
| // Zip folder | |
| ZipFile.CreateFromDirectory(SourceFolder, tmpFile); | |
| // Replace output file | |
| File.Move(tmpFile, OutputFileName); | |
| File.Delete(tmpFile); | |
| 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