diff --git a/src/Cake.Common/IO/ZipAliases.cs b/src/Cake.Common/IO/ZipAliases.cs index 42188b9a4c..cb91063e62 100644 --- a/src/Cake.Common/IO/ZipAliases.cs +++ b/src/Cake.Common/IO/ZipAliases.cs @@ -135,14 +135,27 @@ public static void Zip(this ICakeContext context, DirectoryPath rootPath, FilePa /// [CakeMethodAlias] public static void Unzip(this ICakeContext context, FilePath zipFile, DirectoryPath outputPath) + => context.Unzip(zipFile, outputPath, false); + + /// + /// Unzips the specified file. + /// + /// The context. + /// Zip file to unzip. + /// Output path to unzip into. + /// Flag for if files should be overwritten in output. + /// + /// + /// Unzip("Cake.zip", "./cake", true); + /// + /// + [CakeMethodAlias] + public static void Unzip(this ICakeContext context, FilePath zipFile, DirectoryPath outputPath, bool overwriteFiles) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var zipper = new Zipper(context.FileSystem, context.Environment, context.Log); - zipper.Unzip(zipFile, outputPath); + zipper.Unzip(zipFile, outputPath, overwriteFiles); } } } \ No newline at end of file diff --git a/src/Cake.Common/IO/Zipper.cs b/src/Cake.Common/IO/Zipper.cs index 0b0df02353..252b7fd54b 100644 --- a/src/Cake.Common/IO/Zipper.cs +++ b/src/Cake.Common/IO/Zipper.cs @@ -204,22 +204,25 @@ public void Zip(DirectoryPath rootPath, FilePath outputPath, IEnumerableZip file path. /// Output directory path. public void Unzip(FilePath zipPath, DirectoryPath outputPath) + => Unzip(zipPath, outputPath, false); + + /// + /// Unzips the specified file to the specified output path. + /// + /// Zip file path. + /// Output directory path. + /// Flag for if files should be overwritten in output. + public void Unzip(FilePath zipPath, DirectoryPath outputPath, bool overwriteFiles) { - if (zipPath == null) - { - throw new ArgumentNullException(nameof(zipPath)); - } - if (outputPath == null) - { - throw new ArgumentNullException(nameof(outputPath)); - } + ArgumentNullException.ThrowIfNull(zipPath); + ArgumentNullException.ThrowIfNull(outputPath); // Make root path and output file path absolute. zipPath = zipPath.MakeAbsolute(_environment); outputPath = outputPath.MakeAbsolute(_environment); - _log.Verbose("Unzipping file {0} to {1}", zipPath.FullPath, outputPath.FullPath); - ZipFile.ExtractToDirectory(zipPath.FullPath, outputPath.FullPath); + _log.Verbose("Unzipping file {0} to {1} (overwrite files: {2})", zipPath.FullPath, outputPath.FullPath, overwriteFiles); + ZipFile.ExtractToDirectory(zipPath.FullPath, outputPath.FullPath, overwriteFiles); } private string GetRelativePath(DirectoryPath root, Path path)