Skip to content

Commit

Permalink
(GH-4260) Unzip alias should support overwrite files
Browse files Browse the repository at this point in the history
* fixes #4260
  • Loading branch information
devlead authored and gep13 committed Nov 18, 2023
1 parent 1839d1a commit eb0f19d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
23 changes: 18 additions & 5 deletions src/Cake.Common/IO/ZipAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,27 @@ public static void Zip(this ICakeContext context, DirectoryPath rootPath, FilePa
/// </example>
[CakeMethodAlias]
public static void Unzip(this ICakeContext context, FilePath zipFile, DirectoryPath outputPath)
=> context.Unzip(zipFile, outputPath, false);

/// <summary>
/// Unzips the specified file.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="zipFile">Zip file to unzip.</param>
/// <param name="outputPath">Output path to unzip into.</param>
/// <param name="overwriteFiles">Flag for if files should be overwritten in output.</param>
/// <example>
/// <code>
/// Unzip("Cake.zip", "./cake", true);
/// </code>
/// </example>
[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);
}
}
}
23 changes: 13 additions & 10 deletions src/Cake.Common/IO/Zipper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,25 @@ public void Zip(DirectoryPath rootPath, FilePath outputPath, IEnumerable<FilePat
/// <param name="zipPath">Zip file path.</param>
/// <param name="outputPath">Output directory path.</param>
public void Unzip(FilePath zipPath, DirectoryPath outputPath)
=> Unzip(zipPath, outputPath, false);

/// <summary>
/// Unzips the specified file to the specified output path.
/// </summary>
/// <param name="zipPath">Zip file path.</param>
/// <param name="outputPath">Output directory path.</param>
/// <param name="overwriteFiles">Flag for if files should be overwritten in output.</param>
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)
Expand Down

0 comments on commit eb0f19d

Please sign in to comment.