Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH4260: Unzip alias should support overwrite files #4261

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading