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

58 lines
1.6 KiB
C#

using System;
using FileCompression.Enums;
using FileCompression.Services;
using FileCompression.Services.Strategies;
namespace FileCompression;
public class Compressor
{
private static Compressor? _compressor;
private static readonly Lock _lock = new();
private static readonly StrategyContext StrategyContext = new();
private Compressor()
{
}
public static Compressor Instance
{
get
{
lock (_lock)
{
if (_compressor == null)
{
_compressor = new Compressor();
}
return _compressor;
}
}
}
public void Compress(string sourcePath, string destinationPath, CompressionMode mode)
{
if (string.IsNullOrEmpty(sourcePath))
{
throw new ArgumentNullException(nameof(sourcePath), "Source path cannot be null or empty.");
}
if (string.IsNullOrEmpty(destinationPath))
{
throw new ArgumentNullException(nameof(destinationPath), "Destination path cannot be null or empty.");
}
if (File.Exists(destinationPath))
{
File.Delete(destinationPath);
}
StrategyContext.SetStrategy(CompressorFactory.CreateCompressorStrategy(mode));
var compressor = CompressorFactory.CreateCompressor(sourcePath, destinationPath, mode, StrategyContext);
compressor.Compress();
Console.WriteLine($"Compressed {sourcePath} to {destinationPath} using {mode} mode.");
Console.WriteLine($"Compression completed successfully.");
}
}