Files
2025-07-03 20:56:38 +03:00

60 lines
2.5 KiB
C#

using System;
using FileCompression.Enums;
using FileCompression.Interfaces;
using FileCompression.Services.Composites;
using FileCompression.Services.Strategies;
namespace FileCompression.Services;
public static class CompressorFactory
{
public static ICompressionStrategy CreateCompressorStrategy(CompressionMode mode)
{
return mode switch
{
CompressionMode.Zip => new ZipStrategy(),
CompressionMode.Tar => new TarGzStrategy(),
CompressionMode.TarGz => new TarGzStrategy(),
_ => throw new NotSupportedException($"Compression mode '{mode}' is not supported.")
};
}
public static ICompressor CreateCompressor(string sourcePath, string destinationPath, CompressionMode mode, StrategyContext strategyContext)
{
string basePath = sourcePath.Substring(0, sourcePath.LastIndexOf(Path.DirectorySeparatorChar));
basePath = basePath.Substring(0, sourcePath.LastIndexOf(Path.DirectorySeparatorChar));
Console.WriteLine($"Base path: {basePath}");
if (File.Exists(sourcePath))
{
switch (mode)
{
case CompressionMode.Zip:
return new FileCompressor(sourcePath, destinationPath, basePath, strategyContext);
case CompressionMode.Tar:
return new FileCompressor(sourcePath, destinationPath, basePath, strategyContext);
case CompressionMode.TarGz:
return new TarCompressor(sourcePath, destinationPath, basePath, strategyContext);
default:
throw new NotSupportedException($"Compression mode '{mode}' is not supported.");
}
}
if (Directory.Exists(sourcePath))
{
switch (mode)
{
case CompressionMode.Zip:
return new FolderCompressor(sourcePath, destinationPath, basePath, strategyContext);
case CompressionMode.Tar:
return new FolderCompressor(sourcePath, destinationPath, basePath, strategyContext);
case CompressionMode.TarGz:
return new TarCompressor(sourcePath, destinationPath, basePath, strategyContext);
default:
throw new NotSupportedException($"Compression mode '{mode}' is not supported.");
}
}
throw new ArgumentException("Source path must be a file or directory.", nameof(sourcePath));
}
}