Reputation: 13
I have attempted to find a way to zip a build up using MSBuild without using MSBuildCommunityTasks. I did manage to find some code online but it seems to take all the files, even ones in directories and put it in one file (no directories).
<?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>
How can i get this code to zip up my folder and keep the directories intact?
Upvotes: 1
Views: 1276
Reputation: 10432
<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>
Upvotes: 6
Reputation: 20812
There are many things you could do with directories of files and directories in the archive. As a simplifying assumption, let's pick a base directory and require that all files are below it. In the archive, the entries will be the parts of the file paths up to but not including the base directory name.
So, add a parameter:
<BaseDirectory ParameterType="System.String" Required="true" />
Change the archive path for the entry:
var archiveEntry = archive.CreateEntry(
new Uri(Path.GetFullPath(BaseDirectory) + path.DirectorySeparatorChar)
.MakeRelativeUri(new Uri(Path.GetFullPath(inputFileName))).ToString());
And, as a bonus, fix the too-strict file access sharing:
using (var fs = new FileStream(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
Upvotes: 0