From 559190158a9e7ac37d102b81ab9947972149dd43 Mon Sep 17 00:00:00 2001 From: That-One-Nerd Date: Thu, 30 Mar 2023 19:31:14 -0400 Subject: [PATCH] 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)