Guest User

NativeZip

a guest
Feb 3rd, 2015
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <UsingTask TaskName="NativeZip" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  2. <ParameterGroup>
  3. <InputFileNames ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
  4. <OutputFileName ParameterType="System.String" Required="true" />
  5. <OverwriteExistingFile ParameterType="System.Boolean" Required="false" />
  6. <Flatten ParameterType="System.Boolean" Required="false" />
  7. <RemoveRoot ParameterType="System.String" Required="false" />
  8. </ParameterGroup>
  9. <Task>
  10. <Reference Include="System.IO.Compression" />
  11. <Using Namespace="System.IO.Compression" />
  12. <Code Type="Fragment" Language="cs">
  13. <![CDATA[
  14. var fileMode = OverwriteExistingFile ? FileMode.Create : FileMode.CreateNew;
  15. Directory.CreateDirectory(Path.GetDirectoryName(OutputFileName));
  16. using (var archive = new ZipArchive(new FileStream(OutputFileName, fileMode), ZipArchiveMode.Create))
  17. {
  18. foreach (var inputFileName in InputFileNames.Select(f => f.ItemSpec))
  19. {
  20. var archiveName = Flatten ? Path.GetFileName(inputFileName) : (String.IsNullOrEmpty(RemoveRoot) ? inputFileName : inputFileName.Replace(RemoveRoot, ""));
  21. var archiveEntry = archive.CreateEntry(archiveName);
  22. using (var fs = new FileStream(inputFileName, FileMode.Open))
  23. {
  24. using (var zipStream = archiveEntry.Open())
  25. {
  26. fs.CopyTo(zipStream);
  27. }
  28. }
  29. }
  30. }
  31. ]]>
  32. </Code>
  33. </Task>
  34. </UsingTask>
RAW Paste Data