diff --git a/SrcMod/Shell/GlobalUsings.cs b/SrcMod/Shell/GlobalUsings.cs index 13ff962..c3222e5 100644 --- a/SrcMod/Shell/GlobalUsings.cs +++ b/SrcMod/Shell/GlobalUsings.cs @@ -4,6 +4,7 @@ global using SrcMod.Shell.Modules.ObjectModels; global using System; global using System.Collections.Generic; global using System.Diagnostics; +global using System.Formats.Tar; global using System.IO; global using System.IO.Compression; global using System.Linq; diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index 1b9581a..e1e87fa 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -1,4 +1,7 @@ -namespace SrcMod.Shell.Modules; +using System.Reflection.Emit; +using System.Xml.Linq; + +namespace SrcMod.Shell.Modules; [Module("compress")] public static class CompressionModule @@ -56,6 +59,65 @@ public static class CompressionModule 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")] public static void CompressZip(string source, string? destination = null, CompressionLevel level = CompressionLevel.Optimal, string comment = "")