From f95e3a89fff17cf506661b57af683ecd8a2e6ae2 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Wed, 29 Mar 2023 10:19:34 -0400 Subject: [PATCH 01/11] Added a comment option for the command (#27) --- SrcMod/Shell/Modules/CompressionModule.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index 35742a9..d009282 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -5,7 +5,7 @@ public static class CompressionModule { [Command("zip")] public static void CompressZip(string source, string? destination = null, - CompressionLevel level = CompressionLevel.Optimal) + CompressionLevel level = CompressionLevel.Optimal, string comment = "") { destination ??= Path.Combine(Path.GetDirectoryName(Path.GetFullPath(source))!, $"{Path.GetFileNameWithoutExtension(source)}.zip"); @@ -20,7 +20,10 @@ public static class CompressionModule Write(message); Stream writer = new FileStream(destination, FileMode.CreateNew); - ZipArchive archive = new(writer, ZipArchiveMode.Create); + ZipArchive archive = new(writer, ZipArchiveMode.Create) + { + Comment = comment + }; archive.CreateEntryFromFile(absSource, Path.GetFileName(absSource), level); From 506892b6e0e4248c7f3ec5aafdb5fa0b20997eb7 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Wed, 29 Mar 2023 10:39:11 -0400 Subject: [PATCH 02/11] Added an extraction module, among one other thing. --- SrcMod/Shell/Modules/CompressionModule.cs | 7 +++--- SrcMod/Shell/Modules/ExtractionModule.cs | 26 +++++++++++++++++++++++ SrcMod/Shell/Shell.cs | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 SrcMod/Shell/Modules/ExtractionModule.cs diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index d009282..21aecfc 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -20,10 +20,8 @@ public static class CompressionModule Write(message); Stream writer = new FileStream(destination, FileMode.CreateNew); - ZipArchive archive = new(writer, ZipArchiveMode.Create) - { - Comment = comment - }; + ZipArchive archive = new(writer, ZipArchiveMode.Create); + if (!string.IsNullOrWhiteSpace(comment)) archive.Comment = comment; archive.CreateEntryFromFile(absSource, Path.GetFileName(absSource), level); @@ -42,6 +40,7 @@ public static class CompressionModule Stream writer = new FileStream(destination, FileMode.CreateNew); ZipArchive archive = new(writer, ZipArchiveMode.Create); + if (!string.IsNullOrWhiteSpace(comment)) archive.Comment = comment; List files = new(GetAllFiles(absSource)), relative = new(); diff --git a/SrcMod/Shell/Modules/ExtractionModule.cs b/SrcMod/Shell/Modules/ExtractionModule.cs new file mode 100644 index 0000000..2eb82a2 --- /dev/null +++ b/SrcMod/Shell/Modules/ExtractionModule.cs @@ -0,0 +1,26 @@ +namespace SrcMod.Shell.Modules; + +[Module("extract")] +public static class ExtractionModule +{ + [Command("zip")] + public static void ExtractZip(string source, string? destination = null) + { + if (!File.Exists(source)) throw new($"No file exists at \"{source}\"."); + + destination ??= Path.GetDirectoryName(Path.GetFullPath(source)); + if (destination is null) throw new("Error detecting destination path."); + + if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); + + FileStream reader = new(source, FileMode.Open); + ZipArchive zip = new(reader, ZipArchiveMode.Read); + + if (!string.IsNullOrWhiteSpace(zip.Comment)) Write(zip.Comment); + + zip.ExtractToDirectory(destination, true); + + zip.Dispose(); + reader.Dispose(); + } +} diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 0ea44b3..7275078 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -4,7 +4,7 @@ public class Shell { public const string Author = "That_One_Nerd"; public const string Name = "SrcMod"; - public const string Version = "Alpha 0.2.2"; + public const string Version = "Alpha 0.3.0"; public readonly string? ShellDirectory; From 9928f7d62f9ef7c2e04b8ee77e65cfd2be1baa3f Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Wed, 29 Mar 2023 13:48:34 -0400 Subject: [PATCH 03/11] Fixed an issue with automatic destinations (#29) --- SrcMod/Shell/Modules/CompressionModule.cs | 44 ++++++++++++++++++++--- SrcMod/Shell/Modules/ExtractionModule.cs | 10 ++++-- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index 21aecfc..1fdf64f 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -7,8 +7,13 @@ public static class CompressionModule public static void CompressZip(string source, string? destination = null, CompressionLevel level = CompressionLevel.Optimal, string comment = "") { - destination ??= Path.Combine(Path.GetDirectoryName(Path.GetFullPath(source))!, - $"{Path.GetFileNameWithoutExtension(source)}.zip"); + if (destination is null) + { + string full = Path.GetFullPath(source); + string name = Path.GetFileNameWithoutExtension(full); + string folder = Program.Shell!.WorkingDirectory; + destination ??= $"{folder}\\{name}.zip"; + } string absSource = Path.GetFullPath(source), localDest = Path.GetRelativePath(Program.Shell!.WorkingDirectory, destination); @@ -44,13 +49,34 @@ public static class CompressionModule List files = new(GetAllFiles(absSource)), relative = new(); - foreach (string f in files) relative.Add(Path.GetRelativePath(absSource, f)); + for (int i = 0; i < files.Count; i++) + { + string f = files[i]; + if (f.Trim().ToLower() == destination.Trim().ToLower()) + { + files.RemoveAt(i); + i--; + continue; + } + relative.Add(Path.GetRelativePath(absSource, f)); + } + + int failed = 0; LoadingBarStart(); for (int i = 0; i < files.Count; i++) { - archive.CreateEntryFromFile(files[i], relative[i], level); - LoadingBarSet((i + 1) / (float)files.Count, ConsoleColor.DarkGreen); + bool failedThisTime = false; + try + { + archive.CreateEntryFromFile(files[i], relative[i], level); + } + catch + { + failedThisTime = true; + failed++; + } + LoadingBarSet((i + 1) / (float)files.Count, failedThisTime ? ConsoleColor.Red : ConsoleColor.DarkGreen); ; Console.CursorLeft = 0; string message = $"{relative[i]}"; int remainder = Console.BufferWidth - message.Length; @@ -69,6 +95,14 @@ public static class CompressionModule Write(new string(' ', Console.BufferWidth), newLine: false); Console.SetCursorPosition(0, Console.CursorTop - 2); Write(new string(' ', Console.BufferWidth), newLine: false); + + if (failed > 0) + { + Console.CursorLeft = 0; + Console.CursorTop--; + Write($"{failed} file{(failed == 1 ? " has" : "s have")} been ignored due to an error.", + ConsoleColor.DarkYellow); + } } else throw new("No file or directory located at \"source\""); diff --git a/SrcMod/Shell/Modules/ExtractionModule.cs b/SrcMod/Shell/Modules/ExtractionModule.cs index 2eb82a2..d29774a 100644 --- a/SrcMod/Shell/Modules/ExtractionModule.cs +++ b/SrcMod/Shell/Modules/ExtractionModule.cs @@ -8,8 +8,14 @@ public static class ExtractionModule { if (!File.Exists(source)) throw new($"No file exists at \"{source}\"."); - destination ??= Path.GetDirectoryName(Path.GetFullPath(source)); - if (destination is null) throw new("Error detecting destination path."); + if (destination is null) + { + string full = Path.GetFullPath(source); + string folder = Program.Shell!.WorkingDirectory; + string name = Path.GetFileNameWithoutExtension(full); + + destination = $"{folder}\\{name}"; + } if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); From 97385990882fbc64b0da6f9944eb63b0c427ce81 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Wed, 29 Mar 2023 17:40:26 -0400 Subject: [PATCH 04/11] Part of the GZip system. **INCOMPLETE** --- SrcMod/Shell/Modules/CompressionModule.cs | 57 ++++++++++++++++++++++- SrcMod/Shell/Shell.cs | 8 +++- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index 1fdf64f..c2ceb71 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -3,6 +3,59 @@ [Module("compress")] public static class CompressionModule { + [Command("gz")] + [Command("gzip")] + public static void CompressGzip(string source, string? destination = null, + CompressionLevel level = CompressionLevel.Optimal) + { + if (destination is null) + { + string full = Path.GetFullPath(source); + string name = Path.GetFileNameWithoutExtension(full); + string folder = Program.Shell!.WorkingDirectory; + destination ??= $"{folder}\\{name}.gz"; + } + + string absSource = Path.GetFullPath(source), + localDest = Path.GetRelativePath(Program.Shell!.WorkingDirectory, destination); + + if (File.Exists(source)) + { + if (File.Exists(destination)) throw new($"File already exists at \"{localDest}\""); + string message = $"Compressing file at \"{source}\" into \"{localDest}\"..."; + Write(message); + + FileStream writer = new(localDest, FileMode.CreateNew); + FileStream reader = new(absSource, FileMode.Open); + GZipStream gzip = new(writer, level); + + LoadingBarStart(); + + const int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + for (int i = 0; i < reader.Length; i += bufferSize) + { + int size = reader.Read(buffer, 0, bufferSize); + gzip.Write(buffer, 0, size); + gzip.Flush(); + + LoadingBarSet((float)i / gzip.Length, ConsoleColor.Magenta); + } + + LoadingBarEnd(); + + gzip.Close(); + reader.Close(); + writer.Close(); + + Console.CursorLeft = 0; + Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; + Write(new string(' ', message.Length), newLine: false); + } + else if (Directory.Exists(source)) throw new("The GZip format can only compress 1 file."); + else throw new("No file located at \"source\""); + } + [Command("zip")] public static void CompressZip(string source, string? destination = null, CompressionLevel level = CompressionLevel.Optimal, string comment = "") @@ -24,7 +77,7 @@ public static class CompressionModule string message = $"Compressing file at \"{source}\" into \"{localDest}\"..."; Write(message); - Stream writer = new FileStream(destination, FileMode.CreateNew); + FileStream writer = new(destination, FileMode.CreateNew); ZipArchive archive = new(writer, ZipArchiveMode.Create); if (!string.IsNullOrWhiteSpace(comment)) archive.Comment = comment; @@ -43,7 +96,7 @@ public static class CompressionModule Write($"Compressing folder at \"{source}\" into \"{localDest}\"..."); - Stream writer = new FileStream(destination, FileMode.CreateNew); + FileStream writer = new(destination, FileMode.CreateNew); ZipArchive archive = new(writer, ZipArchiveMode.Create); if (!string.IsNullOrWhiteSpace(comment)) archive.Comment = comment; diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 7275078..3388eca 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -4,7 +4,7 @@ public class Shell { public const string Author = "That_One_Nerd"; public const string Name = "SrcMod"; - public const string Version = "Alpha 0.3.0"; + public const string Version = "Alpha 0.3"; public readonly string? ShellDirectory; @@ -170,10 +170,13 @@ public class Shell if (command.NameId.Trim().ToLower() != commandName) continue; int start = module.NameIsPrefix ? 2 : 1; string[] args = parts.GetRange(start, parts.Count - start).ToArray(); - + +#if RELEASE try { +#endif command.Invoke(args); +#if RELEASE } catch (TargetInvocationException ex) { @@ -185,6 +188,7 @@ public class Shell Write($"[ERROR] {ex.Message}", ConsoleColor.Red); if (LoadingBarEnabled) LoadingBarEnd(); } +#endif return; } } From 87166acda366348c5466843f3990cefcbb28ebeb Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Wed, 29 Mar 2023 19:02:55 -0400 Subject: [PATCH 05/11] Added GZip compatibility --- SrcMod/Shell/Modules/CompressionModule.cs | 27 ++++++------ SrcMod/Shell/Modules/ExtractionModule.cs | 51 +++++++++++++++++++++++ 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index c2ceb71..1b9581a 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -5,14 +5,14 @@ public static class CompressionModule { [Command("gz")] [Command("gzip")] - public static void CompressGzip(string source, string? destination = null, + public static void CompressGZip(string source, string? destination = null, CompressionLevel level = CompressionLevel.Optimal) { if (destination is null) { - string full = Path.GetFullPath(source); - string name = Path.GetFileNameWithoutExtension(full); - string folder = Program.Shell!.WorkingDirectory; + string full = Path.GetFullPath(source), + name = Path.GetFileName(full), + folder = Program.Shell!.WorkingDirectory; destination ??= $"{folder}\\{name}.gz"; } @@ -25,21 +25,21 @@ public static class CompressionModule string message = $"Compressing file at \"{source}\" into \"{localDest}\"..."; Write(message); - FileStream writer = new(localDest, FileMode.CreateNew); - FileStream reader = new(absSource, FileMode.Open); + FileStream writer = new(localDest, FileMode.CreateNew), + reader = new(absSource, FileMode.Open); GZipStream gzip = new(writer, level); LoadingBarStart(); - const int bufferSize = 1024; + int bufferSize = Mathf.Clamp((int)reader.Length / Console.BufferWidth, 1024 * 1024, 128 * 1024 * 1024); byte[] buffer = new byte[bufferSize]; - for (int i = 0; i < reader.Length; i += bufferSize) + for (long i = 0; i < reader.Length; i += bufferSize) { int size = reader.Read(buffer, 0, bufferSize); gzip.Write(buffer, 0, size); gzip.Flush(); - LoadingBarSet((float)i / gzip.Length, ConsoleColor.Magenta); + LoadingBarSet((float)i / reader.Length, ConsoleColor.DarkGreen); } LoadingBarEnd(); @@ -49,7 +49,7 @@ public static class CompressionModule writer.Close(); Console.CursorLeft = 0; - Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; + Console.CursorTop -= (message.Length / Console.BufferWidth) + 2; Write(new string(' ', message.Length), newLine: false); } else if (Directory.Exists(source)) throw new("The GZip format can only compress 1 file."); @@ -62,9 +62,9 @@ public static class CompressionModule { if (destination is null) { - string full = Path.GetFullPath(source); - string name = Path.GetFileNameWithoutExtension(full); - string folder = Program.Shell!.WorkingDirectory; + string full = Path.GetFullPath(source), + name = Path.GetFileNameWithoutExtension(full), + folder = Program.Shell!.WorkingDirectory; destination ??= $"{folder}\\{name}.zip"; } @@ -152,7 +152,6 @@ public static class CompressionModule if (failed > 0) { Console.CursorLeft = 0; - Console.CursorTop--; Write($"{failed} file{(failed == 1 ? " has" : "s have")} been ignored due to an error.", ConsoleColor.DarkYellow); } diff --git a/SrcMod/Shell/Modules/ExtractionModule.cs b/SrcMod/Shell/Modules/ExtractionModule.cs index d29774a..ef342cc 100644 --- a/SrcMod/Shell/Modules/ExtractionModule.cs +++ b/SrcMod/Shell/Modules/ExtractionModule.cs @@ -3,6 +3,57 @@ [Module("extract")] public static class ExtractionModule { + [Command("gz")] + [Command("gzip")] + public static void ExtractGZip(string source, string? destination = null) + { + if (!File.Exists(source)) throw new($"No file exists at \"{source}\"."); + + if (destination is null) + { + string full = Path.GetFullPath(source); + string folder = Program.Shell!.WorkingDirectory; + string name = Path.GetFileNameWithoutExtension(full); + + destination = $"{folder}\\{name}"; + } + + string absSource = Path.GetFullPath(source), + localDest = Path.GetRelativePath(Program.Shell!.WorkingDirectory, destination); + + if (File.Exists(destination)) throw new($"File already exists at \"{destination}\"."); + string message = $"Extracting file at \"{source}\" into \"{localDest}\"..."; + Write(message); + + FileStream writer = new(destination, FileMode.CreateNew), + reader = new(absSource, FileMode.Open); + GZipStream gzip = new(reader, CompressionMode.Decompress); + + LoadingBarStart(); + + int bufferSize = Mathf.Clamp((int)reader.Length / Console.BufferWidth, 1024 * 1024, 128 * 1024 * 1024); + byte[] buffer = new byte[bufferSize]; + int i = 0; + int size; + while ((size = gzip.Read(buffer, i, bufferSize)) > 0) + { + writer.Write(buffer, 0, size); + writer.Flush(); + + LoadingBarSet((float)i / reader.Length, ConsoleColor.DarkGreen); + } + + LoadingBarEnd(); + + gzip.Close(); + reader.Close(); + writer.Close(); + + Console.CursorLeft = 0; + Console.CursorTop -= (message.Length / Console.BufferWidth) + 2; + Write(new string(' ', message.Length), newLine: false); + } + [Command("zip")] public static void ExtractZip(string source, string? destination = null) { From dda3ccad4d0a8f4bafc1dd5a67c06e3cd9690fa1 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Wed, 29 Mar 2023 20:27:10 -0400 Subject: [PATCH 06/11] Added tar functionality and only compression for tar-gz. Extraction coming soon. --- SrcMod/Shell/GlobalUsings.cs | 1 + SrcMod/Shell/Modules/CompressionModule.cs | 64 ++++++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) 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 = "") From 8f7c2775d40338a200917def1dd5ae3834b35320 Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Thu, 30 Mar 2023 09:06:09 -0400 Subject: [PATCH 07/11] Added support for extracting tar and tar.gz formats --- SrcMod/Shell/Modules/ExtractionModule.cs | 51 +++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/SrcMod/Shell/Modules/ExtractionModule.cs b/SrcMod/Shell/Modules/ExtractionModule.cs index ef342cc..e06d826 100644 --- a/SrcMod/Shell/Modules/ExtractionModule.cs +++ b/SrcMod/Shell/Modules/ExtractionModule.cs @@ -50,10 +50,59 @@ public static class ExtractionModule writer.Close(); Console.CursorLeft = 0; - Console.CursorTop -= (message.Length / Console.BufferWidth) + 2; + Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; + if (Console.CursorTop >= Console.BufferHeight - 3) Console.CursorTop--; Write(new string(' ', message.Length), newLine: false); } + [Command("tar")] + [Command("tarball")] + public static void ExtractTar(string source, string? destination = null) + { + if (!File.Exists(source)) throw new($"No file exists at \"{source}\"."); + + if (destination is null) + { + string full = Path.GetFullPath(source); + string folder = Program.Shell!.WorkingDirectory; + string name = Path.GetFileNameWithoutExtension(full); + + destination = $"{folder}\\{name}"; + } + + if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); + + FileStream reader = new(source, FileMode.Open); + TarFile.ExtractToDirectory(reader, Path.GetFileName(destination), true); + + reader.Dispose(); + } + + [Command("targz")] + [Command("tar.gz")] + [Command("tar-gz")] + public static void ExtractTarGz(string source, string? destination = null) + { + if (!File.Exists(source)) throw new($"No file exists at \"{source}\"."); + + if (destination is null) + { + string full = Path.GetFullPath(source); + string folder = Program.Shell!.WorkingDirectory; + string name = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(full)); + + destination = $"{folder}\\{name}"; + } + + string absSource = Path.GetFullPath(source), + temp = Path.Combine(Path.GetDirectoryName(absSource)!, Path.GetFileNameWithoutExtension(absSource)); + + ExtractGZip(source, temp); + ExtractTar(temp, destination); + + File.Delete(temp); + } + [Command("zip")] public static void ExtractZip(string source, string? destination = null) { From 33130bc5f8710a9cb432722b95cbb676200cb8da Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Thu, 30 Mar 2023 10:28:56 -0400 Subject: [PATCH 08/11] Optimized some buffer copying in GZip stuff. --- SrcMod/Shell/Modules/CompressionModule.cs | 17 ++--------------- SrcMod/Shell/Modules/ExtractionModule.cs | 17 +---------------- 2 files changed, 3 insertions(+), 31 deletions(-) diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index e1e87fa..043e653 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -32,27 +32,14 @@ public static class CompressionModule reader = new(absSource, FileMode.Open); GZipStream gzip = new(writer, level); - LoadingBarStart(); - - int bufferSize = Mathf.Clamp((int)reader.Length / Console.BufferWidth, 1024 * 1024, 128 * 1024 * 1024); - byte[] buffer = new byte[bufferSize]; - for (long i = 0; i < reader.Length; i += bufferSize) - { - int size = reader.Read(buffer, 0, bufferSize); - gzip.Write(buffer, 0, size); - gzip.Flush(); - - LoadingBarSet((float)i / reader.Length, ConsoleColor.DarkGreen); - } - - LoadingBarEnd(); + reader.CopyTo(gzip); gzip.Close(); reader.Close(); writer.Close(); Console.CursorLeft = 0; - Console.CursorTop -= (message.Length / Console.BufferWidth) + 2; + Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; Write(new string(' ', message.Length), newLine: false); } else if (Directory.Exists(source)) throw new("The GZip format can only compress 1 file."); diff --git a/SrcMod/Shell/Modules/ExtractionModule.cs b/SrcMod/Shell/Modules/ExtractionModule.cs index e06d826..a7d8bc7 100644 --- a/SrcMod/Shell/Modules/ExtractionModule.cs +++ b/SrcMod/Shell/Modules/ExtractionModule.cs @@ -29,21 +29,7 @@ public static class ExtractionModule reader = new(absSource, FileMode.Open); GZipStream gzip = new(reader, CompressionMode.Decompress); - LoadingBarStart(); - - int bufferSize = Mathf.Clamp((int)reader.Length / Console.BufferWidth, 1024 * 1024, 128 * 1024 * 1024); - byte[] buffer = new byte[bufferSize]; - int i = 0; - int size; - while ((size = gzip.Read(buffer, i, bufferSize)) > 0) - { - writer.Write(buffer, 0, size); - writer.Flush(); - - LoadingBarSet((float)i / reader.Length, ConsoleColor.DarkGreen); - } - - LoadingBarEnd(); + gzip.CopyTo(writer); gzip.Close(); reader.Close(); @@ -51,7 +37,6 @@ public static class ExtractionModule Console.CursorLeft = 0; Console.CursorTop -= (message.Length / Console.BufferWidth) + 1; - if (Console.CursorTop >= Console.BufferHeight - 3) Console.CursorTop--; Write(new string(' ', message.Length), newLine: false); } From 471bb54fe0035dd948c9004f0c3cdd1e401a11ea Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Thu, 30 Mar 2023 14:09:25 -0400 Subject: [PATCH 09/11] Maybe 7Zip extraction? I haven't tested it. --- SrcMod/Shell/GlobalUsings.cs | 2 ++ SrcMod/Shell/Modules/CompressionModule.cs | 11 +++++---- SrcMod/Shell/Modules/ExtractionModule.cs | 28 +++++++++++++++++++++++ SrcMod/Shell/Shell.cs | 2 +- SrcMod/Shell/Shell.csproj | 1 + 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/SrcMod/Shell/GlobalUsings.cs b/SrcMod/Shell/GlobalUsings.cs index c3222e5..56f20a3 100644 --- a/SrcMod/Shell/GlobalUsings.cs +++ b/SrcMod/Shell/GlobalUsings.cs @@ -1,4 +1,6 @@ global using Nerd_STF.Mathematics; +global using SharpCompress.Archives.SevenZip; +global using SharpCompress.Readers; global using SrcMod.Shell; global using SrcMod.Shell.Modules.ObjectModels; global using System; diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index 043e653..7415870 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -1,8 +1,9 @@ -using System.Reflection.Emit; -using System.Xml.Linq; - -namespace SrcMod.Shell.Modules; +namespace SrcMod.Shell.Modules; +// Some things that can be extracted can't be compressed by SharpCompress. +// In the future I might replace it with my own, but that'll take a *really* +// long time and I'm already planning to do that for the valve compression formats, +// so I'll seethe for now. [Module("compress")] public static class CompressionModule { @@ -228,4 +229,6 @@ public static class CompressionModule name = $"Compressed a file or folder into a zip archive located at \"{destination}\"" }); } + + // 7z can't be compressed. } diff --git a/SrcMod/Shell/Modules/ExtractionModule.cs b/SrcMod/Shell/Modules/ExtractionModule.cs index a7d8bc7..1a6285b 100644 --- a/SrcMod/Shell/Modules/ExtractionModule.cs +++ b/SrcMod/Shell/Modules/ExtractionModule.cs @@ -114,4 +114,32 @@ public static class ExtractionModule zip.Dispose(); reader.Dispose(); } + + [Command("7z")] + [Command("7zip")] + [Command("sevenzip")] + public static void Extract7Zip(string source, string? destination = null) + { + if (!File.Exists(source)) throw new($"No file exists at \"{source}\"."); + + if (destination is null) + { + string full = Path.GetFullPath(source); + string folder = Program.Shell!.WorkingDirectory; + string name = Path.GetFileNameWithoutExtension(full); + + destination = $"{folder}\\{name}"; + } + + if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); + + FileStream reader = new(source, FileMode.Open); + SevenZipArchive zip = SevenZipArchive.Open(source); + + IReader data = zip.ExtractAllEntries(); + data.WriteAllToDirectory(destination); + + zip.Dispose(); + reader.Dispose(); + } } diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index 3388eca..e62b406 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -4,7 +4,7 @@ public class Shell { public const string Author = "That_One_Nerd"; public const string Name = "SrcMod"; - public const string Version = "Alpha 0.3"; + public const string Version = "Alpha 0.3.0"; public readonly string? ShellDirectory; diff --git a/SrcMod/Shell/Shell.csproj b/SrcMod/Shell/Shell.csproj index 511ed2f..d662f45 100644 --- a/SrcMod/Shell/Shell.csproj +++ b/SrcMod/Shell/Shell.csproj @@ -31,6 +31,7 @@ + From ce72491109d1867c1fdc28a888881c3f25ce07cf Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Thu, 30 Mar 2023 19:13:43 -0400 Subject: [PATCH 10/11] Added 7z extraction. I can't do compression ATM. --- SrcMod/Shell/Modules/ExtractionModule.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/SrcMod/Shell/Modules/ExtractionModule.cs b/SrcMod/Shell/Modules/ExtractionModule.cs index 1a6285b..12e37f5 100644 --- a/SrcMod/Shell/Modules/ExtractionModule.cs +++ b/SrcMod/Shell/Modules/ExtractionModule.cs @@ -134,10 +134,16 @@ public static class ExtractionModule if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); FileStream reader = new(source, FileMode.Open); - SevenZipArchive zip = SevenZipArchive.Open(source); + SevenZipArchive zip = SevenZipArchive.Open(reader); IReader data = zip.ExtractAllEntries(); - data.WriteAllToDirectory(destination); + data.WriteAllToDirectory(destination, new() + { + ExtractFullPath = true, + Overwrite = true, + PreserveAttributes = true, + PreserveFileTime = true + }); zip.Dispose(); reader.Dispose(); From 559190158a9e7ac37d102b81ab9947972149dd43 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Thu, 30 Mar 2023 19:31:14 -0400 Subject: [PATCH 11/11] Added rar extraction (can't do compression). --- SrcMod/Shell/GlobalUsings.cs | 1 + SrcMod/Shell/Modules/CompressionModule.cs | 2 ++ SrcMod/Shell/Modules/ExtractionModule.cs | 31 +++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/SrcMod/Shell/GlobalUsings.cs b/SrcMod/Shell/GlobalUsings.cs index 56f20a3..1d90052 100644 --- a/SrcMod/Shell/GlobalUsings.cs +++ b/SrcMod/Shell/GlobalUsings.cs @@ -1,4 +1,5 @@ global using Nerd_STF.Mathematics; +global using SharpCompress.Archives.Rar; global using SharpCompress.Archives.SevenZip; global using SharpCompress.Readers; global using SrcMod.Shell; diff --git a/SrcMod/Shell/Modules/CompressionModule.cs b/SrcMod/Shell/Modules/CompressionModule.cs index 7415870..43d5804 100644 --- a/SrcMod/Shell/Modules/CompressionModule.cs +++ b/SrcMod/Shell/Modules/CompressionModule.cs @@ -82,6 +82,8 @@ public static class CompressionModule else throw new("No file or directory located at \"source\""); } + // Rar can't be compressed. + [Command("targz")] [Command("tar.gz")] [Command("tar-gz")] diff --git a/SrcMod/Shell/Modules/ExtractionModule.cs b/SrcMod/Shell/Modules/ExtractionModule.cs index 12e37f5..75e0d6c 100644 --- a/SrcMod/Shell/Modules/ExtractionModule.cs +++ b/SrcMod/Shell/Modules/ExtractionModule.cs @@ -40,6 +40,37 @@ public static class ExtractionModule Write(new string(' ', message.Length), newLine: false); } + [Command("rar")] + public static void ExtractRar(string source, string? destination = null) + { + if (!File.Exists(source)) throw new($"No file exists at \"{source}\"."); + + if (destination is null) + { + string full = Path.GetFullPath(source); + string folder = Program.Shell!.WorkingDirectory; + string name = Path.GetFileNameWithoutExtension(full); + + destination = $"{folder}\\{name}"; + } + + if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); + + FileStream reader = new(source, FileMode.Open); + RarArchive rar = RarArchive.Open(reader); + + IReader data = rar.ExtractAllEntries(); + data.WriteAllToDirectory(destination, new() + { + ExtractFullPath = true, + Overwrite = true, + PreserveFileTime = true + }); + + rar.Dispose(); + reader.Dispose(); + } + [Command("tar")] [Command("tarball")] public static void ExtractTar(string source, string? destination = null)