44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using System;
|
|
using System.IO.Compression;
|
|
using FileCompression.Interfaces;
|
|
using FileCompression.Services.Strategies;
|
|
|
|
namespace FileCompression.Services.Composites;
|
|
|
|
public class TarCompressor : ICompressor
|
|
{
|
|
|
|
private readonly string sourcePath;
|
|
private readonly string destinationPath;
|
|
private readonly StrategyContext strategyContext;
|
|
private readonly string basePath;
|
|
private readonly string tarPath;
|
|
private ICompressor compressor;
|
|
public TarCompressor(string sourcePath, string destinationPath, string basePath, StrategyContext strategyContext)
|
|
{
|
|
this.sourcePath = sourcePath;
|
|
this.destinationPath = destinationPath;
|
|
this.strategyContext = strategyContext;
|
|
this.basePath = basePath;
|
|
this.tarPath = destinationPath + ".tar";
|
|
|
|
compressor = File.Exists(sourcePath) ? new FileCompressor(sourcePath, tarPath, basePath, strategyContext) : new FolderCompressor(sourcePath, tarPath, basePath, strategyContext);
|
|
}
|
|
public void Compress()
|
|
{
|
|
compressor.Compress();
|
|
GZipCompress();
|
|
}
|
|
|
|
private void GZipCompress()
|
|
{
|
|
using (var fileStream = new FileStream(tarPath, FileMode.Open))
|
|
using (var compressedFileStream = File.Create(destinationPath))
|
|
using (var compressionStream = new GZipStream(compressedFileStream, CompressionLevel.Optimal))
|
|
{
|
|
fileStream.CopyTo(compressionStream);
|
|
}
|
|
File.Delete(tarPath);
|
|
}
|
|
}
|