Anul 3 Semestrul 2

This commit is contained in:
2025-07-03 20:56:38 +03:00
parent 184f3bd92e
commit 3b7fb85767
269 changed files with 20955 additions and 0 deletions
@@ -0,0 +1,36 @@
using System;
using FileCompression.Interfaces;
namespace FileCompression.Services.Strategies;
public class StrategyContext
{
private ICompressionStrategy? _compressionStrategy;
public StrategyContext()
{
}
public StrategyContext(ICompressionStrategy compressionStrategy)
{
_compressionStrategy = compressionStrategy;
}
public void SetStrategy(ICompressionStrategy compressionStrategy)
{
_compressionStrategy = compressionStrategy;
}
public void CompressFile(string sourcePath, string destinationPath, string basePath)
{
if (_compressionStrategy == null)
{
throw new InvalidOperationException("Compression strategy is not set.");
}
_compressionStrategy.CompressFile(sourcePath, destinationPath, basePath);
}
public void CompressDirectory(string sourcePath, string destinationPath, string basePath)
{
if (_compressionStrategy == null)
{
throw new InvalidOperationException("Compression strategy is not set.");
}
_compressionStrategy.CompressDirectory(sourcePath, destinationPath, basePath);
}
}
@@ -0,0 +1,68 @@
using System;
using System.Formats.Tar;
using FileCompression.Interfaces;
using FileCompression.Utils;
namespace FileCompression.Services.Strategies;
public class TarGzStrategy : ICompressionStrategy
{
public void CompressFile(string sourcePath, string destinationPath, string basePath)
{
Console.WriteLine($"Compressing {sourcePath} to {destinationPath} using tar and gzip.");
string tempPath = Path.GetTempFileName();
using (var tempStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
using (var tempWriter = new TarWriter(tempStream, TarEntryFormat.Pax))
{
// Copy existing entries to temp file
if (File.Exists(destinationPath))
{
using (var originalStream = new FileStream(destinationPath, FileMode.Open, FileAccess.Read))
using (var tarReader = new TarReader(originalStream, leaveOpen: false))
{
TarEntry? entry;
while ((entry = tarReader.GetNextEntry()) is not null)
{
tempWriter.WriteEntry(entry);
}
}
}
// Add new entry
string entryName = ArchiveUtils.EntryFromPath(sourcePath.AsSpan(basePath.Length));
tempWriter.WriteEntry(sourcePath, entryName);
}
File.Copy(tempPath, destinationPath, overwrite: true);
File.Delete(tempPath);
}
public void CompressDirectory(string sourcePath, string destinationPath, string basePath)
{
Console.WriteLine($"Compressing {sourcePath} to {destinationPath} using tar and gzip.");
string tempPath = Path.GetTempFileName();
using (var tempStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
using (var tempWriter = new TarWriter(tempStream, TarEntryFormat.Pax))
{
// Copy existing entries to temp file
if (File.Exists(destinationPath))
{
using (var originalStream = new FileStream(destinationPath, FileMode.Open, FileAccess.Read))
using (var tarReader = new TarReader(originalStream, leaveOpen: false))
{
TarEntry? entry;
while ((entry = tarReader.GetNextEntry()) is not null)
{
tempWriter.WriteEntry(entry);
}
}
}
// Add new entry
string entryName = ArchiveUtils.EntryFromPath(sourcePath.AsSpan(basePath.Length), appendPathSeparator: true);
tempWriter.WriteEntry(sourcePath, entryName);
}
}
}
@@ -0,0 +1,72 @@
using System;
using FileCompression.Interfaces;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using FileCompression.Utils;
namespace FileCompression.Services.Strategies;
public class ZipStrategy : ICompressionStrategy
{
public void CompressFile(string sourcePath, string destinationPath, string basePath)
{
Console.WriteLine($"Compressing file {sourcePath} to {destinationPath}");
using (Stream destination = new FileStream(destinationPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using ZipArchive archive = new ZipArchive(destination, ZipArchiveMode.Update);
string entryName = ArchiveUtils.EntryFromPath(sourcePath.AsSpan(basePath.Length));
DoCreateEntryFromFile(archive, sourcePath, entryName, CompressionLevel.Optimal);
}
}
public void CompressDirectory(string sourcePath, string destinationPath, string basePath)
{
Console.WriteLine($"Compressing directory {sourcePath} to {destinationPath}");
using (Stream destination = new FileStream(destinationPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using ZipArchive archive = new ZipArchive(destination, ZipArchiveMode.Update);
string entryName = ArchiveUtils.EntryFromPath(sourcePath.AsSpan(basePath.Length), appendPathSeparator: true);
archive.CreateEntry(entryName);
}
}
private ZipArchiveEntry DoCreateEntryFromFile(ZipArchive destination,
string sourceFileName, string entryName, CompressionLevel? compressionLevel)
{
ArgumentNullException.ThrowIfNull(destination);
ArgumentNullException.ThrowIfNull(sourceFileName);
ArgumentNullException.ThrowIfNull(entryName);
// Checking of compressionLevel is passed down to DeflateStream and the IDeflater implementation
// as it is a pluggable component that completely encapsulates the meaning of compressionLevel.
// Argument checking gets passed down to FileStream's ctor and CreateEntry
using (FileStream fs = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 0x1000, useAsync: false))
{
ZipArchiveEntry entry = compressionLevel.HasValue
? destination.CreateEntry(entryName, compressionLevel.Value)
: destination.CreateEntry(entryName);
DateTime lastWrite = File.GetLastWriteTime(sourceFileName);
// If file to be archived has an invalid last modified time, use the first datetime representable in the Zip timestamp format
// (midnight on January 1, 1980):
if (lastWrite.Year < 1980 || lastWrite.Year > 2107)
lastWrite = new DateTime(1980, 1, 1, 0, 0, 0);
entry.LastWriteTime = lastWrite;
using (Stream es = entry.Open())
fs.CopyTo(es);
return entry;
}
}
}