Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/Cake.Compression/Classes/Bzip2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public BZip2(
/// <param name="outputPath">The output path.</param>
/// <param name="filePaths">The file paths.</param>
/// <param name="level">The compression level (1-9).</param>
/// <param name="useTar">Whether to wrap files in a TAR archive before compressing.</param>
public override void Compress(
DirectoryPath rootPath,
FilePath outputPath,
IEnumerable<FilePath> filePaths,
int level)
int level,
bool useTar = true)
{
ArgumentNullException.ThrowIfNull(rootPath, nameof(rootPath));
ArgumentNullException.ThrowIfNull(outputPath, nameof(outputPath));
Expand Down
4 changes: 3 additions & 1 deletion src/Cake.Compression/Classes/CompressionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ protected CompressionBase(
/// <param name="outputPath">The output path.</param>
/// <param name="filePaths">The file paths.</param>
/// <param name="level">The compression level (1-9).</param>
/// <param name="useTar">Whether to wrap files in a TAR archive before compressing.</param>
public abstract void Compress(
DirectoryPath rootPath,
FilePath outputPath,
IEnumerable<FilePath> filePaths,
int level);
int level,
bool useTar = true);

/// <summary>
/// Decompress the specified archive.
Expand Down
65 changes: 44 additions & 21 deletions src/Cake.Compression/Classes/GZip.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using System.Linq;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using System.Text;
Expand Down Expand Up @@ -30,11 +31,13 @@ public GZip(
/// <param name="outputPath">The output path.</param>
/// <param name="filePaths">The file paths.</param>
/// <param name="level">The compression level (1-9).</param>
/// <param name="useTar">Whether to wrap files in a TAR archive before compressing.</param>
public override void Compress(
DirectoryPath rootPath,
FilePath outputPath,
IEnumerable<FilePath> filePaths,
int level)
int level,
bool useTar = true)
{
ArgumentNullException.ThrowIfNull(rootPath, nameof(rootPath));
ArgumentNullException.ThrowIfNull(outputPath, nameof(outputPath));
Expand All @@ -50,32 +53,52 @@ public override void Compress(
var outputFile = FileSystem.GetFile(outputPath);

// Open up a stream to the output file.
Log.Verbose("Creating Zip file: {0}", outputPath.FullPath);
Log.Verbose("Creating GZip file: {0}", outputPath.FullPath);

using (var outputStream = outputFile.Open(FileMode.Create, FileAccess.Write, FileShare.None))
using (var gzipOutputStream = new GZipOutputStream(outputStream))
using (var tarOutputStream = new TarOutputStream(gzipOutputStream, Encoding.UTF8))
{
gzipOutputStream.SetLevel(level);

foreach (var inputPath in filePaths)
if (useTar)
{
var absoluteInputPath = inputPath.MakeAbsolute(Environment);
var file = FileSystem.GetFile(absoluteInputPath);

using (var inputStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
using (var gzipOutputStream = new GZipOutputStream(outputStream))
using (var tarOutputStream = new TarOutputStream(gzipOutputStream, Encoding.UTF8))
{
gzipOutputStream.SetLevel(level);

foreach (var inputPath in filePaths)
{
var absoluteInputPath = inputPath.MakeAbsolute(Environment);
var file = FileSystem.GetFile(absoluteInputPath);

using (var inputStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
{
// Get the relative filename to the rootPath.
var relativeFilePath = GetRelativeFilePath(rootPath, absoluteInputPath);
Log.Verbose("Compressing file {0}", absoluteInputPath);

// Create the tar archive entry.
var entry = TarEntry.CreateTarEntry(relativeFilePath.FullPath);
entry.Size = inputStream.Length;

tarOutputStream.PutNextEntry(entry);
inputStream.CopyTo(tarOutputStream);
tarOutputStream.CloseEntry();
}
}
}
}
else
{
using (var gzipOutputStream = new GZipOutputStream(outputStream))
{
// Get the relative filename to the rootPath.
var relativeFilePath = GetRelativeFilePath(rootPath, absoluteInputPath);
gzipOutputStream.SetLevel(level);
var inputPath = filePaths.First();
var absoluteInputPath = inputPath.MakeAbsolute(Environment);
var file = FileSystem.GetFile(absoluteInputPath);
Log.Verbose("Compressing file {0}", absoluteInputPath);

// Create the tar archive entry.
var entry = TarEntry.CreateTarEntry(relativeFilePath.FullPath);
entry.Size = inputStream.Length;

tarOutputStream.PutNextEntry(entry);
inputStream.CopyTo(tarOutputStream);
tarOutputStream.CloseEntry();
using (var inputStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
{
inputStream.CopyTo(gzipOutputStream);
}
}
}
}
Expand Down
60 changes: 36 additions & 24 deletions src/Cake.Compression/CompressionAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ public static void BZip2Uncompress(
public static void GZipCompress(
this ICakeContext context,
DirectoryPath rootPath,
FilePath outputPath) =>
Compress<GZip>(context, rootPath, outputPath, Level);
FilePath outputPath,
bool useTar = true) =>
Compress<GZip>(context, rootPath, outputPath, Level, useTar);

/// <summary>
/// Create a GZip Tar archive of the specified directory.
Expand All @@ -265,8 +266,9 @@ public static void GZipCompress(
this ICakeContext context,
DirectoryPath rootPath,
FilePath outputPath,
int level) =>
Compress<GZip>(context, rootPath, outputPath, level);
int level,
bool useTar = true) =>
Compress<GZip>(context, rootPath, outputPath, level, useTar);

/// <summary>
/// Create a GZip Tar archive of the files matching the specified pattern.
Expand All @@ -285,8 +287,9 @@ public static void GZipCompress(
this ICakeContext context,
DirectoryPath rootPath,
FilePath outputPath,
string pattern) =>
Compress<GZip>(context, rootPath, outputPath, pattern, Level);
string pattern,
bool useTar = true) =>
Compress<GZip>(context, rootPath, outputPath, pattern, Level, useTar);

/// <summary>
/// Create a GZip Tar archive of the files matching the specified pattern.
Expand All @@ -307,8 +310,9 @@ public static void GZipCompress(
DirectoryPath rootPath,
FilePath outputPath,
string pattern,
int level) =>
Compress<GZip>(context, rootPath, outputPath, pattern, level);
int level,
bool useTar = true) =>
Compress<GZip>(context, rootPath, outputPath, pattern, level, useTar);

/// <summary>
/// Create a GZip Tar archive of the specified files.
Expand All @@ -328,8 +332,9 @@ public static void GZipCompress(
this ICakeContext context,
DirectoryPath rootPath,
FilePath outputPath,
IEnumerable<FilePath> filePaths) =>
Compress<GZip>(context, rootPath, outputPath, filePaths, Level);
IEnumerable<FilePath> filePaths,
bool useTar = true) =>
Compress<GZip>(context, rootPath, outputPath, filePaths, Level, useTar);

/// <summary>
/// Create a GZip Tar archive of the specified files.
Expand All @@ -351,8 +356,9 @@ public static void GZipCompress(
DirectoryPath rootPath,
FilePath outputPath,
IEnumerable<FilePath> filePaths,
int level) =>
Compress<GZip>(context, rootPath, outputPath, filePaths, level);
int level,
bool useTar = true) =>
Compress<GZip>(context, rootPath, outputPath, filePaths, level, useTar);

/// <summary>
/// Create a GZip Tar archive of the specified files.
Expand All @@ -377,8 +383,9 @@ public static void GZipCompress(
this ICakeContext context,
DirectoryPath rootPath,
FilePath outputPath,
IEnumerable<string> filePaths) =>
Compress<GZip>(context, rootPath, outputPath, filePaths, Level);
IEnumerable<string> filePaths,
bool useTar = true) =>
Compress<GZip>(context, rootPath, outputPath, filePaths, Level, useTar);

/// <summary>
/// Create a GZip Tar archive of the specified files.
Expand All @@ -405,8 +412,9 @@ public static void GZipCompress(
DirectoryPath rootPath,
FilePath outputPath,
IEnumerable<string> filePaths,
int level) =>
Compress<GZip>(context, rootPath, outputPath, filePaths, level);
int level,
bool useTar = true) =>
Compress<GZip>(context, rootPath, outputPath, filePaths, level, useTar);

/// <summary>
/// Decompress the specified GZip Tar file.
Expand Down Expand Up @@ -667,7 +675,8 @@ private static void Compress<T>(
ICakeContext context,
DirectoryPath rootPath,
FilePath outputPath,
int level) where T : CompressionBase
int level,
bool useTar = true) where T : CompressionBase
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
ArgumentNullException.ThrowIfNull(rootPath, nameof(rootPath));
Expand All @@ -676,15 +685,16 @@ private static void Compress<T>(
ArgumentOutOfRangeException.ThrowIfGreaterThan(level, 9, nameof(level));

var filePaths = context.GetFiles(string.Concat(rootPath, "/**/*"));
Compress<T>(context, rootPath, outputPath, filePaths, level);
Compress<T>(context, rootPath, outputPath, filePaths, level, useTar);
}

private static void Compress<T>(
ICakeContext context,
DirectoryPath rootPath,
FilePath outputPath,
string pattern,
int level) where T : CompressionBase
int level,
bool useTar = true) where T : CompressionBase
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
ArgumentNullException.ThrowIfNull(rootPath, nameof(rootPath));
Expand All @@ -701,15 +711,16 @@ private static void Compress<T>(
return;
}

Compress<T>(context, rootPath, outputPath, filePaths, level);
Compress<T>(context, rootPath, outputPath, filePaths, level, useTar);
}

private static void Compress<T>(
ICakeContext context,
DirectoryPath rootPath,
FilePath outputPath,
IEnumerable<FilePath> filePaths,
int level) where T : CompressionBase
int level,
bool useTar = true) where T : CompressionBase
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
ArgumentNullException.ThrowIfNull(rootPath, nameof(rootPath));
Expand All @@ -719,15 +730,16 @@ private static void Compress<T>(
ArgumentOutOfRangeException.ThrowIfGreaterThan(level, 9, nameof(level));

var zip = CreateInstance<T>(context.FileSystem, context.Environment, context.Log);
zip.Compress(rootPath, outputPath, filePaths, level);
zip.Compress(rootPath, outputPath, filePaths, level, useTar);
}

private static void Compress<T>(
ICakeContext context,
DirectoryPath rootPath,
FilePath outputPath,
IEnumerable<string> filePaths,
int level) where T : CompressionBase
int level,
bool useTar = true) where T : CompressionBase
{
ArgumentNullException.ThrowIfNull(context, nameof(context));
ArgumentNullException.ThrowIfNull(rootPath, nameof(rootPath));
Expand All @@ -737,7 +749,7 @@ private static void Compress<T>(
ArgumentOutOfRangeException.ThrowIfGreaterThan(level, 9, nameof(level));

var paths = filePaths.Select(path => new FilePath(path)).ToList();
Compress<T>(context, rootPath, outputPath, paths, level);
Compress<T>(context, rootPath, outputPath, paths, level, useTar);
}

private static void Decompress<T>(
Expand Down