Anul 3 Semestrul 2
This commit is contained in:
+25
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using FileCompression.Interfaces;
|
||||
using FileCompression.Services.Strategies;
|
||||
|
||||
namespace FileCompression.Services.Composites;
|
||||
|
||||
public class FileCompressor : ICompressor
|
||||
{
|
||||
|
||||
private string sourcePath;
|
||||
private string destinationPath;
|
||||
private string basePath;
|
||||
private StrategyContext strategyContext;
|
||||
public FileCompressor(string sourcePath, string destinationPath, string basePath, StrategyContext strategyContext)
|
||||
{
|
||||
this.sourcePath = sourcePath;
|
||||
this.destinationPath = destinationPath;
|
||||
this.strategyContext = strategyContext;
|
||||
this.basePath = basePath;
|
||||
}
|
||||
public void Compress()
|
||||
{
|
||||
strategyContext.CompressFile(sourcePath, destinationPath, basePath);
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using FileCompression.Enums;
|
||||
using FileCompression.Interfaces;
|
||||
using FileCompression.Models;
|
||||
using FileCompression.Services.Strategies;
|
||||
|
||||
namespace FileCompression.Services.Composites;
|
||||
|
||||
public class FolderCompressor : ICompressor
|
||||
{
|
||||
private readonly string sourcePath;
|
||||
private readonly string destinationPath;
|
||||
private readonly StrategyContext strategyContext;
|
||||
private readonly string basePath;
|
||||
public FolderCompressor(string sourcePath, string destinationPath, string basePath, StrategyContext strategyContext)
|
||||
{
|
||||
this.sourcePath = sourcePath;
|
||||
this.destinationPath = destinationPath;
|
||||
this.strategyContext = strategyContext;
|
||||
this.basePath = basePath;
|
||||
}
|
||||
public void Compress()
|
||||
{
|
||||
strategyContext.CompressDirectory(sourcePath, destinationPath, basePath);
|
||||
var entries = GetEntries();
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
entry.Compress();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private List<ICompressor> GetEntries()
|
||||
{
|
||||
List<ICompressor> entries = [];
|
||||
foreach (var file in Directory.GetFiles(sourcePath))
|
||||
{
|
||||
entries.Add(new FileCompressor(file, destinationPath, basePath, strategyContext));
|
||||
}
|
||||
|
||||
foreach (var directory in Directory.GetDirectories(sourcePath))
|
||||
{
|
||||
entries.Add(new FolderCompressor(directory, destinationPath, basePath, strategyContext));
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user