@@ -65,6 +65,17 @@ namespace MSBuild.Community.Tasks
65
65
/// ZipFileName="D:\svn\repo.zip" />
66
66
/// </Target>
67
67
/// ]]></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>
68
79
/// </example>
69
80
public class Zip : Task
70
81
{
@@ -139,11 +150,19 @@ public Zip()
139
150
/// </summary>
140
151
/// <value>The working directory.</value>
141
152
/// <remarks>
142
- /// The working directory is the base of the zip file.
143
153
/// All files will be made relative from the working directory.
144
154
/// </remarks>
145
155
public string WorkingDirectory { get ; set ; }
146
156
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
+
147
166
/// <summary>
148
167
/// Gets or sets the password.
149
168
/// </summary>
@@ -292,7 +311,7 @@ private bool ZipFiles()
292
311
// maybe a directory
293
312
if ( Directory . Exists ( name ) )
294
313
{
295
- var directoryEntry = zip . AddDirectory ( name , directoryPathInArchive ) ;
314
+ var directoryEntry = zip . AddDirectory ( name , GetArchivePath ( RootDirectory , directoryPathInArchive ) ) ;
296
315
if ( ! Quiet )
297
316
Log . LogMessage ( Resources . ZipAdded , directoryEntry . FileName ) ;
298
317
@@ -308,7 +327,7 @@ private bool ZipFiles()
308
327
&& Path . GetFileName ( directoryPathInArchive ) == Path . GetFileName ( name ) )
309
328
directoryPathInArchive = Path . GetDirectoryName ( directoryPathInArchive ) ;
310
329
311
- var entry = zip . AddFile ( name , directoryPathInArchive ) ;
330
+ var entry = zip . AddFile ( name , GetArchivePath ( RootDirectory , directoryPathInArchive ) ) ;
312
331
if ( ! Quiet )
313
332
Log . LogMessage ( Resources . ZipAdded , entry . FileName ) ;
314
333
}
@@ -326,6 +345,15 @@ private bool ZipFiles()
326
345
return true ;
327
346
}
328
347
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
+
329
357
private static string GetPath ( string originalPath , string rootDirectory )
330
358
{
331
359
var relativePath = new List < string > ( ) ;
0 commit comments