From 54dce49fddf58e88911d47c7bab92ea36d26ecba Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Tue, 28 Dec 2021 15:02:38 -0500 Subject: [PATCH] Archive fixes for replacing folder. --- Switch_Toolbox_Library/Helpers/TreeHelper.cs | 11 +++++++++++ .../Interfaces/FileFormatting/IArchiveFile.cs | 18 ++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Switch_Toolbox_Library/Helpers/TreeHelper.cs b/Switch_Toolbox_Library/Helpers/TreeHelper.cs index 8087e5c8..7a835be9 100644 --- a/Switch_Toolbox_Library/Helpers/TreeHelper.cs +++ b/Switch_Toolbox_Library/Helpers/TreeHelper.cs @@ -214,6 +214,17 @@ namespace Toolbox.Library } } + public static string GetFolderAbsoultePath(TreeNode node, ArchiveRootNodeWrapper rootNode) + { + string nodePath = node.FullPath; + int startIndex = nodePath.IndexOf(rootNode.Text); + if (startIndex > 0) + nodePath = nodePath.Substring(startIndex); + + string slash = Path.DirectorySeparatorChar.ToString(); + string slashAlt = Path.AltDirectorySeparatorChar.ToString(); + return nodePath.Replace(rootNode.Text + slash, string.Empty).Replace(slash ?? "", slashAlt); + } public static void AddFiles(TreeNode parentNode, ArchiveRootNodeWrapper rootNode, string[] Files) { diff --git a/Switch_Toolbox_Library/Interfaces/FileFormatting/IArchiveFile.cs b/Switch_Toolbox_Library/Interfaces/FileFormatting/IArchiveFile.cs index 45f3b172..566d0365 100644 --- a/Switch_Toolbox_Library/Interfaces/FileFormatting/IArchiveFile.cs +++ b/Switch_Toolbox_Library/Interfaces/FileFormatting/IArchiveFile.cs @@ -137,7 +137,10 @@ namespace Toolbox.Library if (ofd.ShowDialog() == DialogResult.OK) { - FileData = File.ReadAllBytes(ofd.FileName); + if (FileDataStream != null) + FileDataStream = new MemoryStream(File.ReadAllBytes(ofd.FileName)); + else + FileData = File.ReadAllBytes(ofd.FileName); return true; } return false; @@ -782,7 +785,18 @@ namespace Toolbox.Library Nodes.Clear(); - TreeHelper.AddFiles(this, RootNode, Directory.GetFiles(ofd.SelectedPath)); + var proccessedFiles = TreeHelper.ReadFiles(ofd.SelectedPath); + + string folderPath = TreeHelper.GetFolderAbsoultePath(this, RootNode); + for (int i = 0; i < proccessedFiles.Count; i++) + { + ArchiveFile.AddFile(new ArchiveFileInfo() + { + FileName = $"{folderPath}/{proccessedFiles[i].Item1}", + FileData = File.ReadAllBytes(proccessedFiles[i].Item2), + }); + } + RootNode.FillTreeNodes(); } }