MSBuild zip目录任务没有任何外部依赖(没有MSBuildCommunityTasks)

时间:2014-08-12 11:02:57

标签: msbuild zip msbuildcommunitytasks

我试图找到一种在不使用MSBuildCommunityTasks的情况下使用MSBuild压缩构建的方法。我确实设法在线找到一些代码,但它似乎占用了所有文件,甚至是目录中的文件,并将其放在一个文件中(没有目录)。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<UsingTask TaskName="Zip" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
  <InputFileNames ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
  <OutputFileName ParameterType="System.String" Required="true" />
  <OverwriteExistingFile ParameterType="System.Boolean" Required="false" />
</ParameterGroup>
<Task>
  <Reference Include="System.IO.Compression" />
  <Using Namespace="System.IO.Compression" />
  <Code Type="Fragment" Language="cs">
  <![CDATA[ 
    const int BufferSize = 64 * 1024;

    var buffer = new byte[BufferSize];
    var fileMode = OverwriteExistingFile ? FileMode.Create : FileMode.CreateNew;

    using (var outputFileStream = new FileStream(OutputFileName, fileMode))
    {
      using (var archive = new ZipArchive(outputFileStream, ZipArchiveMode.Create))
      {
        foreach (var inputFileName in InputFileNames.Select(f => f.ItemSpec))
        {
          var archiveEntry = archive.CreateEntry(Path.GetFileName(inputFileName));

          using (var fs = new FileStream(inputFileName, FileMode.Open))
          {
            using (var zipStream = archiveEntry.Open())
            {
              int bytesRead = -1;
              while ((bytesRead = fs.Read(buffer, 0, BufferSize)) > 0)
              {
                zipStream.Write(buffer, 0, bytesRead);
              }
            }
          }
        }
      }
    }        
  ]]>
  </Code>
</Task>
</UsingTask>
</Project>

如何让这段代码压缩我的文件夹并保持目录不变?

2 个答案:

答案 0 :(得分:6)

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <UsingTask TaskName="Zip" TaskFactory="CodeTaskFactory" AssemblyName="Microsoft.Build.Tasks.v12.0">
    <ParameterGroup>
      <Directory ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.IO.Compression.FileSystem" />
      <Code>System.IO.Compression.ZipFile.CreateFromDirectory(Directory, Directory + ".zip");</Code>
    </Task>
  </UsingTask>

  <Target Name="Foo">
    <Zip Directory="C:\Users\Ilya.Kozhevnikov\Dropbox\Foo" />
  </Target>
</Project>

答案 1 :(得分:0)

您可以使用归档中的文件和目录目录执行许多操作。作为一个简化的假设,让我们选择一个基本目录,并要求所有文件都在它下面。在存档中,条目将是文件路径的部分,但不包括基本目录名称。

所以,添加一个参数:

<BaseDirectory ParameterType="System.String" Required="true" />

更改条目的存档路径:

var archiveEntry = archive.CreateEntry(
    new Uri(Path.GetFullPath(BaseDirectory) + path.DirectorySeparatorChar)
        .MakeRelativeUri(new Uri(Path.GetFullPath(inputFileName))).ToString());

并且,作为奖励,修复过于严格的文件访问共享:

using (var fs = new FileStream(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read))