@@ -65,6 +65,17 @@ namespace MSBuild.Community.Tasks
6565 /// ZipFileName="D:\svn\repo.zip" />
6666 /// </Target>
6767 /// ]]></code>
68+ /// Create a zip file using a root directory.
69+ /// <code><![CDATA[
70+ /// <ItemGroup>
71+ /// <RepoFiles Include="D:\svn\repo\**\*.*" />
72+ /// </ItemGroup>
73+ /// <Target Name="Zip">
74+ /// <Zip Files="@(RepoFiles)"
75+ /// RootDirectory="Repo"
76+ /// ZipFileName="D:\svn\repo.zip" />
77+ /// </Target>
78+ /// ]]></code>
6879 /// </example>
6980 public class Zip : Task
7081 {
@@ -139,11 +150,19 @@ public Zip()
139150 /// </summary>
140151 /// <value>The working directory.</value>
141152 /// <remarks>
142- /// The working directory is the base of the zip file.
143153 /// All files will be made relative from the working directory.
144154 /// </remarks>
145155 public string WorkingDirectory { get ; set ; }
146156
157+ /// <summary>
158+ /// Gets or sets the root directory for the archive.
159+ /// </summary>
160+ /// <value>The root directory.</value>
161+ /// <remarks>
162+ /// The root directory is the base of the archive.
163+ /// </remarks>
164+ public string RootDirectory { get ; set ; }
165+
147166 /// <summary>
148167 /// Gets or sets the password.
149168 /// </summary>
@@ -292,7 +311,7 @@ private bool ZipFiles()
292311 // maybe a directory
293312 if ( Directory . Exists ( name ) )
294313 {
295- var directoryEntry = zip . AddDirectory ( name , directoryPathInArchive ) ;
314+ var directoryEntry = zip . AddDirectory ( name , GetArchivePath ( RootDirectory , directoryPathInArchive ) ) ;
296315 if ( ! Quiet )
297316 Log . LogMessage ( Resources . ZipAdded , directoryEntry . FileName ) ;
298317
@@ -308,7 +327,7 @@ private bool ZipFiles()
308327 && Path . GetFileName ( directoryPathInArchive ) == Path . GetFileName ( name ) )
309328 directoryPathInArchive = Path . GetDirectoryName ( directoryPathInArchive ) ;
310329
311- var entry = zip . AddFile ( name , directoryPathInArchive ) ;
330+ var entry = zip . AddFile ( name , GetArchivePath ( RootDirectory , directoryPathInArchive ) ) ;
312331 if ( ! Quiet )
313332 Log . LogMessage ( Resources . ZipAdded , entry . FileName ) ;
314333 }
@@ -326,6 +345,15 @@ private bool ZipFiles()
326345 return true ;
327346 }
328347
348+ private static string GetArchivePath ( string rootDirectory , string directoryPathInArchive )
349+ {
350+ return String . IsNullOrEmpty ( rootDirectory )
351+ ? directoryPathInArchive
352+ : String . IsNullOrEmpty ( directoryPathInArchive )
353+ ? rootDirectory
354+ : Path . Combine ( rootDirectory , directoryPathInArchive ) ;
355+ }
356+
329357 private static string GetPath ( string originalPath , string rootDirectory )
330358 {
331359 var relativePath = new List < string > ( ) ;
0 commit comments