Added tar functionality and only compression for tar-gz. Extraction coming soon.

This commit is contained in:
That-One-Nerd 2023-03-29 20:27:10 -04:00
parent 87166acda3
commit dda3ccad4d
2 changed files with 64 additions and 1 deletions

View File

@ -4,6 +4,7 @@ global using SrcMod.Shell.Modules.ObjectModels;
global using System; global using System;
global using System.Collections.Generic; global using System.Collections.Generic;
global using System.Diagnostics; global using System.Diagnostics;
global using System.Formats.Tar;
global using System.IO; global using System.IO;
global using System.IO.Compression; global using System.IO.Compression;
global using System.Linq; global using System.Linq;

View File

@ -1,4 +1,7 @@
namespace SrcMod.Shell.Modules; using System.Reflection.Emit;
using System.Xml.Linq;
namespace SrcMod.Shell.Modules;
[Module("compress")] [Module("compress")]
public static class CompressionModule public static class CompressionModule
@ -56,6 +59,65 @@ public static class CompressionModule
else throw new("No file located at \"source\""); else throw new("No file located at \"source\"");
} }
[Command("tar")]
[Command("tarball")]
public static void CompressTar(string source, string? destination = null)
{
if (destination is null)
{
string full = Path.GetFullPath(source),
name = Path.GetFileNameWithoutExtension(full),
folder = Program.Shell!.WorkingDirectory;
destination ??= $"{folder}\\{name}.tar";
}
string absSource = Path.GetFullPath(source),
localDest = Path.GetRelativePath(Program.Shell!.WorkingDirectory, destination);
if (File.Exists(source)) throw new("The Tar format cannot compress a single file.");
else if (Directory.Exists(source))
{
if (File.Exists(destination)) throw new($"File already exists at \"{localDest}\"");
Write($"Compressing folder at \"{source}\" into \"{localDest}\"...");
FileStream writer = new(destination, FileMode.CreateNew);
TarFile.CreateFromDirectory(absSource, writer, false);
writer.Close();
Console.CursorLeft = 0;
Write(new string(' ', Console.BufferWidth), newLine: false);
Console.SetCursorPosition(0, Console.CursorTop - 1);
Write(new string(' ', Console.BufferWidth), newLine: false);
}
else throw new("No file or directory located at \"source\"");
}
[Command("targz")]
[Command("tar.gz")]
[Command("tar-gz")]
public static void CompressTarGzip(string source, string? destination = null,
CompressionLevel level = CompressionLevel.Optimal)
{
if (destination is null)
{
string full = Path.GetFullPath(source),
name = Path.GetFileNameWithoutExtension(full),
folder = Program.Shell!.WorkingDirectory;
destination ??= $"{folder}\\{name}.tar.gz";
}
string firstDest = Path.GetFileNameWithoutExtension(destination);
CompressTar(source, firstDest);
CompressGZip(firstDest, destination, level);
File.Delete(firstDest);
Console.CursorLeft = 0;
Console.CursorTop--;
}
[Command("zip")] [Command("zip")]
public static void CompressZip(string source, string? destination = null, public static void CompressZip(string source, string? destination = null,
CompressionLevel level = CompressionLevel.Optimal, string comment = "") CompressionLevel level = CompressionLevel.Optimal, string comment = "")