Skip to content

Commit eb0f19d

Browse files
devleadgep13
authored andcommitted
(GH-4260) Unzip alias should support overwrite files
* fixes #4260
1 parent 1839d1a commit eb0f19d

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/Cake.Common/IO/ZipAliases.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,27 @@ public static void Zip(this ICakeContext context, DirectoryPath rootPath, FilePa
135135
/// </example>
136136
[CakeMethodAlias]
137137
public static void Unzip(this ICakeContext context, FilePath zipFile, DirectoryPath outputPath)
138+
=> context.Unzip(zipFile, outputPath, false);
139+
140+
/// <summary>
141+
/// Unzips the specified file.
142+
/// </summary>
143+
/// <param name="context">The context.</param>
144+
/// <param name="zipFile">Zip file to unzip.</param>
145+
/// <param name="outputPath">Output path to unzip into.</param>
146+
/// <param name="overwriteFiles">Flag for if files should be overwritten in output.</param>
147+
/// <example>
148+
/// <code>
149+
/// Unzip("Cake.zip", "./cake", true);
150+
/// </code>
151+
/// </example>
152+
[CakeMethodAlias]
153+
public static void Unzip(this ICakeContext context, FilePath zipFile, DirectoryPath outputPath, bool overwriteFiles)
138154
{
139-
if (context == null)
140-
{
141-
throw new ArgumentNullException(nameof(context));
142-
}
155+
ArgumentNullException.ThrowIfNull(context);
143156

144157
var zipper = new Zipper(context.FileSystem, context.Environment, context.Log);
145-
zipper.Unzip(zipFile, outputPath);
158+
zipper.Unzip(zipFile, outputPath, overwriteFiles);
146159
}
147160
}
148161
}

src/Cake.Common/IO/Zipper.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,25 @@ public void Zip(DirectoryPath rootPath, FilePath outputPath, IEnumerable<FilePat
204204
/// <param name="zipPath">Zip file path.</param>
205205
/// <param name="outputPath">Output directory path.</param>
206206
public void Unzip(FilePath zipPath, DirectoryPath outputPath)
207+
=> Unzip(zipPath, outputPath, false);
208+
209+
/// <summary>
210+
/// Unzips the specified file to the specified output path.
211+
/// </summary>
212+
/// <param name="zipPath">Zip file path.</param>
213+
/// <param name="outputPath">Output directory path.</param>
214+
/// <param name="overwriteFiles">Flag for if files should be overwritten in output.</param>
215+
public void Unzip(FilePath zipPath, DirectoryPath outputPath, bool overwriteFiles)
207216
{
208-
if (zipPath == null)
209-
{
210-
throw new ArgumentNullException(nameof(zipPath));
211-
}
212-
if (outputPath == null)
213-
{
214-
throw new ArgumentNullException(nameof(outputPath));
215-
}
217+
ArgumentNullException.ThrowIfNull(zipPath);
218+
ArgumentNullException.ThrowIfNull(outputPath);
216219

217220
// Make root path and output file path absolute.
218221
zipPath = zipPath.MakeAbsolute(_environment);
219222
outputPath = outputPath.MakeAbsolute(_environment);
220223

221-
_log.Verbose("Unzipping file {0} to {1}", zipPath.FullPath, outputPath.FullPath);
222-
ZipFile.ExtractToDirectory(zipPath.FullPath, outputPath.FullPath);
224+
_log.Verbose("Unzipping file {0} to {1} (overwrite files: {2})", zipPath.FullPath, outputPath.FullPath, overwriteFiles);
225+
ZipFile.ExtractToDirectory(zipPath.FullPath, outputPath.FullPath, overwriteFiles);
223226
}
224227

225228
private string GetRelativePath(DirectoryPath root, Path path)

0 commit comments

Comments
 (0)